Skip to content

Commit 6c29fa2

Browse files
committed
fix(detector): sending testCase for html
1 parent 149b6a0 commit 6c29fa2

File tree

7 files changed

+49
-34
lines changed

7 files changed

+49
-34
lines changed

cli/src/find-smells.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22
import fs from 'node:fs/promises';
33
import { glob } from 'glob';
4-
import { Smell, SmellDetector, SupportedLanguages } from 'smelly-detector';
4+
import { Smell, SmellDetector, SupportedLanguages, TestCase } from 'smelly-detector';
55
import { SmellsAggreagtor, SmellsList } from 'smelly-detector/reports';
66
import { statSync } from 'fs';
77

@@ -37,16 +37,18 @@ async function execute() {
3737

3838
if (report) {
3939
const aggregator: SmellsList[] = [];
40+
const testCases: TestCase[] = [];
4041

4142
for (const file of pathWithAllFilesFound) {
4243
const fileContent = await fs.readFile(file, { encoding: 'utf8' });
4344
const smellDetector = new SmellDetector(file, fileContent, language);
44-
const smells = smellDetector.findAll().smellsList.smells;
45-
aggregator.push({ fileName: file, smells, language, fileContent });
45+
const smells = smellDetector.findAll();
46+
aggregator.push(smells.smellsList);
47+
testCases.push(...smells.testCases);
4648
}
4749

4850
const to = path.resolve(reportOutput.replace('--report-output=', ''));
49-
const report = new SmellsAggreagtor(aggregator, { to });
51+
const report = new SmellsAggreagtor(testCases, aggregator, { to });
5052
await report.build();
5153

5254
console.info('Report HTML generated');

detector/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { SmellDetector } from './smells-detector';
2-
export { Smell, SupportedLanguages } from './types';
2+
export { Smell, SupportedLanguages, TestCase } from './types';
33
export { supportedLanguages } from './languages/Supported';

detector/src/reporters/SmellsAgreggator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { SmellsList } from '../types';
1+
import { SmellsList, TestCase } from '../types';
22
import { HtmlOutput } from './Output';
33
import { AgreggatorSmellls, ExportOptions } from './types';
44

55
export class SmellsAggreagtor implements AgreggatorSmellls {
66
constructor(
7-
private totalTestFiles: SmellsList[],
8-
private exportOptions: ExportOptions
7+
private readonly result: TestCase[],
8+
private readonly totalTestFiles: SmellsList[],
9+
private readonly exportOptions: ExportOptions
910
) { }
1011

1112
async build(): Promise<void> {
@@ -18,7 +19,7 @@ export class SmellsAggreagtor implements AgreggatorSmellls {
1819
totalSmells,
1920
data: this.totalTestFiles,
2021
averageSmellsPerTestFile: totalSmells / totalFiles,
21-
totalTestCases: 0,
22+
totalTestCases: this.result.length,
2223
}, this.exportOptions);
2324
} catch (err) {
2425
console.log(err);

detector/src/smells-detector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SmellDetectorRunnerResult, SupportedLanguages } from './types';
77
export class SmellDetector {
88

99
constructor(
10-
private fileName: string,
10+
private readonly fileName: string,
1111
private readonly code: string,
1212
private readonly language: string
1313
) { }

detector/test/html-report-builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AggregatedData, ExportOptions, SmellsList } from '../src/reporters/types';
1+
import { AggregatedData, ExportOptions } from '../src/reporters/types';
2+
import { SmellsList } from '../src/types';
23
import { HtmlOutput } from '../src/reporters/Output';
34
import { readFileSync } from 'fs';
45

detector/test/html-report.integration.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { test, describe, expect, afterEach, beforeEach } from 'vitest';
22
import { parse } from 'node-html-parser';
3-
import { AggregatedData, ExportOptions, SmellsList } from '../src/reporters/types';
3+
import { AggregatedData, ExportOptions } from '../src/reporters/types';
44
import { rmSync } from 'fs';
55
import { buildEmptyHtmlReportForTestSmells, buildHtmlReportForTestSmellsFor } from './html-report-builder';
66
import { SmellsBuilder } from '../src/smells-builder';
7-
import { Smell, SupportedLanguages } from '../src/types';
7+
import { Smell, SmellsList, SupportedLanguages } from '../src/types';
88

99
describe('html report', () => {
1010
const exportsOptions: ExportOptions = { to: '.' };
@@ -71,7 +71,7 @@ describe('html report', () => {
7171
},
7272
];
7373

74-
return { data: smellsFound, totalSmells: 0, averageSmellsPerTestFile: 0 };
74+
return { data: smellsFound, totalSmells: 0, averageSmellsPerTestFile: 0, totalTestCases: 0 };
7575
};
7676

7777
test('renders number of test smell for a given file', async () => {

detector/test/smells-aggregator.test.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { vi,test, describe, expect } from 'vitest';
22
import { HtmlOutput } from '../src/reporters/Output';
33
import { SmellsBuilder } from '../src/smells-builder';
44
import { SmellsAggreagtor } from '../src/reporters/SmellsAgreggator';
5-
import { ExportOptions, SmellsList } from '../src/reporters/types';
6-
import { Smell, SmellType, SupportedLanguages } from '../src/types';
5+
import { ExportOptions } from '../src/reporters/types';
6+
import { Smell, SmellsList, SmellType, SupportedLanguages, TestCase } from '../src/types';
77

88
vi.mock('../src/reporters/Output');
99

@@ -37,7 +37,7 @@ describe('smells aggregator', () => {
3737

3838
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
3939

40-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
40+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
4141

4242
await reporter.build();
4343

@@ -51,20 +51,20 @@ describe('smells aggregator', () => {
5151

5252
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
5353

54-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
54+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
5555

5656
await reporter.build();
5757

5858
expect(write.mock.calls[0][0].data[0].fileContent).toEqual('console.log("Hello world")');
5959
});
6060

61-
test.skip('should send total of test cases to the output', async () => {
61+
test('should send total of test cases to the output', async () => {
6262
const smellsFound: SmellsList[] = buildListWithSingleSmell([createSingleSmell()]);
6363
const exportsOptions: ExportOptions = { to: '.' };
6464

6565
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
6666

67-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
67+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
6868

6969
await reporter.build();
7070

@@ -77,36 +77,47 @@ describe('smells aggregator', () => {
7777

7878
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
7979

80-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
80+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
8181

8282
await reporter.build();
8383

8484
expect(write.mock.calls[0][0].data).toEqual(smellsFound);
8585
});
8686

8787
test('compute smells for a single file', async () => {
88-
const smell: Smell = {
89-
type: SmellType.consoleStatement,
90-
lineStart: 0,
91-
lineEnd: 1,
92-
startAt: 10,
93-
endsAt: 20,
94-
description: '',
95-
diagnostic: ''
96-
};
97-
9888
const smellsFound: SmellsList[] = buildListWithSingleSmell([createSingleSmell()]);
9989
const exportsOptions: ExportOptions = { to: '.' };
10090

10191
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
10292

103-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
93+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
10494

10595
await reporter.build();
10696

10797
expect(write.mock.calls[0][0].totalSmells).toEqual(1);
10898
});
10999

100+
test('compute test cases for a single file', async () => {
101+
const smellsFound: SmellsList[] = buildListWithSingleSmell([createSingleSmell()]);
102+
const testCases: TestCase[] = [
103+
{
104+
lineStart: 0,
105+
lineEnd: 0,
106+
startAt: 0,
107+
endsAt: 0
108+
}
109+
];
110+
const exportsOptions: ExportOptions = { to: '.' };
111+
112+
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
113+
114+
const reporter = new SmellsAggreagtor(testCases, smellsFound, exportsOptions);
115+
116+
await reporter.build();
117+
118+
expect(write.mock.calls[0][0].totalTestCases).toEqual(1);
119+
});
120+
110121
test('two test file with 5 smells each, should have average of 5 smells per test file', async () => {
111122
const smell: Smell = SmellsBuilder.console(0, 1, 10, 20);
112123
const smellsFound: SmellsList[] = [
@@ -127,7 +138,7 @@ describe('smells aggregator', () => {
127138

128139
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
129140

130-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
141+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
131142

132143
await reporter.build();
133144

@@ -154,7 +165,7 @@ describe('smells aggregator', () => {
154165

155166
const write = vi.mocked(HtmlOutput.prototype.writeTo = vi.fn());
156167

157-
const reporter = new SmellsAggreagtor(smellsFound, exportsOptions);
168+
const reporter = new SmellsAggreagtor([], smellsFound, exportsOptions);
158169

159170
await reporter.build();
160171

0 commit comments

Comments
 (0)