diff --git a/fixtures/cli-preprocessor/index.js b/fixtures/cli-preprocessor/index.js new file mode 100644 index 000000000..85e2454e5 --- /dev/null +++ b/fixtures/cli-preprocessor/index.js @@ -0,0 +1,4 @@ +export default function (options) { + console.log('hi from js preprocessor'); + return options; +} diff --git a/fixtures/cli-preprocessor/index.ts b/fixtures/cli-preprocessor/index.ts new file mode 100644 index 000000000..73487f28a --- /dev/null +++ b/fixtures/cli-preprocessor/index.ts @@ -0,0 +1,4 @@ +export default function (options) { + console.log('hi from ts preprocessor'); + return options; +} diff --git a/fixtures/cli-preprocessor/node_modules/@org/preprocessor/index.js b/fixtures/cli-preprocessor/node_modules/@org/preprocessor/index.js new file mode 100644 index 000000000..2de00cde0 --- /dev/null +++ b/fixtures/cli-preprocessor/node_modules/@org/preprocessor/index.js @@ -0,0 +1,4 @@ +export default function (options) { + console.log('hi from scoped preprocessor'); + return options; +} diff --git a/fixtures/cli-preprocessor/node_modules/@org/preprocessor/package.json b/fixtures/cli-preprocessor/node_modules/@org/preprocessor/package.json new file mode 100644 index 000000000..c98c1fffa --- /dev/null +++ b/fixtures/cli-preprocessor/node_modules/@org/preprocessor/package.json @@ -0,0 +1,4 @@ +{ + "name": "@org/preprocessor", + "type": "module" +} diff --git a/fixtures/cli-preprocessor/node_modules/knip-preprocessor/index.js b/fixtures/cli-preprocessor/node_modules/knip-preprocessor/index.js new file mode 100644 index 000000000..7d49738be --- /dev/null +++ b/fixtures/cli-preprocessor/node_modules/knip-preprocessor/index.js @@ -0,0 +1,4 @@ +module.exports = function (options) { + console.log('hi from pkg preprocessor'); + return options; +}; diff --git a/fixtures/cli-preprocessor/node_modules/knip-preprocessor/package.json b/fixtures/cli-preprocessor/node_modules/knip-preprocessor/package.json new file mode 100644 index 000000000..fcb515122 --- /dev/null +++ b/fixtures/cli-preprocessor/node_modules/knip-preprocessor/package.json @@ -0,0 +1,3 @@ +{ + "name": "knip-preprocessor" +} diff --git a/fixtures/cli-preprocessor/package.json b/fixtures/cli-preprocessor/package.json new file mode 100644 index 000000000..3a72b1354 --- /dev/null +++ b/fixtures/cli-preprocessor/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/cli-preprocessor" +} diff --git a/fixtures/cli-reporter/index.js b/fixtures/cli-reporter/index.js new file mode 100644 index 000000000..c6192d180 --- /dev/null +++ b/fixtures/cli-reporter/index.js @@ -0,0 +1,3 @@ +export default function () { + console.log('hi from js reporter'); +} diff --git a/fixtures/cli-reporter/index.ts b/fixtures/cli-reporter/index.ts new file mode 100644 index 000000000..4d4a00ba4 --- /dev/null +++ b/fixtures/cli-reporter/index.ts @@ -0,0 +1,3 @@ +export default function () { + console.log('hi from ts reporter'); +} diff --git a/fixtures/cli-reporter/node_modules/@org/reporter/index.js b/fixtures/cli-reporter/node_modules/@org/reporter/index.js new file mode 100644 index 000000000..3ba3bb40f --- /dev/null +++ b/fixtures/cli-reporter/node_modules/@org/reporter/index.js @@ -0,0 +1,3 @@ +export default function () { + console.log('hi from scoped reporter'); +} diff --git a/fixtures/cli-reporter/node_modules/@org/reporter/package.json b/fixtures/cli-reporter/node_modules/@org/reporter/package.json new file mode 100644 index 000000000..870b26ae5 --- /dev/null +++ b/fixtures/cli-reporter/node_modules/@org/reporter/package.json @@ -0,0 +1,4 @@ +{ + "name": "@org/reporter", + "type": "module" +} diff --git a/fixtures/cli-reporter/node_modules/knip-reporter/index.js b/fixtures/cli-reporter/node_modules/knip-reporter/index.js new file mode 100644 index 000000000..dd676cdab --- /dev/null +++ b/fixtures/cli-reporter/node_modules/knip-reporter/index.js @@ -0,0 +1,3 @@ +module.exports = function () { + console.log('hi from pkg reporter'); +}; diff --git a/fixtures/cli-reporter/node_modules/knip-reporter/package.json b/fixtures/cli-reporter/node_modules/knip-reporter/package.json new file mode 100644 index 000000000..3f701e222 --- /dev/null +++ b/fixtures/cli-reporter/node_modules/knip-reporter/package.json @@ -0,0 +1,3 @@ +{ + "name": "knip-reporter" +} diff --git a/fixtures/cli-reporter/package.json b/fixtures/cli-reporter/package.json new file mode 100644 index 000000000..7eab78373 --- /dev/null +++ b/fixtures/cli-reporter/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/cli-reporter" +} diff --git a/fixtures/cli/package.json b/fixtures/cli/package.json new file mode 100644 index 000000000..1a0994fc4 --- /dev/null +++ b/fixtures/cli/package.json @@ -0,0 +1,3 @@ +{ + "name": "@fixtures/cli" +} diff --git a/tests/cli-preprocessor.test.ts b/tests/cli-preprocessor.test.ts new file mode 100644 index 000000000..11d5ca76f --- /dev/null +++ b/tests/cli-preprocessor.test.ts @@ -0,0 +1,27 @@ +import assert from 'node:assert/strict'; +import { execSync } from 'node:child_process'; +import test from 'node:test'; +import { resolve } from '../src/util/path.js'; + +const cwd = resolve('fixtures/cli-preprocessor'); + +const exec = (command: string) => { + const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd }); + return output.toString().trim(); +}; + +test('knip --preprocessor ./index.js', () => { + assert.equal(exec('knip --preprocessor ./index.js'), 'hi from js preprocessor'); +}); + +test('knip --preprocessor ./index.ts', () => { + assert.equal(exec('knip --preprocessor ./index.ts'), 'hi from ts preprocessor'); +}); + +test('knip --preprocessor knip-preprocessor', () => { + assert.equal(exec('knip --preprocessor knip-preprocessor'), 'hi from pkg preprocessor'); +}); + +test('knip --preprocessor @org/preprocessor', () => { + assert.equal(exec('knip --preprocessor @org/preprocessor'), 'hi from scoped preprocessor'); +}); diff --git a/tests/cli-reporter.test.ts b/tests/cli-reporter.test.ts new file mode 100644 index 000000000..47351e11d --- /dev/null +++ b/tests/cli-reporter.test.ts @@ -0,0 +1,27 @@ +import assert from 'node:assert/strict'; +import { execSync } from 'node:child_process'; +import test from 'node:test'; +import { resolve } from '../src/util/path.js'; + +const cwd = resolve('fixtures/cli-reporter'); + +const exec = (command: string) => { + const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd }); + return output.toString().trim(); +}; + +test('knip --reporter ./index.js', () => { + assert.equal(exec('knip --reporter ./index.js'), 'hi from js reporter'); +}); + +test('knip --reporter ./index.ts', () => { + assert.equal(exec('knip --reporter ./index.ts'), 'hi from ts reporter'); +}); + +test('knip --reporter knip-reporter', () => { + assert.equal(exec('knip --reporter knip-reporter'), 'hi from pkg reporter'); +}); + +test('knip --reporter @org/reporter', () => { + assert.equal(exec('knip --reporter @org/reporter'), 'hi from scoped reporter'); +}); diff --git a/tests/cli.test.ts b/tests/cli.test.ts index da18a615f..49b19f89f 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -2,10 +2,13 @@ import assert from 'node:assert/strict'; import { execSync } from 'node:child_process'; import test from 'node:test'; import { helpText } from '../src/util/cli-arguments.js'; +import { resolve } from '../src/util/path.js'; import { version } from '../src/version.js'; +const cwd = resolve('fixtures/cli'); + const exec = (command: string) => { - const output = execSync(command.replace(/^knip/, 'node --no-warnings --loader tsx src/cli.ts')); + const output = execSync(command.replace(/^knip/, 'node ../../dist/cli.js'), { cwd }); return output.toString().trim(); }; @@ -16,7 +19,3 @@ test('knip --version', () => { test('knip --help', () => { assert.equal(exec('knip --help'), helpText); }); - -test('knip', { skip: true }, () => { - assert.equal(exec('knip'), '✂️ Excellent, Knip found no issues.'); -});