|
| 1 | +import Directory from '../../@internal/Directory'; |
| 2 | +import File from '../../@internal/File'; |
| 3 | +import FilesystemPath from '../../@internal/FilesystemPath'; |
| 4 | + |
| 5 | +// take a given file path and generate a stub for a test suite |
| 6 | +function createAndSaveTestCompanionForUnitWith( |
| 7 | + givenAbsolutePath: string, |
| 8 | +) { |
| 9 | + const componentsOfAbsolutePath = givenAbsolutePath.split('/'); |
| 10 | + const fileNameWithExtension = componentsOfAbsolutePath.pop() ?? ''; |
| 11 | + const [fileName, fileExtension] = fileNameWithExtension.split('.'); |
| 12 | + |
| 13 | + if ( |
| 14 | + !fileName || !fileExtension |
| 15 | + ) throw new Error(`Could not parse file name and extension from: ${givenAbsolutePath}`); // eslint-disable-line no-restricted-syntax -- "@/library/Attempt is not accessible outside of src" |
| 16 | + |
| 17 | + const testFileName = `${fileName}.test.${fileExtension}`; |
| 18 | + const absolutePathToTestCompanion = [...componentsOfAbsolutePath, testFileName].join('/'); |
| 19 | + const symbolName = 'someSymbol'; |
| 20 | + |
| 21 | + const contentsOfTestCompanion = `import { |
| 22 | + describe, |
| 23 | +} from 'vitest'; |
| 24 | +
|
| 25 | +import { |
| 26 | + ${symbolName}, |
| 27 | +} from './${fileNameWithExtension}'; |
| 28 | +
|
| 29 | +describe.todo(${symbolName}); |
| 30 | +`; |
| 31 | + |
| 32 | + const pathToTestCompanion = FilesystemPath.parsedFrom(absolutePathToTestCompanion); |
| 33 | + const pathToDirectoryOfTestCompanion = FilesystemPath.parsedFrom(pathToTestCompanion.dir); |
| 34 | + |
| 35 | + if ( |
| 36 | + !File.doesExistAt(pathToDirectoryOfTestCompanion) |
| 37 | + ) Directory.createAt(pathToDirectoryOfTestCompanion); |
| 38 | + |
| 39 | + const testCompanionShouldBeCreated = !File.doesExistAt(pathToTestCompanion); |
| 40 | + |
| 41 | + if ( |
| 42 | + testCompanionShouldBeCreated |
| 43 | + ) File.write({ |
| 44 | + contents: contentsOfTestCompanion, |
| 45 | + to : pathToTestCompanion, |
| 46 | + }); |
| 47 | + |
| 48 | + return { |
| 49 | + created : testCompanionShouldBeCreated, |
| 50 | + pathForTestFile: absolutePathToTestCompanion, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +const inputPath = process.argv[2]; |
| 55 | + |
| 56 | +if (!inputPath) { |
| 57 | + console.error('Usage: npx jiti scripts/generate-stub-for-test-suite.ts <path-to-source-file.ts>'); |
| 58 | + process.exit(1); |
| 59 | +} |
| 60 | + |
| 61 | +// eslint-disable-next-line no-restricted-syntax -- "@/library/Attempt is not accessible outside of src" |
| 62 | +try { |
| 63 | + const result = createAndSaveTestCompanionForUnitWith(inputPath); |
| 64 | + |
| 65 | + const messageForResult = result.created |
| 66 | + ? `Created test stub: ${result.pathForTestFile}` |
| 67 | + : `Test file already exists: ${result.pathForTestFile}`; |
| 68 | + |
| 69 | + console.log(messageForResult); |
| 70 | +} |
| 71 | +catch (err) { |
| 72 | + console.error('Error:', (err as Error).message); |
| 73 | + process.exit(1); |
| 74 | +} |
0 commit comments