Skip to content

Commit 72b975f

Browse files
committed
chore(cli): run with regex
1 parent d981142 commit 72b975f

File tree

4 files changed

+119
-54
lines changed

4 files changed

+119
-54
lines changed

cli/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"description": "Find out the smells in your tests, suggestions for correction and the theory behind them",
66
"bin": "build/index.js",
77
"dependencies": {
8-
"smelly-detector": "*",
9-
"commander": "^12.1.0"
8+
"commander": "^12.1.0",
9+
"glob": "^11.0.0",
10+
"smelly-detector": "*"
1011
},
1112
"repository": {
1213
"url": "https://github.com/marabesi/smelly-test/tree/main/cli"
@@ -24,9 +25,9 @@
2425
"nyc": "^15.1.0",
2526
"sinon": "^19.0.2",
2627
"ts-loader": "*",
27-
"vitest": "*",
2828
"ts-mocha": "^10.0.0",
29-
"ts-node": "^10.9.2"
29+
"ts-node": "^10.9.2",
30+
"vitest": "*"
3031
},
3132
"scripts": {
3233
"compile": "npm run clean && tsc && cp index.js build/",

cli/src/find-smells.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import path from 'path';
22
import fs, { readdir } from 'node:fs/promises';
3+
import { glob, globSync, globStream, globStreamSync, Glob } from 'glob';
34
import { join } from 'node:path';
45
import { Smell, SmellDetector, SupportedLanguages } from 'smelly-detector';
56
import { SmellsAggreagtor, SmellsList } from 'smelly-detector/reports';
7+
import { statSync } from 'fs';
68

79
const args = process.argv;
810
const fileName = args[2];
@@ -11,7 +13,7 @@ const report = args[4];
1113
const reportOutput = args[5];
1214

1315
if (!fileName) {
14-
console.error('[SMELLY] please provide a test file');
16+
console.error('[SMELLY] please provide a test file or a regex to search for test files');
1517
process.exit();
1618
}
1719

@@ -22,24 +24,23 @@ const walk: any = async (dirPath: string) => Promise.all(
2224
}))
2325
);
2426

25-
async function execute() {
27+
function isDirectorySync(path: string): boolean {
2628
try {
27-
const isFile = await fs.stat(fileName);
28-
if (isFile && isFile.isFile()) {
29-
const fileContents = await fs.readFile(fileName, { encoding: 'utf8' });
30-
const smellDetector = new SmellDetector(fileContents, language);
31-
32-
const aggregator = [{ fileName, smells: smellDetector.findAll().smells, language }];
33-
34-
const to = path.resolve(reportOutput.replace('--report-output=', ''));
35-
const report = new SmellsAggreagtor(aggregator, { to });
36-
await report.build();
29+
const stats = statSync(path);
30+
return stats.isDirectory();
31+
} catch (error) {
32+
return false;
33+
}
34+
}
3735

38-
console.info('Report HTML generated');
36+
async function execute() {
37+
try {
38+
if (isDirectorySync(fileName)) {
39+
console.info('[SMELLY] please use a regex or a file');
3940
return;
4041
}
4142

42-
const allFiles = await walk(fileName);
43+
const allFiles = await glob(fileName);
4344
const pathWithAllFilesFound = allFiles.flat(Number.POSITIVE_INFINITY);
4445

4546
if (report) {

cli/test/cli.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,28 @@ describe('cli', () => {
2828
test('find no smells for a given path', async () => {
2929
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/ javascript --report=html --report-output=$(pwd)`);
3030

31+
expect(stdout).toContain("[SMELLY] please use a regex or a file");
32+
});
33+
34+
test('find no smells for a path with regex', async () => {
35+
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/**/*test.js javascript --report=html --report-output=$(pwd)`);
36+
3137
expect(stdout).toContain("Report HTML generated");
3238
expect(stdout).not.toContain("Error:");
3339
});
40+
41+
describe('validations', () => {
42+
test('generate empty report if path does not exists', async () => {
43+
const { stdout } = await execPromise(`npm run cli -- /bla/foo/whatever/ javascript --report=html --report-output=$(pwd)`);
44+
45+
expect(stdout).toContain("Report HTML generated");
46+
});
47+
48+
test('requires a path to generate report', async () => {
49+
const { stderr } = await execPromise(`npm run cli`);
50+
51+
expect(stderr).toContain("[SMELLY] please provide a test file or a regex to search for test files");
52+
});
53+
});
3454
});
3555
});

0 commit comments

Comments
 (0)