|
| 1 | +import chalk from 'chalk'; |
1 | 2 | import fs from 'fs';
|
2 | 3 | import mkdirp from 'mkdirp';
|
3 | 4 | import path from 'path';
|
| 5 | +import rimraf from 'rimraf'; |
4 | 6 | import jsxKnownIssues from '../jsx-known-issues';
|
5 | 7 |
|
6 | 8 | const jsxFilesWithKnownIssues = jsxKnownIssues.map(f => `${f}.src.js`);
|
| 9 | +const forceOverwrite = process.argv.includes('--force'); |
| 10 | + |
| 11 | +type PackageName = 'parser' | 'typescript-estree'; |
| 12 | +// set built up over time listing the fixtures that were written |
| 13 | +const writtenFixtures: Readonly<Record<PackageName, Set<string>>> = { |
| 14 | + parser: new Set(), |
| 15 | + 'typescript-estree': new Set(), |
| 16 | +}; |
| 17 | + |
| 18 | +const TEST_NAMES = { |
| 19 | + WITH_LOCATION_INFO: 'withLocationInfo.test.ts', |
| 20 | + WITHOUT_LOCATION_INFO: 'withoutLocationInfo.test.ts', |
| 21 | +}; |
7 | 22 |
|
8 | 23 | function main(): void {
|
| 24 | + // write new fixtures this also works to build the list of fixtures that should exist, |
| 25 | + // so that we can later check which fixtures have been deleted. |
9 | 26 | const fixtureFolder = 'fixtures';
|
10 | 27 | const fixtureGroups = fs.readdirSync(fixtureFolder);
|
11 | 28 | fixtureGroups.forEach(groupName => {
|
12 | 29 | const groupFolder = path.join(fixtureFolder, groupName);
|
13 |
| - handleGroup(groupFolder); |
| 30 | + handleGroup(groupFolder, fixturePath => { |
| 31 | + if ( |
| 32 | + !jsxFilesWithKnownIssues.some(jsxName => fixturePath.endsWith(jsxName)) |
| 33 | + ) { |
| 34 | + createTest(fixturePath, 'typescript-estree'); |
| 35 | + createTest(fixturePath, 'parser'); |
| 36 | + } |
| 37 | + }); |
14 | 38 | });
|
15 | 39 |
|
16 |
| - function handleGroup(groupFolder: string): void { |
17 |
| - const fixtures = fs.readdirSync(groupFolder); |
18 |
| - fixtures.forEach(fileOrSubGroupName => { |
19 |
| - const fileOrSubGroupPath = path.join(groupFolder, fileOrSubGroupName); |
20 |
| - if (fs.statSync(fileOrSubGroupPath).isDirectory()) { |
21 |
| - handleGroup(fileOrSubGroupPath); |
22 |
| - } else if ( |
23 |
| - !jsxFilesWithKnownIssues.some(jsxName => |
24 |
| - fileOrSubGroupPath.endsWith(jsxName), |
| 40 | + // cleanup deleted fixtures |
| 41 | + const removeTestRegEx = new RegExp( |
| 42 | + `[\\/\\\\]${TEST_NAMES.WITH_LOCATION_INFO}`, |
| 43 | + ); |
| 44 | + (Object.keys(writtenFixtures) as PackageName[]).forEach(packageName => { |
| 45 | + const generatedTestDir = getPackageDirectory(packageName); |
| 46 | + handleGroup(generatedTestDir, fixtureFilePath => { |
| 47 | + if (!fixtureFilePath.endsWith(TEST_NAMES.WITH_LOCATION_INFO)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const fixtureDir = fixtureFilePath.replace(removeTestRegEx, ''); |
| 52 | + if ( |
| 53 | + !writtenFixtures[packageName].has( |
| 54 | + fixtureFilePath.replace(removeTestRegEx, ''), |
25 | 55 | )
|
26 | 56 | ) {
|
27 |
| - createTest(fileOrSubGroupPath, 'typescript-estree'); |
28 |
| - createTest(fileOrSubGroupPath, 'parser'); |
| 57 | + rimraf.sync(fixtureDir); |
| 58 | + console.log( |
| 59 | + chalk.red('deleted test:'), |
| 60 | + path.relative(__dirname, fixtureDir).replace(/\.\.\//g, ''), |
| 61 | + ); |
29 | 62 | }
|
30 | 63 | });
|
31 |
| - } |
| 64 | + }); |
32 | 65 | }
|
33 | 66 |
|
34 |
| -function createTest( |
35 |
| - fixturePath: string, |
36 |
| - moduleName: 'parser' | 'typescript-estree', |
| 67 | +function handleGroup( |
| 68 | + groupFolder: string, |
| 69 | + callback: (fixturePath: string) => void, |
37 | 70 | ): void {
|
| 71 | + const fixtures = fs.readdirSync(groupFolder); |
| 72 | + fixtures.forEach(fileOrSubGroupName => { |
| 73 | + const fileOrSubGroupPath = path.join(groupFolder, fileOrSubGroupName); |
| 74 | + if (fs.statSync(fileOrSubGroupPath).isDirectory()) { |
| 75 | + handleGroup(fileOrSubGroupPath, callback); |
| 76 | + } else { |
| 77 | + callback(fileOrSubGroupPath); |
| 78 | + } |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function getPackageDirectory(packageName: PackageName): string { |
| 83 | + return path.resolve( |
| 84 | + __dirname, |
| 85 | + '..', |
| 86 | + '..', |
| 87 | + packageName, |
| 88 | + 'tests', |
| 89 | + 'shared-fixtures', |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +function createTest(fixturePath: string, packageName: PackageName): void { |
38 | 94 | const fixtureWithoutExt = fixturePath
|
39 | 95 | // fixtures all end in /.src.[jt]sx?/
|
40 | 96 | .substring(0, fixturePath.indexOf('.'))
|
41 | 97 | // mark them as generated
|
42 | 98 | .replace('fixtures/', 'generated/');
|
43 | 99 |
|
44 | 100 | const testDir = path.resolve(
|
45 |
| - __dirname, |
46 |
| - '..', |
47 |
| - '..', |
48 |
| - moduleName, |
49 |
| - 'tests', |
50 |
| - 'shared-fixtures', |
| 101 | + getPackageDirectory(packageName), |
51 | 102 | fixtureWithoutExt,
|
52 | 103 | );
|
53 | 104 |
|
54 | 105 | // create the folder and all parent folders
|
55 | 106 | mkdirp.sync(testDir);
|
| 107 | + writtenFixtures[packageName].add(testDir); |
56 | 108 |
|
57 | 109 | // create test files
|
58 |
| - fs.writeFileSync( |
59 |
| - path.join(testDir, 'withLocationInfo.test.ts'), |
60 |
| - testContents(fixturePath, 'With'), |
61 |
| - 'utf8', |
62 |
| - ); |
| 110 | + function writeTestFile(type: 'With' | 'Without'): void { |
| 111 | + const testFile = path.join( |
| 112 | + testDir, |
| 113 | + type === 'With' |
| 114 | + ? TEST_NAMES.WITH_LOCATION_INFO |
| 115 | + : TEST_NAMES.WITHOUT_LOCATION_INFO, |
| 116 | + ); |
| 117 | + if (forceOverwrite || !fs.existsSync(testFile)) { |
| 118 | + fs.writeFileSync(testFile, testContents(fixturePath, type), 'utf8'); |
| 119 | + console.log( |
| 120 | + chalk.green('wrote test:'), |
| 121 | + path.relative(__dirname, testFile).replace(/\.\.\//g, ''), |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + writeTestFile('With'); |
63 | 126 | // TODO - waiting for https://github.com/typescript-eslint/typescript-eslint/pull/704
|
64 |
| - // fs.writeFileSync( |
65 |
| - // path.join(testDir, 'withoutLocationInfo.test.ts'), |
66 |
| - // testContents(fixturePath, 'Without'), |
67 |
| - // 'utf8', |
68 |
| - // ); |
69 |
| - |
70 |
| - console.log('wrote tests:', path.relative(__dirname, testDir)); |
| 127 | + // writeTestFile('Without'); |
71 | 128 | }
|
72 | 129 |
|
73 | 130 | const testContents = (
|
|
0 commit comments