feat(cli): Enhance test discovery with support for single file and glob pattern execution using improved CLI argument detection

This commit is contained in:
2025-05-15 14:37:55 +00:00
parent 1f73751a8c
commit a57edeef64
11 changed files with 1660 additions and 1252 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@git.zone/tstest',
version: '1.0.96',
version: '1.1.0',
description: 'a test utility to run tests that match test/**/*.ts'
}

View File

@ -1,10 +1,29 @@
import { TsTest } from './tstest.classes.tstest.js';
export enum TestExecutionMode {
DIRECTORY = 'directory',
FILE = 'file',
GLOB = 'glob'
}
export const runCli = async () => {
if (!process.argv[2]) {
console.error('You must specify a test directory as argument. Please try again.');
console.error('You must specify a test directory/file/pattern as argument. Please try again.');
process.exit(1);
}
const tsTestInstance = new TsTest(process.cwd(), process.argv[2]);
const testPath = process.argv[2];
let executionMode: TestExecutionMode;
// Detect execution mode based on the argument
if (testPath.includes('*') || testPath.includes('?') || testPath.includes('[') || testPath.includes('{')) {
executionMode = TestExecutionMode.GLOB;
} else if (testPath.endsWith('.ts')) {
executionMode = TestExecutionMode.FILE;
} else {
executionMode = TestExecutionMode.DIRECTORY;
}
const tsTestInstance = new TsTest(process.cwd(), testPath, executionMode);
await tsTestInstance.run();
};

View File

@ -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;
}

View File

@ -7,9 +7,11 @@ import { coloredString as cs } from '@push.rocks/consolecolor';
import { TestDirectory } from './tstest.classes.testdirectory.js';
import { TapCombinator } from './tstest.classes.tap.combinator.js';
import { TapParser } from './tstest.classes.tap.parser.js';
import { TestExecutionMode } from './index.js';
export class TsTest {
public testDir: TestDirectory;
public executionMode: TestExecutionMode;
public smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
@ -20,8 +22,9 @@ export class TsTest {
public tsbundleInstance = new plugins.tsbundle.TsBundle();
constructor(cwdArg: string, relativePathToTestDirectory: string) {
this.testDir = new TestDirectory(cwdArg, relativePathToTestDirectory);
constructor(cwdArg: string, testPathArg: string, executionModeArg: TestExecutionMode) {
this.executionMode = executionModeArg;
this.testDir = new TestDirectory(cwdArg, testPathArg, executionModeArg);
}
async run() {