Skip to content

Commit

Permalink
chore(cli): run with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
marabesi committed Dec 7, 2024
1 parent d981142 commit 72b975f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 54 deletions.
9 changes: 5 additions & 4 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"description": "Find out the smells in your tests, suggestions for correction and the theory behind them",
"bin": "build/index.js",
"dependencies": {
"smelly-detector": "*",
"commander": "^12.1.0"
"commander": "^12.1.0",
"glob": "^11.0.0",
"smelly-detector": "*"
},
"repository": {
"url": "https://github.com/marabesi/smelly-test/tree/main/cli"
Expand All @@ -24,9 +25,9 @@
"nyc": "^15.1.0",
"sinon": "^19.0.2",
"ts-loader": "*",
"vitest": "*",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.2"
"ts-node": "^10.9.2",
"vitest": "*"
},
"scripts": {
"compile": "npm run clean && tsc && cp index.js build/",
Expand Down
29 changes: 15 additions & 14 deletions cli/src/find-smells.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'path';
import fs, { readdir } from 'node:fs/promises';
import { glob, globSync, globStream, globStreamSync, Glob } from 'glob';
import { join } from 'node:path';
import { Smell, SmellDetector, SupportedLanguages } from 'smelly-detector';
import { SmellsAggreagtor, SmellsList } from 'smelly-detector/reports';
import { statSync } from 'fs';

const args = process.argv;
const fileName = args[2];
Expand All @@ -11,7 +13,7 @@ const report = args[4];
const reportOutput = args[5];

if (!fileName) {
console.error('[SMELLY] please provide a test file');
console.error('[SMELLY] please provide a test file or a regex to search for test files');
process.exit();
}

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

async function execute() {
function isDirectorySync(path: string): boolean {
try {
const isFile = await fs.stat(fileName);
if (isFile && isFile.isFile()) {
const fileContents = await fs.readFile(fileName, { encoding: 'utf8' });
const smellDetector = new SmellDetector(fileContents, language);

const aggregator = [{ fileName, smells: smellDetector.findAll().smells, language }];

const to = path.resolve(reportOutput.replace('--report-output=', ''));
const report = new SmellsAggreagtor(aggregator, { to });
await report.build();
const stats = statSync(path);
return stats.isDirectory();
} catch (error) {
return false;
}
}

console.info('Report HTML generated');
async function execute() {
try {
if (isDirectorySync(fileName)) {
console.info('[SMELLY] please use a regex or a file');
return;
}

const allFiles = await walk(fileName);
const allFiles = await glob(fileName);
const pathWithAllFilesFound = allFiles.flat(Number.POSITIVE_INFINITY);

if (report) {
Expand Down
20 changes: 20 additions & 0 deletions cli/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,28 @@ describe('cli', () => {
test('find no smells for a given path', async () => {
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/ javascript --report=html --report-output=$(pwd)`);

expect(stdout).toContain("[SMELLY] please use a regex or a file");
});

test('find no smells for a path with regex', async () => {
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/**/*test.js javascript --report=html --report-output=$(pwd)`);

expect(stdout).toContain("Report HTML generated");
expect(stdout).not.toContain("Error:");
});

describe('validations', () => {
test('generate empty report if path does not exists', async () => {
const { stdout } = await execPromise(`npm run cli -- /bla/foo/whatever/ javascript --report=html --report-output=$(pwd)`);

expect(stdout).toContain("Report HTML generated");
});

test('requires a path to generate report', async () => {
const { stderr } = await execPromise(`npm run cli`);

expect(stderr).toContain("[SMELLY] please provide a test file or a regex to search for test files");
});
});
});
});
Loading

0 comments on commit 72b975f

Please sign in to comment.