-
-
Notifications
You must be signed in to change notification settings - Fork 55
fix(dts): clean correct tsbuildinfo file #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,101 +1,204 @@ | ||
| import { expect, test } from '@rstest/core'; | ||
| import { mergeAliasWithTsConfigPaths, prettyTime } from '../src/utils'; | ||
|
|
||
| test('should pretty time correctly', () => { | ||
| expect(prettyTime(0.0012)).toEqual('0.001 s'); | ||
| expect(prettyTime(0.0123)).toEqual('0.01 s'); | ||
| expect(prettyTime(0.1234)).toEqual('0.12 s'); | ||
| expect(prettyTime(1.234)).toEqual('1.23 s'); | ||
| expect(prettyTime(12.34)).toEqual('12.3 s'); | ||
| expect(prettyTime(120)).toEqual('2 m'); | ||
| expect(prettyTime(123.4)).toEqual('2 m 3.4 s'); | ||
| expect(prettyTime(1234)).toEqual('20 m 34 s'); | ||
| expect(prettyTime(1234.5)).toEqual('20 m 34.5 s'); | ||
| }); | ||
| // import { execSync } from 'node:child_process'; | ||
Timeless0911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import fs from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, test } from '@rstest/core'; | ||
| import { | ||
| cleanTsBuildInfoFile, | ||
| loadTsconfig, | ||
| mergeAliasWithTsConfigPaths, | ||
| prettyTime, | ||
| } from '../src/utils'; | ||
|
|
||
| test('mergeAliasWithTsConfigPaths should handle empty inputs', () => { | ||
| // Both undefined | ||
| expect( | ||
| mergeAliasWithTsConfigPaths(undefined, undefined), | ||
| ).toMatchInlineSnapshot({}, '{}'); | ||
|
|
||
| // Empty objects | ||
| expect(mergeAliasWithTsConfigPaths({}, {})).toMatchInlineSnapshot({}, '{}'); | ||
|
|
||
| // One empty, one undefined | ||
| expect(mergeAliasWithTsConfigPaths({}, undefined)).toMatchInlineSnapshot( | ||
| {}, | ||
| '{}', | ||
| ); | ||
| expect(mergeAliasWithTsConfigPaths(undefined, {})).toMatchInlineSnapshot( | ||
| {}, | ||
| '{}', | ||
| ); | ||
| describe('prettyTime', () => { | ||
| test('should pretty time correctly', () => { | ||
| expect(prettyTime(0.0012)).toEqual('0.001 s'); | ||
| expect(prettyTime(0.0123)).toEqual('0.01 s'); | ||
| expect(prettyTime(0.1234)).toEqual('0.12 s'); | ||
| expect(prettyTime(1.234)).toEqual('1.23 s'); | ||
| expect(prettyTime(12.34)).toEqual('12.3 s'); | ||
| expect(prettyTime(120)).toEqual('2 m'); | ||
| expect(prettyTime(123.4)).toEqual('2 m 3.4 s'); | ||
| expect(prettyTime(1234)).toEqual('20 m 34 s'); | ||
| expect(prettyTime(1234.5)).toEqual('20 m 34.5 s'); | ||
| }); | ||
| }); | ||
|
|
||
| test('mergeAliasWithTsConfigPaths should handle paths only', () => { | ||
| const paths = { | ||
| '@/*': ['./src/*'], | ||
| }; | ||
| describe('mergeAliasWithTsConfigPaths', () => { | ||
| test('mergeAliasWithTsConfigPaths should handle empty inputs', () => { | ||
| // Both undefined | ||
| expect( | ||
| mergeAliasWithTsConfigPaths(undefined, undefined), | ||
| ).toMatchInlineSnapshot({}, '{}'); | ||
|
|
||
| const result = mergeAliasWithTsConfigPaths(paths, undefined); | ||
| // Empty objects | ||
| expect(mergeAliasWithTsConfigPaths({}, {})).toMatchInlineSnapshot({}, '{}'); | ||
|
|
||
| expect(result).toMatchInlineSnapshot(` | ||
| { | ||
| "@/*": [ | ||
| "./src/*", | ||
| ], | ||
| } | ||
| `); | ||
| }); | ||
| // One empty, one undefined | ||
| expect(mergeAliasWithTsConfigPaths({}, undefined)).toMatchInlineSnapshot( | ||
| {}, | ||
| '{}', | ||
| ); | ||
| expect(mergeAliasWithTsConfigPaths(undefined, {})).toMatchInlineSnapshot( | ||
| {}, | ||
| '{}', | ||
| ); | ||
| }); | ||
|
|
||
| test('mergeAliasWithTsConfigPaths should handle alias only', () => { | ||
| const alias = { | ||
| '@': './src', | ||
| }; | ||
| test('mergeAliasWithTsConfigPaths should handle paths only', () => { | ||
| const paths = { | ||
| '@/*': ['./src/*'], | ||
| }; | ||
|
|
||
| const result = mergeAliasWithTsConfigPaths(undefined, alias); | ||
| const result = mergeAliasWithTsConfigPaths(paths, undefined); | ||
|
|
||
| expect(result).toMatchInlineSnapshot(` | ||
| { | ||
| "@": [ | ||
| "./src", | ||
| ], | ||
| } | ||
| `); | ||
| }); | ||
| expect(result).toMatchInlineSnapshot(` | ||
| { | ||
| "@/*": [ | ||
| "./src/*", | ||
| ], | ||
| } | ||
| `); | ||
| }); | ||
|
|
||
| test('mergeAliasWithTsConfigPaths should handle alias overriding paths', () => { | ||
| const paths = { | ||
| '@utils/*': ['./lib/utils/*'], | ||
| '@/*': ['./src/*'], | ||
| }; | ||
| test('mergeAliasWithTsConfigPaths should handle alias only', () => { | ||
| const alias = { | ||
| '@': './src', | ||
| }; | ||
|
|
||
| const alias = { | ||
| '@utils/*': './src/utils', | ||
| '@components': './src/components', | ||
| }; | ||
| const result = mergeAliasWithTsConfigPaths(undefined, alias); | ||
|
|
||
| const result = mergeAliasWithTsConfigPaths(paths, alias); | ||
|
|
||
| expect(Object.keys(result)).toMatchInlineSnapshot(` | ||
| [ | ||
| "@utils/*", | ||
| "@components", | ||
| "@/*", | ||
| ] | ||
| `); | ||
| expect(Object.values(result)).toMatchInlineSnapshot(` | ||
| [ | ||
| [ | ||
| "./src/utils", | ||
| ], | ||
| expect(result).toMatchInlineSnapshot(` | ||
| { | ||
| "@": [ | ||
| "./src", | ||
| ], | ||
| } | ||
| `); | ||
| }); | ||
|
|
||
| test('mergeAliasWithTsConfigPaths should handle alias overriding paths', () => { | ||
| const paths = { | ||
| '@utils/*': ['./lib/utils/*'], | ||
| '@/*': ['./src/*'], | ||
| }; | ||
|
|
||
| const alias = { | ||
| '@utils/*': './src/utils', | ||
| '@components': './src/components', | ||
| }; | ||
|
|
||
| const result = mergeAliasWithTsConfigPaths(paths, alias); | ||
|
|
||
| expect(Object.keys(result)).toMatchInlineSnapshot(` | ||
| [ | ||
| "./src/components", | ||
| ], | ||
| "@utils/*", | ||
| "@components", | ||
| "@/*", | ||
| ] | ||
| `); | ||
| expect(Object.values(result)).toMatchInlineSnapshot(` | ||
| [ | ||
| "./src/*", | ||
| ], | ||
| ] | ||
| `); | ||
| [ | ||
| "./src/utils", | ||
| ], | ||
| [ | ||
| "./src/components", | ||
| ], | ||
| [ | ||
| "./src/*", | ||
| ], | ||
| ] | ||
| `); | ||
| }); | ||
| }); | ||
|
|
||
| describe('cleanTsBuildInfoFile', () => { | ||
| const fileExists = async (filePath: string): Promise<boolean> => { | ||
| try { | ||
| await fs.access(filePath); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const testCases = [ | ||
| { | ||
| name: 'default config without custom tsBuildInfoFile', | ||
| config: { composite: true }, | ||
| expectedPath: 'tsconfig.case1.tsbuildinfo', | ||
| }, | ||
| { | ||
| name: 'custom tsBuildInfoFile path', | ||
| config: { | ||
| composite: true, | ||
| tsBuildInfoFile: 'node_modules/.cache/tsconfig.case2.tsbuildinfo', | ||
| }, | ||
| expectedPath: 'node_modules/.cache/tsconfig.case2.tsbuildinfo', | ||
| }, | ||
| { | ||
| name: 'outDir without rootDir', | ||
| config: { composite: true, outDir: 'dist3' }, | ||
| expectedPath: 'dist3/tsconfig.case3.tsbuildinfo', | ||
| }, | ||
| { | ||
| name: 'outDir with rootDir', | ||
| config: { composite: true, outDir: 'dist4', rootDir: 'src' }, | ||
| expectedPath: 'tsconfig.case4.tsbuildinfo', | ||
| }, | ||
| { | ||
| name: 'nested outDir with rootDir', | ||
| config: { composite: true, outDir: 'dist/5', rootDir: 'src' }, | ||
| expectedPath: 'dist/tsconfig.case5.tsbuildinfo', | ||
| }, | ||
| ]; | ||
|
|
||
| test('should clean tsBuildInfo files for various tsconfig configurations', async () => { | ||
| delete process.env.NO_COLOR; | ||
| const tempDir = await fs.mkdtemp(path.join(__dirname, 'temp-test-')); | ||
|
|
||
| const srcPath = path.join(tempDir, 'src/index.ts'); | ||
| await fs.mkdir(path.dirname(srcPath), { recursive: true }); | ||
| await fs.writeFile(srcPath, 'export {}'); | ||
|
|
||
| try { | ||
| await Promise.all( | ||
| testCases.map(async (testCase, index) => { | ||
| const caseNumber = index + 1; | ||
| const tsconfigPath = path.join( | ||
| tempDir, | ||
| `tsconfig.case${caseNumber}.json`, | ||
| ); | ||
| const expectedBuildInfoPath = path.join( | ||
| tempDir, | ||
| testCase.expectedPath, | ||
| ); | ||
|
|
||
| await fs.writeFile( | ||
| tsconfigPath, | ||
| JSON.stringify({ compilerOptions: testCase.config }, null, 2), | ||
| ); | ||
|
|
||
| // execSync(`npx tsc --p ${tsconfigPath}`, { cwd: tempDir }); | ||
| // tsc is too slow, so we directly create the file | ||
Timeless0911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await fs.mkdir(path.dirname(expectedBuildInfoPath), { | ||
| recursive: true, | ||
| }); | ||
| await fs.writeFile( | ||
| path.join(tempDir, testCase.expectedPath), | ||
| 'Generated tsBuildInfo content', | ||
| ); | ||
|
|
||
| expect(await fileExists(expectedBuildInfoPath)).toBe(true); | ||
|
|
||
| const tsconfig = loadTsconfig(tsconfigPath); | ||
| await cleanTsBuildInfoFile(tsconfigPath, tsconfig.options); | ||
|
|
||
| expect(await fileExists(expectedBuildInfoPath)).toBe(false); | ||
| }), | ||
| ); | ||
| } finally { | ||
| await fs.rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.