Skip to content

Commit

Permalink
fix(cli): ensure filter and exclude when expanding glob from shell
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Jul 27, 2024
1 parent c9f831d commit de9d817
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/@types/poku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export type FinalResults = {
started: Date;
};

export type States = {
isSinglePath?: boolean;
};

type cliConfigs = {
/** By default, **Poku** searches for _`.test.`_ and `.spec.` files, but you can customize it. */
include?: string | string[];
Expand Down
6 changes: 5 additions & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Configs } from '../@types/poku.js';
import process from 'node:process';
import { escapeRegExp } from '../modules/helpers/list-files.js';
import { getArg, getPaths, hasArg, argToArray } from '../parsers/get-arg.js';
import { fileResults } from '../configs/files.js';
import { fileResults, states } from '../configs/files.js';
import { platformIsValid } from '../parsers/get-runtime.js';
import { format } from '../services/format.js';
import { kill } from '../modules/helpers/kill.js';
Expand Down Expand Up @@ -64,6 +64,10 @@ import { getConfigs } from '../parsers/options.js';
return Number.isNaN(value) ? defaultConfigs?.concurrency : value;
})();

if (dirs.length === 1) {
states.isSinglePath = true;
}

const tasks: Promise<unknown>[] = [];

if (killPort || defaultConfigs?.kill?.port) {
Expand Down
4 changes: 3 additions & 1 deletion src/configs/files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FileResults } from '../@types/list-files.js';
import type { FinalResults } from '../@types/poku.js';
import type { FinalResults, States } from '../@types/poku.js';

export const states = {} as States;

export const fileResults: FileResults = {
success: new Map(),
Expand Down
19 changes: 17 additions & 2 deletions src/modules/helpers/list-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Configs } from '../../@types/list-files.js';
import { env } from 'node:process';
import { sep, join } from 'node:path';
import { readdir, stat as fsStat } from '../../polyfills/fs.js';
import { states } from '../../configs/files.js';

const regex = {
sep: /[/\\]+/g,
Expand Down Expand Up @@ -38,7 +39,17 @@ export const getAllFiles = async (
files: Set<string> = new Set(),
configs?: Configs
): Promise<Set<string>> => {
const currentFiles = await readdir(sanitizePath(dirPath));
let isFullPath = false;

const currentFiles = await (async () => {
if (await isFile(dirPath)) {
isFullPath = true;

return Array.prototype.concat(sanitizePath(dirPath));
}

return await readdir(sanitizePath(dirPath));
})();

const filter: RegExp = envFilter
? envFilter
Expand All @@ -54,7 +65,7 @@ export const getAllFiles = async (

await Promise.all(
currentFiles.map(async (file) => {
const fullPath = join(dirPath, file);
const fullPath = isFullPath ? dirPath : join(dirPath, file);
const stat = await fsStat(fullPath);

if (
Expand All @@ -64,6 +75,10 @@ export const getAllFiles = async (
return;
}

if (isFullPath && states?.isSinglePath) {
return files.add(fullPath);
}

if (exclude) {
for (const pattern of exclude) {
if (pattern.test(fullPath)) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const processAssert = async (
exit(1);
}

/* c8 ignore next */ // Unknown external error
/* c8 ignore next 2 */ // Unknown external error
throw error;
}
};
Expand Down
16 changes: 4 additions & 12 deletions src/services/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { cwd as processCWD, hrtime } from 'node:process';
import { join, relative, sep } from 'node:path';
import { runner } from '../parsers/get-runner.js';
import { indentation } from '../configs/indentation.js';
import {
isFile as IS_FILE,
listFiles,
sanitizePath,
} from '../modules/helpers/list-files.js';
import { isFile as IS_FILE, listFiles } from '../modules/helpers/list-files.js';
import { Write } from '../services/write.js';
import { format } from './format.js';
import { runTestFile } from './run-test-file.js';
Expand All @@ -24,15 +20,13 @@ export const runTests = async (
const testDir = join(cwd, dir);
const currentDir = relative(cwd, testDir);
const isFile = await IS_FILE(testDir);
const files = isFile
? [sanitizePath(testDir)]
: await listFiles(testDir, configs);
const files = await listFiles(testDir, configs);
const totalTests = files.length;
const showLogs = !isQuiet(configs);

let passed = true;

if (showLogs) {
if (showLogs && files.length > 0) {
Write.hr();
Write.log(
`${format(isFile ? 'File:' : 'Directory:').bold()} ${format(`.${sep}${currentDir}`).underline()}\n`
Expand Down Expand Up @@ -93,9 +87,7 @@ export const runTestsParallel = async (
configs?: Configs
): Promise<boolean> => {
const testDir = join(cwd, dir);
const files = (await IS_FILE(dir))
? [sanitizePath(dir)]
: await listFiles(testDir, configs);
const files = await listFiles(testDir, configs);
const filesByConcurrency: string[][] = [];
const concurrencyLimit =
configs?.concurrency ?? Math.max(Math.floor(availableParallelism() / 2), 1);
Expand Down

0 comments on commit de9d817

Please sign in to comment.