Skip to content

Commit

Permalink
fix(ScoreResultCalculator): fix faulty filenames in stryker score res…
Browse files Browse the repository at this point in the history
…ult (#1165)

The filter method used causes a side-effect that creates a corrupt path, I solved this by short circuiting the filter method so it only returns the elements up until the element that does no longer match.

Fixes #1140
  • Loading branch information
Sander Koenders authored and simondel committed Oct 2, 2018
1 parent bf80c2a commit 2555f49
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/stryker/src/ScoreResultCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,22 @@ export default class ScoreResultCalculator {
const uniqueFileDirectories = uniqueFiles.map(file => file.substr(basePath.length).split(path.sep));

if (uniqueFileDirectories.length) {
return uniqueFileDirectories
.reduce((previousDirectories, currentDirectories) => previousDirectories.filter((token, index) => currentDirectories[index] === token))
.join(path.sep);
return uniqueFileDirectories.reduce(this.filterDirectories).join(path.sep);
} else {
return '';
}
}

private filterDirectories(previousDirectories: string[], currentDirectories: string[]) {
for (let i = 0; i < previousDirectories.length; i++) {
if (previousDirectories[i] !== currentDirectories[i]) {
return previousDirectories.splice(0, i);
}
}

return previousDirectories;
}

private countNumbers(mutantResults: MutantResult[]) {
const count = (mutantResult: MutantStatus) => mutantResults.filter(_ => _.status === mutantResult).length;

Expand Down
11 changes: 11 additions & 0 deletions packages/stryker/test/unit/ScoreResultCalculatorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ describe('ScoreResult', () => {
expect(actual.childResults[0].name).to.eq('dir1/one');
expect(actual.childResults[1].name).to.eq('dir2/two');
});

it('should be able to handle the same file name in two different directories', () => {
const actual = sut.calculate([
mutantResult({ sourceFilePath: path.join('a', 'b', 'x.js') }),
mutantResult({ sourceFilePath: path.join('a', 'c', 'x.js') })
]);

expect(actual.name).to.eq('a');
expect(actual.childResults[0].name).to.equal(path.join('b', 'x.js'));
expect(actual.childResults[1].name).to.equal(path.join('c', 'x.js'));
});
});

describe('determineExitCode', () => {
Expand Down

0 comments on commit 2555f49

Please sign in to comment.