From 63d9f3582991aad3231ae6140510b0cdc48e6ddb Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Sep 2020 09:40:54 -0400 Subject: [PATCH 1/6] Run all unit tests against root --- test/unit.test.js | 28 ++++++++++--------- .../unit/yarn-workspaces-base-root/.gitignore | 2 -- test/unit/yarn-workspaces-base-root/input.js | 1 - .../yarn-workspaces-base-root/node_modules/x | 1 - test/unit/yarn-workspaces-base-root/output.js | 8 ------ .../packages/x/dep.js | 1 - .../packages/x/main.ts | 1 - .../packages/x/package.json | 3 -- 8 files changed, 15 insertions(+), 30 deletions(-) delete mode 100644 test/unit/yarn-workspaces-base-root/.gitignore delete mode 100644 test/unit/yarn-workspaces-base-root/input.js delete mode 120000 test/unit/yarn-workspaces-base-root/node_modules/x delete mode 100644 test/unit/yarn-workspaces-base-root/output.js delete mode 100644 test/unit/yarn-workspaces-base-root/packages/x/dep.js delete mode 100644 test/unit/yarn-workspaces-base-root/packages/x/main.ts delete mode 100644 test/unit/yarn-workspaces-base-root/packages/x/package.json diff --git a/test/unit.test.js b/test/unit.test.js index c97a15b5..fd3d77b3 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -5,15 +5,19 @@ 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) { + if (process.platform === 'win32' && skipOnWindows.includes(testName)) { + console.log('skipping symlink test on Windows: ' + testName); continue; - } - it(`should correctly trace ${unitTest}`, async () => { - const unitPath = join(__dirname, 'unit', unitTest); + }; + it(`should correctly trace ${testName} from ${isRoot ? 'root' : 'cwd'}`, 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,12 +29,10 @@ 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, @@ -38,11 +40,11 @@ for (const unitTest of fs.readdirSync(join(__dirname, 'unit'))) { dep: 'test/unit/esm-paths/esm-dep.js', 'dep/': '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', readFile: readFileMock @@ -73,7 +75,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" -} From fbfc1639177f6ac288d18558652091734994550d Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Sep 2020 10:07:25 -0400 Subject: [PATCH 2/6] Fix tests on windows --- test/unit.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/unit.test.js b/test/unit.test.js index fd3d77b3..660110f0 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -12,11 +12,12 @@ const unitTests = [ ]; for (const { testName, isRoot } of unitTests) { - if (process.platform === 'win32' && skipOnWindows.includes(testName)) { - console.log('skipping symlink test on Windows: ' + testName); + 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 ${testName} from ${isRoot ? 'root' : 'cwd'}`, async () => { + 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 From 4b240421f83355aee1aea4b8a14a4d14ab1206ed Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Sep 2020 10:07:38 -0400 Subject: [PATCH 3/6] Revert inPath --- src/node-file-trace.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/node-file-trace.ts b/src/node-file-trace.ts index 2623a83d..717aad73 100644 --- a/src/node-file-trace.ts +++ b/src/node-file-trace.ts @@ -5,14 +5,12 @@ import analyze, { AnalyzeResult } from './analyze'; import resolveDependency from './resolve-dependency'; import { isMatch } from 'micromatch'; import { sharedLibEmit } from './utils/sharedlib-emit'; -import { join } from 'path'; const { gracefulify } = require('graceful-fs'); gracefulify(fs); function inPath (path: string, parent: string) { - const pathWithSep = join(parent, sep); - return path.startsWith(pathWithSep) && path !== pathWithSep; + return path.startsWith(parent) && path[parent.length] === sep && path.length > parent.length; } export async function nodeFileTrace(files: string[], opts: NodeFileTraceOptions = {}): Promise { From a6365f01e573305df43e22fdfd4097c03a266520 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Sep 2020 10:38:19 -0400 Subject: [PATCH 4/6] Fix double slash --- src/node-file-trace.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/node-file-trace.ts b/src/node-file-trace.ts index 717aad73..40c92f0a 100644 --- a/src/node-file-trace.ts +++ b/src/node-file-trace.ts @@ -5,12 +5,14 @@ import analyze, { AnalyzeResult } from './analyze'; import resolveDependency from './resolve-dependency'; import { isMatch } from 'micromatch'; import { sharedLibEmit } from './utils/sharedlib-emit'; +import { join } from 'path'; const { gracefulify } = require('graceful-fs'); gracefulify(fs); function inPath (path: string, parent: string) { - return path.startsWith(parent) && path[parent.length] === sep && path.length > parent.length; + const pathWithSep = join(parent, sep); + return path.startsWith(pathWithSep) && path !== pathWithSep; } export async function nodeFileTrace(files: string[], opts: NodeFileTraceOptions = {}): Promise { @@ -230,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) { From 5b2c55b734fa27e53f565c672ff8f65a76d7d4df Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Sep 2020 11:17:51 -0400 Subject: [PATCH 5/6] Fix `paths` --- test/unit.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit.test.js b/test/unit.test.js index 660110f0..f3a0586c 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -38,8 +38,8 @@ for (const { testName, isRoot } of unitTests) { 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: testName.startsWith('exports-only'), ts: true, From 6b9eb99e2d3de5feadcb7f62d3c9636beff829a3 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 4 Sep 2020 11:46:52 -0400 Subject: [PATCH 6/6] Add ignore func --- test/unit.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit.test.js b/test/unit.test.js index f3a0586c..76fa70b6 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -47,7 +47,8 @@ for (const { testName, isRoot } of unitTests) { // disable analysis for basic-analysis unit tests 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;