Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(score-result): Wrap single file reports #379

Merged
merged 1 commit into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/stryker/src/ScoreResultCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { MutationScoreThresholds } from 'stryker-api/core';
import { MutantResult, MutantStatus, ScoreResult } from 'stryker-api/report';
import { freezeRecursively, setExitCode } from './utils/objectUtils';


export default class ScoreResultCalculator {

static calculate(results: MutantResult[]): ScoreResult {
return this.calculateScoreResult(results, '');
const scoreResult = this.calculateScoreResult(results, '');
return this.wrapIfSingleFileScoreResult(scoreResult);
}

static determineExitCode(score: ScoreResult, thresholds: MutationScoreThresholds | undefined) {
Expand All @@ -28,20 +28,36 @@ export default class ScoreResultCalculator {
}
}

private static wrapIfSingleFileScoreResult(scoreResult: ScoreResult): ScoreResult {
if (scoreResult.representsFile) {
return this.copy(scoreResult, {
name: path.dirname(scoreResult.name), childResults: [
this.copy(scoreResult, { name: path.basename(scoreResult.name) })
]
});
} else {
return scoreResult;
}
}

private static calculateScoreResult(results: MutantResult[], basePath: string): ScoreResult {
const numbers = this.countNumbers(results);
const facts = this.determineFacts(basePath, results);
return freezeRecursively(_.assign(numbers, facts));
}

private static copy(defaults: ScoreResult, overrides: Partial<ScoreResult>): ScoreResult {
return Object.assign({}, defaults, overrides);
}

private static determineFacts(basePath: string, results: MutantResult[]) {
const name = this.determineCommonBasePath(results, basePath);
const childResults = this.calculateChildScores(results, name, basePath);
return {
name,
path: path.join(basePath, name),
childResults,
representsFile: childResults.length === 0
representsFile: childResults.length === 0 && results.length > 0
};
}

Expand Down
37 changes: 25 additions & 12 deletions packages/stryker/test/unit/ScoreResultCalculatorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ describe('ScoreResult', () => {
mutantResult({ status: MutantStatus.Killed, sourceFilePath: fileName }),
mutantResult({ status: MutantStatus.TimedOut, sourceFilePath: fileName }),
]);
expect(actual.name).to.eq(fileName);
expect(actual.killed, 'killed').to.eq(2);
expect(actual.survived, 'survived').to.eq(1);
expect(actual.noCoverage, 'noCoverage').to.eq(1);
expect(actual.timedOut, 'timedOut').to.eq(1);
expect(actual.totalCovered, 'totalCovered').to.eq(4);
expect(actual.totalDetected, 'detected').to.eq(3);
expect(actual.totalMutants, 'mutants').to.eq(5);
expect(actual.totalUndetected, 'undetected').to.eq(2);
expect(actual.mutationScoreBasedOnCoveredCode, 'percentageBasedOnCoveredCode').to.eq(75);
expect(actual.mutationScore, 'mutationScore').to.eq(60);
expect(actual.childResults).to.have.lengthOf(0);
expect(actual.name).to.eq('base');
function assertNumbers(actual: ScoreResult) {
expect(actual.killed, 'killed').to.eq(2);
expect(actual.survived, 'survived').to.eq(1);
expect(actual.noCoverage, 'noCoverage').to.eq(1);
expect(actual.timedOut, 'timedOut').to.eq(1);
expect(actual.totalCovered, 'totalCovered').to.eq(4);
expect(actual.totalDetected, 'detected').to.eq(3);
expect(actual.totalMutants, 'mutants').to.eq(5);
expect(actual.totalUndetected, 'undetected').to.eq(2);
expect(actual.mutationScoreBasedOnCoveredCode, 'percentageBasedOnCoveredCode').to.eq(75);
expect(actual.mutationScore, 'mutationScore').to.eq(60);
}
assertNumbers(actual);
assertNumbers(actual.childResults[0]);
});

it('should wrap a single result in its base directory', () => {
const fileName = path.join('base', 'something');
const actual = ScoreResultCalculator.calculate([
mutantResult({ status: MutantStatus.Error, sourceFilePath: fileName })
]);
expect(actual.name).eq('base');
expect(actual.childResults).lengthOf(1);
expect(actual.childResults[0].name).eq('something');
});

it('should count results of multiple files', () => {
Expand Down