From f1baf97ac577b909c2cc7629f9a7478d355659fa Mon Sep 17 00:00:00 2001 From: Kazushi Konosu Date: Mon, 16 Dec 2024 20:52:42 +0900 Subject: [PATCH 1/2] test: add test case for parsing arg --- test/fixtures/parse_args/src/a.ts | 3 + test/fixtures/parse_args/src/main.ts | 3 + test/fixtures/parse_args/tsconfig.json | 8 +++ test/parse_args.test.ts | 76 ++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 test/fixtures/parse_args/src/a.ts create mode 100644 test/fixtures/parse_args/src/main.ts create mode 100644 test/fixtures/parse_args/tsconfig.json create mode 100644 test/parse_args.test.ts diff --git a/test/fixtures/parse_args/src/a.ts b/test/fixtures/parse_args/src/a.ts new file mode 100644 index 0000000..69fa37c --- /dev/null +++ b/test/fixtures/parse_args/src/a.ts @@ -0,0 +1,3 @@ +export const a = 'a'; + +export const a2 = 'a2'; diff --git a/test/fixtures/parse_args/src/main.ts b/test/fixtures/parse_args/src/main.ts new file mode 100644 index 0000000..47935fa --- /dev/null +++ b/test/fixtures/parse_args/src/main.ts @@ -0,0 +1,3 @@ +import { a } from '@/a.js'; + +console.log(a); diff --git a/test/fixtures/parse_args/tsconfig.json b/test/fixtures/parse_args/tsconfig.json new file mode 100644 index 0000000..7f4c432 --- /dev/null +++ b/test/fixtures/parse_args/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "paths": { + // this can be anything, it's just specified to check if we're reading the tsconfig correctly + "@/*": ["./src/*"] + } + } +} diff --git a/test/parse_args.test.ts b/test/parse_args.test.ts new file mode 100644 index 0000000..d6be822 --- /dev/null +++ b/test/parse_args.test.ts @@ -0,0 +1,76 @@ +import assert from 'node:assert/strict'; +import * as child_process from 'node:child_process'; +import { promisify } from 'node:util'; +import { dirname, resolve } from 'node:path'; +import { describe, it } from 'node:test'; +import { fileURLToPath } from 'node:url'; +import stripAnsi from 'strip-ansi'; + +const exec = promisify(child_process.exec); + +const projectRoot = resolve( + dirname(fileURLToPath(import.meta.url)), + 'fixtures/parse_args', +); + +const bin = resolve(dirname(fileURLToPath(import.meta.url)), '../dist/cli.js'); + +await exec('npm run build', { cwd: projectRoot }); + +describe('parse_args', () => { + it('should show help', async () => { + const { stdout } = await exec(`node ${bin} --help`); + + const output = stripAnsi(stdout); + + assert.equal( + output, + ` +Usage: + tsr [options] [...entrypoints] + +Options: + -p, --project Path to your tsconfig.json + -w, --write Write changes in place + -r, --recursive Recursively look into files until the project is clean + --include-d-ts Check for unused code in .d.ts files + -h, --help Display this message + -v, --version Display version number + +Examples: + # Check unused code for a project with an entrypoint of src/main.ts + tsr 'src/main\\.ts$' + + # Write changes in place + tsr --write 'src/main\\.ts$' + + # Check unused code for a project with a custom tsconfig.json + tsr --project tsconfig.app.json 'src/main\\.ts$' + + # Check unused code for a project with multiple entrypoints in src/pages + tsr 'src/pages/.*\\.ts$' + +`, + ); + }); + + it('should use default tsconfig.json', async () => { + const { stdout, code } = await exec(`node ${bin} 'src/main\\.ts$'`, { + cwd: projectRoot, + }) + .then((res) => ({ ...res, code: 0 })) + .catch((e) => e as { stdout: string; code: number }); + + const output = stripAnsi(stdout.toString()); + + assert.equal( + output, + `tsconfig tsconfig.json +Project has 2 files. Found 1 entrypoint file +export src/a.ts:2:0 'a2' +✖ remove 1 export +`, + ); + assert.equal(code, 1); + }); +}); From b917842b5870cb1565c25caf4490948dac405faf Mon Sep 17 00:00:00 2001 From: Kazushi Konosu Date: Mon, 16 Dec 2024 20:57:03 +0900 Subject: [PATCH 2/2] test: add test case for custom tsconfig --- test/fixtures/parse_args/tsconfig.main.json | 3 +++ test/parse_args.test.ts | 22 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 test/fixtures/parse_args/tsconfig.main.json diff --git a/test/fixtures/parse_args/tsconfig.main.json b/test/fixtures/parse_args/tsconfig.main.json new file mode 100644 index 0000000..32ad926 --- /dev/null +++ b/test/fixtures/parse_args/tsconfig.main.json @@ -0,0 +1,3 @@ +{ + "include": ["src/main.ts"] +} diff --git a/test/parse_args.test.ts b/test/parse_args.test.ts index d6be822..3a8819a 100644 --- a/test/parse_args.test.ts +++ b/test/parse_args.test.ts @@ -73,4 +73,26 @@ export src/a.ts:2:0 'a2' ); assert.equal(code, 1); }); + + it('should work with custom tsconfig.json', async () => { + const { stdout, code } = await exec( + `node ${bin} --project tsconfig.main.json 'src/main\\.ts$'`, + { + cwd: projectRoot, + }, + ) + .then((res) => ({ ...res, code: 0 })) + .catch((e) => e as { stdout: string; code: number }); + + const output = stripAnsi(stdout.toString()); + + assert.equal( + output, + `tsconfig tsconfig.main.json +Project has 1 file. Found 1 entrypoint file +✔ all good! +`, + ); + assert.equal(code, 0); + }); });