Skip to content

Commit ab66e07

Browse files
committed
add tests
1 parent 6133dd9 commit ab66e07

File tree

13 files changed

+106
-5
lines changed

13 files changed

+106
-5
lines changed

test/unit.test.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { nodeFileTrace } = require('../out/node-file-trace');
44

55
global._unit = true;
66

7-
const skipOnWindows = ['yarn-workspaces', 'yarn-workspaces-base-root', 'yarn-workspace-esm', 'asset-symlink', 'require-symlink'];
7+
const skipOnWindows = ['yarn-workspaces', 'yarn-workspaces-base-root', 'yarn-workspace-esm', 'asset-symlink', 'require-symlink', "cjs-querystring"];
88
const unitTestDirs = fs.readdirSync(join(__dirname, 'unit'));
99
const unitTests = [
1010
...unitTestDirs.map(testName => ({testName, isRoot: false})),
@@ -13,11 +13,26 @@ const unitTests = [
1313

1414
for (const { testName, isRoot } of unitTests) {
1515
const testSuffix = `${testName} from ${isRoot ? 'root' : 'cwd'}`;
16-
if (process.platform === 'win32' && (isRoot || skipOnWindows.includes(testName))) {
17-
console.log(`Skipping unit test on Windows: ${testSuffix}`);
18-
continue;
19-
};
2016
const unitPath = join(__dirname, 'unit', testName);
17+
18+
if (process.platform === 'win32') {
19+
if (isRoot || skipOnWindows.includes(testName)) {
20+
console.log(`Skipping unit test on Windows: ${testSuffix}`);
21+
continue;
22+
}
23+
}
24+
else {
25+
// The point of this test is to see how filenames with question marks in
26+
// them are handled, but that's disallowed in Windows, so much so that you
27+
// can't even check out the commit for CI purposes if the `?`s are
28+
// permanently in the filename. So we store the file without them and add
29+
// them in just for the test on non-Windows platforms.
30+
if (testName === 'cjs-querystring') {
31+
const currentFilepath = join(unitPath, "whowhatidk.js")
32+
const newFilepath = currentFilepath.replace("whowhatidk.js", "who?what?idk!.js")
33+
fs.renameSync(currentFilepath, newFilepath)
34+
}
35+
}
2136

2237
it(`should correctly trace ${testSuffix}`, async () => {
2338

@@ -41,6 +56,25 @@ for (const { testName, isRoot } of unitTests) {
4156
return null
4257
}
4358
}
59+
60+
// mock an in-memory module store (such as webpack's) where the same filename with
61+
// two different querystrings can correspond to two different modules, one importing
62+
// the other
63+
if (testName === "querystring-self-import") {
64+
if (id.endsWith("input.js") || id.endsWith("base.js") || id.endsWith("dep.js")) {
65+
return fs.readFileSync(id).toString()
66+
}
67+
68+
if (id.endsWith("base.js?__withQuery")) {
69+
return `
70+
import * as origBase from './base';
71+
export const dogs = origBase.dogs.concat('Cory', 'Bodhi');
72+
export const cats = origBase.cats.concat('Teaberry', 'Sassafras', 'Persephone');
73+
export const rats = origBase.rats;
74+
`;
75+
}
76+
}
77+
4478
return this.constructor.prototype.readFile.apply(this, arguments);
4579
});
4680

test/unit/cjs-querystring/input.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test that CJS files treat question marks in filenames as any other character,
2+
// matching Node behavior
3+
4+
// https://www.youtube.com/watch?v=2ve20PVNZ18
5+
6+
const baseball = require("./who?what?idk!");
7+
console.log(baseball.players);

test/unit/cjs-querystring/output.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"package.json",
3+
"test/unit/cjs-querystring/input.js",
4+
"test/unit/cjs-querystring/who?what?idk!.js"
5+
]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
players: {
3+
first: "Who",
4+
second: "What",
5+
third: "I Don't Know",
6+
left: "Why",
7+
center: "Because",
8+
pitcher: "Tomorrow",
9+
catcher: "Today",
10+
shortstop: "I Don't Give a Damn!",
11+
},
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { numSpecies } from "./bear?beaver?bison";
2+
console.log(`There are ${numSpecies} species of bears.`);
3+
4+
export const food = "termites";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as cheetah from "./cheetah?cow=chipmunk";
2+
console.log(`Cheetahs can run ${cheetah.topSpeed} mph.`);
3+
4+
export const numSpecies = 8;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const topSpeed = 65;

test/unit/esm-querystring/input.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Test that querystrings of various forms get stripped from esm imports
2+
3+
import * as aardvark from "./animalFacts/aardvark?anteater";
4+
5+
console.log(`Aardvarks eat ${aardvark.food}.`);

test/unit/esm-querystring/output.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
"package.json",
3+
"test/unit/esm-querystring/animalFacts/aardvark.js",
4+
"test/unit/esm-querystring/animalFacts/bear.js",
5+
"test/unit/esm-querystring/animalFacts/cheetah.js",
6+
"test/unit/esm-querystring/input.js"
7+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as dep from "./dep";
2+
3+
export const dogs = ["Charlie", "Maisey"];
4+
export const cats = ["Piper"];
5+
export const rats = dep.rats;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const rats = ["Debra"];
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that if a file and the same file with a querystring correspond to different
2+
// modules in memory, one can successfully import the other. The import chain
3+
// goes `input` (this file) -> `base?__withQuery` -> `base` -> `dep`, which means
4+
// that if `dep` shows up in `output`, we know that both `base?__withQuery` and
5+
// `base` have been loaded successfully.
6+
7+
import * as baseWithQuery from "./base?__withQuery";
8+
console.log("Dogs:", baseWithQuery.dogs);
9+
console.log("Cats:", baseWithQuery.cats);
10+
console.log("Rats:", baseWithQuery.rats);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
"package.json",
3+
"test/unit/querystring-self-import/base.js",
4+
"test/unit/querystring-self-import/dep.js",
5+
"test/unit/querystring-self-import/input.js"
6+
]

0 commit comments

Comments
 (0)