-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from line/test/add-test-case-for-parsing-arg
test: add test case for parsing args correctly
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const a = 'a'; | ||
|
||
export const a2 = 'a2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { a } from '@/a.js'; | ||
|
||
console.log(a); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
// this can be anything, it's just specified to check if we're reading the tsconfig correctly | ||
"@/*": ["./src/*"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"include": ["src/main.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
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 <file> 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); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |