From 2555f49e13ac646d79ecb00381a37d9c62584cb7 Mon Sep 17 00:00:00 2001 From: Sander Koenders Date: Tue, 2 Oct 2018 06:53:16 +0200 Subject: [PATCH] fix(ScoreResultCalculator): fix faulty filenames in stryker score result (#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 --- packages/stryker/src/ScoreResultCalculator.ts | 14 +++++++++++--- .../stryker/test/unit/ScoreResultCalculatorSpec.ts | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/stryker/src/ScoreResultCalculator.ts b/packages/stryker/src/ScoreResultCalculator.ts index 9b3051eccc..0ca48ea053 100644 --- a/packages/stryker/src/ScoreResultCalculator.ts +++ b/packages/stryker/src/ScoreResultCalculator.ts @@ -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; diff --git a/packages/stryker/test/unit/ScoreResultCalculatorSpec.ts b/packages/stryker/test/unit/ScoreResultCalculatorSpec.ts index 749ec689e7..e8e0a73843 100644 --- a/packages/stryker/test/unit/ScoreResultCalculatorSpec.ts +++ b/packages/stryker/test/unit/ScoreResultCalculatorSpec.ts @@ -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', () => {