Skip to content

Commit

Permalink
feat(reporter): Add threshold reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kucherenko committed Aug 17, 2018
1 parent 6ae45ca commit e8e1b25
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ![AVA](logo.svg)


# Copy/paste detector (ALPHA VERSION).

Expand Down
3 changes: 1 addition & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cli.parse(process.argv);
const options: IOptions = prepareOptions(cli);

if (cli.list) {
console.log(bold(white("Supported formats: ")));
console.log(bold(white('Supported formats: ')));
console.log(getSupportedFormats().join(', '));
} else {
const cpd: JSCPD = new JSCPD({
Expand All @@ -74,4 +74,3 @@ if (cli.list) {
});
cpd.detectInFiles(options.path);
}

2 changes: 1 addition & 1 deletion src/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class Detector {
const source: ISource = sourcesStore.get(sourceId);
if (source && source.hashes) {
source.hashes[format] = source.hashes.hasOwnProperty(format)
? source.hashes[format].concat(hash)
? [... new Set(source.hashes[format].concat(hash))]
: [hash];
sourcesStore.set(sourceId, source);
}
Expand Down
6 changes: 5 additions & 1 deletion src/reporters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { IReporter } from '../interfaces/reporter.interface';
import { ConsoleReporter } from './console';
import { ConsoleFullReporter } from './consoleFull';
import { JsonReporter } from './json';
import { SilentReporter } from './silent';
import { StatisticReporter } from './statistic';
import { TimeReporter } from './time';
import { SilentReporter } from './silent';
import { XmlReporter } from './xml';
import { ThresholdReporter } from './threshold';

const REPORTERS: { [key: string]: IReporter } = {};

Expand Down Expand Up @@ -47,4 +48,7 @@ export function registerReportersByName(options: IOptions) {
if (reporter.includes('silent')) {
registerReporter('silent', new SilentReporter(options));
}
if (reporter.includes('threshold')) {
registerReporter('threshold', new ThresholdReporter(options));
}
}
13 changes: 7 additions & 6 deletions src/reporters/silent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { STATISTIC_DB } from '../stores/models';
import { StoresManager } from '../stores/stores-manager';

export class SilentReporter implements IReporter {
constructor(private options: IOptions) {
}
constructor(private options: IOptions) {}

public attach(): void {
Events.on(END_EVENT, this.finish.bind(this));
Expand All @@ -19,10 +18,12 @@ export class SilentReporter implements IReporter {
);
if (statistic) {
console.log(
`Duplications detection: Found ${bold(statistic.all.clones)} `+
`exact clones with ${bold(statistic.all.duplicatedLines)}(${statistic.all.percentage}%) `+
`duplicated lines in ${bold(statistic.all.sources)} ` +
`(${Object.keys(statistic.formats).length} formats) files.`
`Duplications detection: Found ${bold(statistic.all.clones)} ` +
`exact clones with ${bold(statistic.all.duplicatedLines)}(${
statistic.all.percentage
}%) ` +
`duplicated lines in ${bold(statistic.all.sources)} ` +
`(${Object.keys(statistic.formats).length} formats) files.`
);
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/reporters/threshold.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { red } from 'colors/safe';
import { END_PROCESS_EVENT, Events } from '../events';
import { IOptions } from '../interfaces/options.interface';
import { IReporter } from '../interfaces/reporter.interface';
import { STATISTIC_DB } from '../stores/models';
import { StoresManager } from '../stores/stores-manager';

export class ThresholdReporter implements IReporter {
constructor(private options: IOptions) {
}

public attach(): void {
Events.on(END_PROCESS_EVENT, this.finish.bind(this));
}

private finish() {
const statistic = StoresManager.getStore(STATISTIC_DB).get(
this.options.executionId
);
if (statistic) {
if (this.options.threshold && this.options.threshold < statistic.all.percentage) {
console.error(red('ERROR: jscpd found too many duplicates over threshold'));
process.exit(1);
}
}
}
}
33 changes: 21 additions & 12 deletions src/reporters/xml.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { writeFileSync } from 'fs';
import { ensureDirSync } from 'fs-extra';
import { StoresManager } from '..';
import { Events } from '../events';
import { IClone } from '../interfaces/clone.interface';
import { IOptions } from '../interfaces/options.interface';
import { IReporter } from '../interfaces/reporter.interface';
import { SOURCES_DB } from '../stores/models';
import { StoresManager } from '..';


export class XmlReporter implements IReporter {

constructor(private options: IOptions) {}

public attach(): void {
Expand All @@ -19,24 +17,35 @@ export class XmlReporter implements IReporter {
private saveReport(clones: IClone[]) {
let xmlDoc: string = '<?xml version="1.0" encoding="UTF-8" ?>';

xmlDoc = this.options.xslHref ? xmlDoc + '<?xml-stylesheet type="text/xsl" href="' + this.options.xslHref + '"?>' : xmlDoc;
xmlDoc = this.options.xslHref
? xmlDoc +
'<?xml-stylesheet type="text/xsl" href="' +
this.options.xslHref +
'"?>'
: xmlDoc;
xmlDoc += '<pmd-cpd>';

clones.forEach((clone: IClone) => {
xmlDoc = `${xmlDoc}
<duplication lines="${clone.duplicationA.end.loc.end.line - clone.duplicationA.start.loc.start.line}">
<file path="${StoresManager.getStore(SOURCES_DB).get(clone.duplicationA.sourceId).id}" line="${clone.duplicationA.start.loc.start.line}" />
<file path="${StoresManager.getStore(SOURCES_DB).get(clone.duplicationB.sourceId).id}" line="${clone.duplicationB.start.loc.start.line}" />
<duplication lines="${clone.duplicationA.end.loc.end.line -
clone.duplicationA.start.loc.start.line}">
<file path="${
StoresManager.getStore(SOURCES_DB).get(
clone.duplicationA.sourceId
).id
}" line="${clone.duplicationA.start.loc.start.line}" />
<file path="${
StoresManager.getStore(SOURCES_DB).get(
clone.duplicationB.sourceId
).id
}" line="${clone.duplicationB.start.loc.start.line}" />
<codefragment><![CDATA[${clone.fragment}]]></codefragment>
</duplication>
`
`;
});
xmlDoc += '</pmd-cpd>';

ensureDirSync(this.options.output);
writeFileSync(
this.options.output + '/jscpd-report.xml',
xmlDoc
);
writeFileSync(this.options.output + '/jscpd-report.xml', xmlDoc);
}
}
1 change: 0 additions & 1 deletion src/stores/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,5 @@ export class FilesStore<TValue extends IStoreValue> implements IStore<TValue> {

public close(): void {
writeJSONSync(this.pathToFile, this.values);
this.values = {};
}
}
4 changes: 2 additions & 2 deletions src/stores/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ export class MemoryStore<TValue extends IStoreValue> implements IStore<TValue> {
}

public close(): void {
this.values = {};
}
Object.freeze(this.values);
};
}
4 changes: 3 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ export function prepareOptions(cli: Command): IOptions {
...storedConfig,
...argsConfig
};

if (result.silent) {
result.reporter = ['silent'];
}
if (result.threshold) {
result.reporter = [...(result.reporter || []), 'threshold'];
}
result.reporter = ['stat', ...(result.reporter || []), 'time'];
result.reporter = [...new Set(result.reporter)];
return result;
Expand Down

0 comments on commit e8e1b25

Please sign in to comment.