Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compartment-mapper)!: invert keys/values of renames in captureFromMap #2667

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/compartment-mapper/src/capture-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ const captureCompartmentMap = (compartmentMap, sources) => {
return {
captureCompartmentMap,
captureSources,
compartmentRenames,
compartmentRenames: fromEntries(
entries(compartmentRenames).map(([oldName, newName]) => [
newName,
oldName,
]),
),
};
};

Expand Down
11 changes: 11 additions & 0 deletions packages/compartment-mapper/src/types/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,19 @@ type LinkingOptions = ParserForLanguageOption &
* The result of `captureFromMap`.
*/
export type CaptureResult = {
/**
* Normalized `CompartmentMapDescriptor`
*/
captureCompartmentMap: CompartmentMapDescriptor;
/**
* Sources found in the `CompartmentMapDescriptor`
*/
captureSources: Sources;

/**
* A record of renamed {@link CompartmentDescriptor CompartmentDescriptors}
* from _new_ to _original_ name
*/
compartmentRenames: Record<string, string>;
};

Expand Down
13 changes: 3 additions & 10 deletions packages/compartment-mapper/test/capture-lite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { mapNodeModules } from '../src/node-modules.js';
import { makeReadPowers } from '../src/node-powers.js';
import { defaultParserForLanguage } from '../src/import-parsers.js';

const { keys, entries, fromEntries } = Object;
const { keys } = Object;

test('captureFromMap() should resolve with a CaptureResult', async t => {
t.plan(5);
Expand All @@ -26,18 +26,14 @@ test('captureFromMap() should resolve with a CaptureResult', async t => {
parserForLanguage: defaultParserForLanguage,
});

const renames = fromEntries(
entries(compartmentRenames).map(([filepath, id]) => [id, filepath]),
);

t.deepEqual(
keys(captureSources).sort(),
['bundle', 'bundle-dep-v0.0.0'],
'captureSources should contain sources for each compartment map descriptor',
);

t.deepEqual(
keys(renames).sort(),
keys(compartmentRenames).sort(),
['bundle', 'bundle-dep-v0.0.0'],
'compartmentRenames must contain same compartment names as in captureCompartmentMap',
);
Expand Down Expand Up @@ -82,14 +78,11 @@ test('captureFromMap() should round-trip sources based on parsers', async t => {
},
);

const renames = fromEntries(
entries(compartmentRenames).map(([filepath, id]) => [id, filepath]),
);
const decoder = new TextDecoder();
// the actual source depends on the value of `parserForLanguage` above
const actual = decoder.decode(captureSources.bundle['./icando.cjs'].bytes);
const expected = await fs.promises.readFile(
path.join(url.fileURLToPath(renames.bundle), 'icando.cjs'),
path.join(url.fileURLToPath(compartmentRenames.bundle), 'icando.cjs'),
'utf-8',
);
t.is(actual, expected, 'Source code should not be pre-compiled');
Expand Down
Loading