From 2607017f421ee91009a60d29c3e08eac65718b31 Mon Sep 17 00:00:00 2001 From: sverweij Date: Sun, 20 Nov 2022 20:25:42 +0100 Subject: [PATCH] bugfix(cli): adds using the baseDir when checking for file existence --- src/cli/index.js | 14 +++++-- .../alternate-basedir/expected.json | 41 +++++++++++++++++++ .../alternate-basedir/src/index.ts | 0 .../expected.dot} | 0 test/cli/index.spec.mjs | 20 ++++++++- 5 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 test/cli/__fixtures__/alternate-basedir/expected.json create mode 100644 test/cli/__fixtures__/alternate-basedir/src/index.ts rename test/cli/__fixtures__/{duplicate-subs.dot => duplicate-subs/expected.dot} (100%) diff --git a/src/cli/index.js b/src/cli/index.js index 44d734aaf..c67887aa3 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -1,3 +1,4 @@ +const path = require("path"); const glob = require("glob"); const clone = require("lodash/clone"); const set = require("lodash/set"); @@ -89,14 +90,19 @@ function setUpListener(pCruiseOptions) { } function runCruise(pFileDirectoryArray, pCruiseOptions) { - pFileDirectoryArray - .filter((pFileOrDirectory) => !glob.hasMagic(pFileOrDirectory)) - .forEach(validateFileExistence); - const lCruiseOptions = addKnownViolations( normalizeCliOptions(pCruiseOptions) ); + pFileDirectoryArray + .filter((pFileOrDirectory) => !glob.hasMagic(pFileOrDirectory)) + .map((pFileOrDirectory) => + lCruiseOptions?.ruleSet?.options?.baseDir + ? path.join(lCruiseOptions.ruleSet.options.baseDir, pFileOrDirectory) + : pFileOrDirectory + ) + .forEach(validateFileExistence); + setUpListener(lCruiseOptions); bus.emit("start"); diff --git a/test/cli/__fixtures__/alternate-basedir/expected.json b/test/cli/__fixtures__/alternate-basedir/expected.json new file mode 100644 index 000000000..bc103fb89 --- /dev/null +++ b/test/cli/__fixtures__/alternate-basedir/expected.json @@ -0,0 +1,41 @@ +{ + "modules": [ + { + "source": "src/index.ts", + "dependencies": [], + "dependents": [], + "orphan": true, + "valid": true + } + ], + "summary": { + "violations": [], + "error": 0, + "warn": 0, + "info": 0, + "ignore": 0, + "totalCruised": 1, + "totalDependenciesCruised": 0, + "optionsUsed": { + "combinedDependencies": false, + "doNotFollow": { + "path": "node_modules" + }, + "exoticRequireStrings": [], + "externalModuleResolutionStrategy": "node_modules", + "metrics": false, + "moduleSystems": [ + "es6", + "cjs", + "tsd", + "amd" + ], + "outputTo": "test/cli/__output__/alternate-basedir.json", + "outputType": "json", + "preserveSymlinks": false, + "tsPreCompilationDeps": false, + "args": "src" + }, + "ruleSetUsed": {} + } +} \ No newline at end of file diff --git a/test/cli/__fixtures__/alternate-basedir/src/index.ts b/test/cli/__fixtures__/alternate-basedir/src/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/test/cli/__fixtures__/duplicate-subs.dot b/test/cli/__fixtures__/duplicate-subs/expected.dot similarity index 100% rename from test/cli/__fixtures__/duplicate-subs.dot rename to test/cli/__fixtures__/duplicate-subs/expected.dot diff --git a/test/cli/index.spec.mjs b/test/cli/index.spec.mjs index 49cf9ef88..5eee97cce 100644 --- a/test/cli/index.spec.mjs +++ b/test/cli/index.spec.mjs @@ -12,7 +12,7 @@ import deleteDammit from "./delete-dammit.utl.cjs"; const OUT_DIR = "./test/cli/__output__"; const FIX_DIR = "./test/cli/__fixtures__"; -/* eslint max-len:0*/ +/* eslint max-len:0 */ const TEST_PAIRS = [ { description: @@ -91,7 +91,7 @@ const TEST_PAIRS = [ outputType: "dot", exclude: "node_modules", }, - expect: "duplicate-subs.dot", + expect: "duplicate-subs/expected.dot", cleanup: true, }, { @@ -106,6 +106,22 @@ const TEST_PAIRS = [ expect: "{{moduleType}}.dir.filtered.csv", cleanup: true, }, + { + description: "alternate basedir", + dirOrFile: "src", + options: { + outputTo: path.join(OUT_DIR, "alternate-basedir.json"), + outputType: "json", + doNotFollow: "node_modules", + ruleSet: { + options: { + baseDir: "test/cli/__fixtures__/alternate-basedir", + }, + }, + }, + expect: "alternate-basedir/expected.json", + cleanup: true, + }, ]; function resetOutputDirectory() {