diff --git a/src/node-file-trace.ts b/src/node-file-trace.ts index 2623a83d..40c92f0a 100644 --- a/src/node-file-trace.ts +++ b/src/node-file-trace.ts @@ -232,7 +232,7 @@ export class Job { // keep backtracking for realpath, emitting folder symlinks within base if (!inPath(path, this.base)) return path; - return this.realpath(dirname(path), parent, seen) + sep + basename(path); + return join(this.realpath(dirname(path), parent, seen), basename(path)); } emitFile (path: string, reason: string, parent?: string, isRealpath = false) { diff --git a/test/unit.test.js b/test/unit.test.js index c97a15b5..76fa70b6 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -5,15 +5,20 @@ const { nodeFileTrace } = require('../out/node-file-trace'); global._unit = true; const skipOnWindows = ['yarn-workspaces', 'yarn-workspaces-base-root', 'yarn-workspace-esm', 'asset-symlink', 'require-symlink']; -const setBaseToRoot = ['yarn-workspaces-base-root']; +const unitTestDirs = fs.readdirSync(join(__dirname, 'unit')); +const unitTests = [ + ...unitTestDirs.map(testName => ({testName, isRoot: false})), + ...unitTestDirs.map(testName => ({testName, isRoot: true})), +]; -for (const unitTest of fs.readdirSync(join(__dirname, 'unit'))) { - if (process.platform === 'win32' && skipOnWindows.includes(unitTest)) { - console.log('skipping symlink test on Windows: ' + unitTest); +for (const { testName, isRoot } of unitTests) { + const testSuffix = `${testName} from ${isRoot ? 'root' : 'cwd'}`; + if (process.platform === 'win32' && (isRoot || skipOnWindows.includes(testName))) { + console.log(`Skipping unit test on Windows: ${testSuffix}`); continue; - } - it(`should correctly trace ${unitTest}`, async () => { - const unitPath = join(__dirname, 'unit', unitTest); + }; + it(`should correctly trace ${testSuffix}`, async () => { + const unitPath = join(__dirname, 'unit', testName); // We mock readFile because when node-file-trace is integrated into @now/node // this is the hook that triggers TypeScript compilation. So if this doesn't @@ -25,26 +30,25 @@ for (const unitTest of fs.readdirSync(join(__dirname, 'unit'))) { let inputFileName = "input.js"; - if (unitTest === "tsx-input") { + if (testName === "tsx-input") { inputFileName = "input.tsx"; } - const isRoot = setBaseToRoot.includes(unitTest); - const { fileList, reasons } = await nodeFileTrace([join(unitPath, inputFileName)], { base: isRoot ? '/' : `${__dirname}/../`, processCwd: unitPath, paths: { - dep: 'test/unit/esm-paths/esm-dep.js', - 'dep/': 'test/unit/esm-paths-trailer/' + dep: `${__dirname}/../test/unit/esm-paths/esm-dep.js`, + 'dep/': `${__dirname}/../test/unit/esm-paths-trailer/` }, - exportsOnly: unitTest.startsWith('exports-only'), + exportsOnly: testName.startsWith('exports-only'), ts: true, log: true, // disable analysis for basic-analysis unit tests - analysis: !unitTest.startsWith('basic-analysis'), + analysis: !testName.startsWith('basic-analysis'), mixedModules: true, - ignore: '**/actual.js', + // Ignore unit test output "actual.js", and ignore GitHub Actions preinstalled packages + ignore: (str) => str.endsWith('/actual.js') || str.startsWith('usr/local'), readFile: readFileMock }); let expected; @@ -73,7 +77,7 @@ for (const unitTest of fs.readdirSync(join(__dirname, 'unit'))) { throw e; } - if (unitTest === "tsx-input") { + if (testName === "tsx-input") { expect(readFileMock.mock.calls.length).toBe(2); } }); diff --git a/test/unit/yarn-workspaces-base-root/.gitignore b/test/unit/yarn-workspaces-base-root/.gitignore deleted file mode 100644 index 864b66cd..00000000 --- a/test/unit/yarn-workspaces-base-root/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# include node_modules to test the symlinks -!node_modules diff --git a/test/unit/yarn-workspaces-base-root/input.js b/test/unit/yarn-workspaces-base-root/input.js deleted file mode 100644 index d757ce0b..00000000 --- a/test/unit/yarn-workspaces-base-root/input.js +++ /dev/null @@ -1 +0,0 @@ -require('x'); diff --git a/test/unit/yarn-workspaces-base-root/node_modules/x b/test/unit/yarn-workspaces-base-root/node_modules/x deleted file mode 120000 index 8bace9bb..00000000 --- a/test/unit/yarn-workspaces-base-root/node_modules/x +++ /dev/null @@ -1 +0,0 @@ -../packages/x \ No newline at end of file diff --git a/test/unit/yarn-workspaces-base-root/output.js b/test/unit/yarn-workspaces-base-root/output.js deleted file mode 100644 index 7864f4c5..00000000 --- a/test/unit/yarn-workspaces-base-root/output.js +++ /dev/null @@ -1,8 +0,0 @@ -[ - "package.json", - "test/unit/yarn-workspaces-base-root/input.js", - "test/unit/yarn-workspaces-base-root/node_modules/x", - "test/unit/yarn-workspaces-base-root/packages/x/dep.js", - "test/unit/yarn-workspaces-base-root/packages/x/main.ts", - "test/unit/yarn-workspaces-base-root/packages/x/package.json" -] \ No newline at end of file diff --git a/test/unit/yarn-workspaces-base-root/packages/x/dep.js b/test/unit/yarn-workspaces-base-root/packages/x/dep.js deleted file mode 100644 index 888cae37..00000000 --- a/test/unit/yarn-workspaces-base-root/packages/x/dep.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = 42; diff --git a/test/unit/yarn-workspaces-base-root/packages/x/main.ts b/test/unit/yarn-workspaces-base-root/packages/x/main.ts deleted file mode 100644 index 3e5530eb..00000000 --- a/test/unit/yarn-workspaces-base-root/packages/x/main.ts +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dep.js'); diff --git a/test/unit/yarn-workspaces-base-root/packages/x/package.json b/test/unit/yarn-workspaces-base-root/packages/x/package.json deleted file mode 100644 index 528005c9..00000000 --- a/test/unit/yarn-workspaces-base-root/packages/x/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "main": "./main" -}