feat(cli): Enhance test discovery with support for single file and glob pattern execution using improved CLI argument detection
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| import * as plugins from './tstest.plugins.js'; | ||||
| import * as paths from './tstest.paths.js'; | ||||
| import { SmartFile } from '@push.rocks/smartfile'; | ||||
| import { TestExecutionMode } from './index.js'; | ||||
|  | ||||
| // tap related stuff | ||||
| import { TapCombinator } from './tstest.classes.tap.combinator.js'; | ||||
| @@ -14,14 +15,14 @@ export class TestDirectory { | ||||
|   cwd: string; | ||||
|  | ||||
|   /** | ||||
|    * the relative location of the test dir | ||||
|    * the test path or pattern | ||||
|    */ | ||||
|   relativePath: string; | ||||
|   testPath: string; | ||||
|  | ||||
|   /** | ||||
|    * the absolute path of the test dir | ||||
|    * the execution mode | ||||
|    */ | ||||
|   absolutePath: string; | ||||
|   executionMode: TestExecutionMode; | ||||
|  | ||||
|   /** | ||||
|    * an array of Smartfiles | ||||
| @@ -30,27 +31,71 @@ export class TestDirectory { | ||||
|  | ||||
|   /** | ||||
|    * the constructor for TestDirectory | ||||
|    * tell it the path | ||||
|    * @param pathToTestDirectory | ||||
|    * @param cwdArg - the current working directory | ||||
|    * @param testPathArg - the test path/pattern | ||||
|    * @param executionModeArg - the execution mode | ||||
|    */ | ||||
|   constructor(cwdArg: string, relativePathToTestDirectory: string) { | ||||
|   constructor(cwdArg: string, testPathArg: string, executionModeArg: TestExecutionMode) { | ||||
|     this.cwd = cwdArg; | ||||
|     this.relativePath = relativePathToTestDirectory; | ||||
|     this.testPath = testPathArg; | ||||
|     this.executionMode = executionModeArg; | ||||
|   } | ||||
|  | ||||
|   private async _init() { | ||||
|     this.testfileArray = await plugins.smartfile.fs.fileTreeToObject( | ||||
|       plugins.path.join(this.cwd, this.relativePath), | ||||
|       'test*.ts' | ||||
|     ); | ||||
|     switch (this.executionMode) { | ||||
|       case TestExecutionMode.FILE: | ||||
|         // Single file mode | ||||
|         const filePath = plugins.path.isAbsolute(this.testPath)  | ||||
|           ? this.testPath  | ||||
|           : plugins.path.join(this.cwd, this.testPath); | ||||
|          | ||||
|         if (await plugins.smartfile.fs.fileExists(filePath)) { | ||||
|           this.testfileArray = [await plugins.smartfile.SmartFile.fromFilePath(filePath)]; | ||||
|         } else { | ||||
|           throw new Error(`Test file not found: ${filePath}`); | ||||
|         } | ||||
|         break; | ||||
|          | ||||
|       case TestExecutionMode.GLOB: | ||||
|         // Glob pattern mode - use listFileTree which supports glob patterns | ||||
|         const globPattern = this.testPath; | ||||
|         const matchedFiles = await plugins.smartfile.fs.listFileTree(this.cwd, globPattern); | ||||
|          | ||||
|         this.testfileArray = await Promise.all( | ||||
|           matchedFiles.map(async (filePath) => { | ||||
|             const absolutePath = plugins.path.isAbsolute(filePath)  | ||||
|               ? filePath  | ||||
|               : plugins.path.join(this.cwd, filePath); | ||||
|             return await plugins.smartfile.SmartFile.fromFilePath(absolutePath); | ||||
|           }) | ||||
|         ); | ||||
|         break; | ||||
|          | ||||
|       case TestExecutionMode.DIRECTORY: | ||||
|         // Directory mode - now recursive with ** pattern | ||||
|         const dirPath = plugins.path.join(this.cwd, this.testPath); | ||||
|         const testPattern = '**/test*.ts'; | ||||
|          | ||||
|         const testFiles = await plugins.smartfile.fs.listFileTree(dirPath, testPattern); | ||||
|          | ||||
|         this.testfileArray = await Promise.all( | ||||
|           testFiles.map(async (filePath) => { | ||||
|             const absolutePath = plugins.path.isAbsolute(filePath) | ||||
|               ? filePath | ||||
|               : plugins.path.join(dirPath, filePath); | ||||
|             return await plugins.smartfile.SmartFile.fromFilePath(absolutePath); | ||||
|           }) | ||||
|         ); | ||||
|         break; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   async getTestFilePathArray() { | ||||
|     await this._init(); | ||||
|     const testFilePaths: string[] = []; | ||||
|     for (const testFile of this.testfileArray) { | ||||
|       const filePath = plugins.path.join(this.relativePath, testFile.path); | ||||
|       testFilePaths.push(filePath); | ||||
|       // Use the path directly from the SmartFile | ||||
|       testFilePaths.push(testFile.path); | ||||
|     } | ||||
|     return testFilePaths; | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user