Skip to content

Commit

Permalink
feat(deps): update source-map dep to current major release
Browse files Browse the repository at this point in the history
This should have feature parity with current Stryker functionality.
  • Loading branch information
simondel authored and nicojs committed May 17, 2019
1 parent ee4d27c commit 45fa0f8
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 74 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"progress": "~2.0.0",
"rimraf": "~2.6.1",
"rxjs": "~6.5.1",
"source-map": "~0.6.1",
"source-map": "~0.7.3",
"surrial": "~1.0.0",
"tree-kill": "~1.2.0",
"tslib": "~1.9.3",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/mutants/MutantTestMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MutantTestMatcher {
}
}

public matchWithMutants(mutants: ReadonlyArray<Mutant>): ReadonlyArray<TestableMutant> {
public async matchWithMutants(mutants: ReadonlyArray<Mutant>): Promise<ReadonlyArray<TestableMutant>> {

const testableMutants = this.createTestableMutants(mutants);

Expand All @@ -58,14 +58,14 @@ export class MutantTestMatcher {
this.log.warn('No coverage result found, even though coverageAnalysis is "%s". Assuming that all tests cover each mutant. This might have a big impact on the performance.', this.options.coverageAnalysis);
testableMutants.forEach(mutant => mutant.selectAllTests(this.initialRunResult.runResult, TestSelectionResult.FailedButAlreadyReported));
} else {
testableMutants.forEach(testableMutant => this.enrichWithCoveredTests(testableMutant));
await Promise.all(testableMutants.map(testableMutant => this.enrichWithCoveredTests(testableMutant)));
}
this.reporter.onAllMutantsMatchedWithTests(Object.freeze(testableMutants.map(this.mapMutantOnMatchedMutant)));
return testableMutants;
}

public enrichWithCoveredTests(testableMutant: TestableMutant) {
const transpiledLocation = this.initialRunResult.sourceMapper.transpiledLocationFor({
public async enrichWithCoveredTests(testableMutant: TestableMutant) {
const transpiledLocation = await this.initialRunResult.sourceMapper.transpiledLocationFor({
fileName: testableMutant.mutant.fileName,
location: testableMutant.location
});
Expand Down
34 changes: 19 additions & 15 deletions packages/core/src/transpiler/SourceMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default abstract class SourceMapper {
* Calculated a transpiled location for a given original location
* @param originalLocation The original location to be converted to a transpiled location
*/
public abstract transpiledLocationFor(originalLocation: MappedLocation): MappedLocation;
public abstract async transpiledLocationFor(originalLocation: MappedLocation): Promise<MappedLocation>;

public abstract transpiledFileNameFor(originalFileName: string): string;

Expand Down Expand Up @@ -69,11 +69,11 @@ export class TranspiledSourceMapper extends SourceMapper {
/**
* @inheritdoc
*/
public transpiledLocationFor(originalLocation: MappedLocation): MappedLocation {
public async transpiledLocationFor(originalLocation: MappedLocation): Promise<MappedLocation> {
const sourceMap = this.getSourceMap(originalLocation.fileName);
const relativeSource = this.getRelativeSource(sourceMap, originalLocation);
const start = sourceMap.generatedPositionFor(originalLocation.location.start, relativeSource);
const end = sourceMap.generatedPositionFor(originalLocation.location.end, relativeSource);
const start = await sourceMap.generatedPositionFor(originalLocation.location.start, relativeSource);
const end = await sourceMap.generatedPositionFor(originalLocation.location.end, relativeSource);
return {
fileName: sourceMap.transpiledFile.name,
location: {
Expand Down Expand Up @@ -213,27 +213,31 @@ export class PassThroughSourceMapper extends SourceMapper {
/**
* @inheritdoc
*/
public transpiledLocationFor(originalLocation: MappedLocation): MappedLocation {
return originalLocation;
public async transpiledLocationFor(originalLocation: MappedLocation): Promise<MappedLocation> {
return Promise.resolve(originalLocation);
}
}

class SourceMap {
private readonly sourceMap: SourceMapConsumer;
constructor(public transpiledFile: File, public sourceMapFileName: string, rawSourceMap: RawSourceMap) {
this.sourceMap = new SourceMapConsumer(rawSourceMap);
private sourceMap: SourceMapConsumer | undefined;
constructor(public transpiledFile: File, public sourceMapFileName: string, private readonly rawSourceMap: RawSourceMap) {
}
public generatedPositionFor(originalPosition: Position, relativeSource: string): Position {
const transpiledPosition = this.sourceMap.generatedPositionFor({
public async generatedPositionFor(originalPosition: Position, relativeSource: string): Promise<Position> {
if (!this.sourceMap) {
this.sourceMap = await new SourceMapConsumer(this.rawSourceMap);
}

const transpiledPosition = await this.sourceMap.generatedPositionFor({
bias: SourceMapConsumer.LEAST_UPPER_BOUND,
column: originalPosition.column,
line: originalPosition.line + 1, // SourceMapConsumer works 1-based
source: relativeSource
});
return {
column: transpiledPosition.column,
line: transpiledPosition.line - 1 // Stryker works 0-based
};

return Promise.resolve({
column: transpiledPosition.column || 0,
line: (transpiledPosition.line || 1) - 1 // Stryker works 0-based
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Source mapper integration', () => {
});

it('it should be able to map to transpiled location', async () => {
const actual = sut.transpiledLocationFor({
const actual = await sut.transpiledLocationFor({
fileName: resolve('external-source-maps', 'ts', 'src', 'math.ts'),
location: {
end: { line: 7, column: 42 },
Expand All @@ -50,7 +50,7 @@ describe('Source mapper integration', () => {
sut = new TranspiledSourceMapper(files);
});
it('it should be able to map to transpiled location', async () => {
const actual = sut.transpiledLocationFor({
const actual = await sut.transpiledLocationFor({
fileName: resolve('inline-source-maps', 'ts', 'src', 'math.ts'),
location: {
end: { line: 7, column: 42 },
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/unit/SandboxPool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe(SandboxPool.name, () => {
runResult: { tests: [], status: RunStatus.Complete },
sourceMapper: {
transpiledFileNameFor: n => n,
transpiledLocationFor: n => n
transpiledLocationFor: n => Promise.resolve(n)
}
};

Expand Down
70 changes: 46 additions & 24 deletions packages/core/test/unit/mutants/MutantTestMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,21 @@ describe(MutantTestMatcher.name, () => {

describe('without code coverage info', () => {

it('should add both tests to the mutants and report failure', () => {
const result = sut.matchWithMutants(mutants);
it('should add both tests to the mutants and report failure', async () => {
const expectedTestSelection = [{ id: 0, name: 'test one' }, { id: 1, name: 'test two' }];

const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).deep.eq(expectedTestSelection);
expect(result[1].selectedTests).deep.eq(expectedTestSelection);
expect(TestSelectionResult[result[0].testSelectionResult]).eq(TestSelectionResult[TestSelectionResult.FailedButAlreadyReported]);
expect(TestSelectionResult[result[1].testSelectionResult]).eq(TestSelectionResult[TestSelectionResult.FailedButAlreadyReported]);
expect(testInjector.logger.warn).calledWith('No coverage result found, even though coverageAnalysis is "%s". Assuming that all tests cover each mutant. This might have a big impact on the performance.', 'perTest');
});

it('should have both mutants matched', () => {
const result = sut.matchWithMutants(mutants);
it('should have both mutants matched', async () => {
const result = await sut.matchWithMutants(mutants);

const matchedMutants: MatchedMutant[] = [
{
fileName: result[0].fileName,
Expand All @@ -122,6 +125,7 @@ describe(MutantTestMatcher.name, () => {
timeSpentScopedTests: result[1].timeSpentScopedTests
}
];

expect(reporter.onAllMutantsMatchedWithTests).calledWith(Object.freeze(matchedMutants));
});
});
Expand Down Expand Up @@ -188,8 +192,9 @@ describe(MutantTestMatcher.name, () => {
};
});

it('should not have added the run results to the mutants', () => {
const result = sut.matchWithMutants(mutants);
it('should not have added the run results to the mutants', async () => {
const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).lengthOf(0);
expect(result[1].selectedTests).lengthOf(0);
});
Expand Down Expand Up @@ -226,12 +231,14 @@ describe(MutantTestMatcher.name, () => {
sut = createSut();
});

it('should have added the run results to the mutants', () => {
const result = sut.matchWithMutants(mutants);
it('should have added the run results to the mutants', async () => {
const expectedTestSelectionFirstMutant: TestSelection[] = [
{ id: 0, name: 'test one' },
{ id: 1, name: 'test two' }
];

const result = await sut.matchWithMutants(mutants);

const expectedTestSelectionSecondMutant: TestSelection[] = [{ id: 0, name: 'test one' }];
expect(result[0].selectedTests).deep.eq(expectedTestSelectionFirstMutant);
expect(result[1].selectedTests).deep.eq(expectedTestSelectionSecondMutant);
Expand All @@ -248,12 +255,14 @@ describe(MutantTestMatcher.name, () => {
sut = createSut();
});

it('should select all test in the test run but not report the error yet', () => {
const result = sut.matchWithMutants(mutants);
it('should select all test in the test run but not report the error yet', async () => {
const expectedTestSelection: TestSelection[] = [
{ name: 'test one', id: 0 },
{ name: 'test two', id: 1 }
];

const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).deep.eq(expectedTestSelection);
expect(result[1].selectedTests).deep.eq(expectedTestSelection);
expect(result[0].testSelectionResult).eq(TestSelectionResult.Failed);
Expand All @@ -278,9 +287,11 @@ describe(MutantTestMatcher.name, () => {
sut = createSut();
});

it('should have added the run results to the mutant', () => {
const result = sut.matchWithMutants(mutants);
it('should have added the run results to the mutant', async () => {
const expectedTestSelection = [{ id: 0, name: 'test one' }];

const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).deep.eq(expectedTestSelection);
});
});
Expand Down Expand Up @@ -309,9 +320,11 @@ describe(MutantTestMatcher.name, () => {
sut = createSut();
});

it('should add all test results to the mutant that is covered by the baseline', () => {
const result = sut.matchWithMutants(mutants);
it('should add all test results to the mutant that is covered by the baseline', async () => {
const expectedTestSelection = [{ id: 0, name: 'test one' }, { id: 1, name: 'test two' }];

const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).deep.eq(expectedTestSelection);
expect(result[1].selectedTests).deep.eq(expectedTestSelection);
});
Expand All @@ -320,7 +333,8 @@ describe(MutantTestMatcher.name, () => {
});

describe('should not result in regression', () => {
it('should match up mutant for issue #151 (https://github.com/stryker-mutator/stryker/issues/151)', () => {
it('should match up mutant for issue #151 (https://github.com/stryker-mutator/stryker/issues/151)', async () => {
// Arrange
const sourceFile = new SourceFile(new File('', ''));
sourceFile.getLocation = () => ({ start: { line: 13, column: 38 }, end: { line: 24, column: 5 } });
const testableMutant = new TestableMutant('1', mutant({
Expand All @@ -338,7 +352,11 @@ describe(MutantTestMatcher.name, () => {
timeSpentMs: 5
});
sut = createSut();
sut.enrichWithCoveredTests(testableMutant);

// Act
await sut.enrichWithCoveredTests(testableMutant);

// Assert
expect(testableMutant.selectedTests).deep.eq([{
id: 0,
name: 'controllers SearchResultController should open a modal dialog with product details'
Expand All @@ -354,11 +372,13 @@ describe(MutantTestMatcher.name, () => {
sut = createSut();
});

it('should match all mutants to all tests and log a warning when there is no coverage data', () => {
it('should match all mutants to all tests and log a warning when there is no coverage data', async () => {
mutants.push(mutant({ fileName: 'fileWithMutantOne' }), mutant({ fileName: 'fileWithMutantTwo' }));
initialRunResult.runResult.tests.push(testResult(), testResult());
const result = sut.matchWithMutants(mutants);
const expectedTestSelection: TestSelection[] = [{ id: 0, name: 'name' }, { id: 1, name: 'name' }];

const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).deep.eq(expectedTestSelection);
expect(result[1].selectedTests).deep.eq(expectedTestSelection);
expect(result[0].testSelectionResult).deep.eq(TestSelectionResult.FailedButAlreadyReported);
Expand All @@ -380,12 +400,12 @@ describe(MutantTestMatcher.name, () => {
};
});

it('should retrieves source mapped location', () => {
it('should retrieves source mapped location', async () => {
// Arrange
mutants.push(mutant({ fileName: 'fileWithMutantOne', range: [4, 5] }));

// Act
sut.matchWithMutants(mutants);
await sut.matchWithMutants(mutants);

// Assert
const expectedLocation: MappedLocation = {
Expand All @@ -398,13 +418,13 @@ describe(MutantTestMatcher.name, () => {
expect(initialRunResult.sourceMapper.transpiledLocationFor).calledWith(expectedLocation);
});

it('should match mutant to single test result', () => {
it('should match mutant to single test result', async () => {
// Arrange
mutants.push(mutant({ fileName: 'fileWithMutantOne', range: [4, 5] }));
initialRunResult.runResult.tests.push(testResult({ name: 'test 1' }), testResult({ name: 'test 2' }));

// Act
const result = sut.matchWithMutants(mutants);
const result = await sut.matchWithMutants(mutants);

// Assert
const expectedTestSelection: TestSelection[] = [{
Expand All @@ -428,11 +448,13 @@ describe(MutantTestMatcher.name, () => {
sut = createSut();
});

it('should match all mutants to all tests', () => {
it('should match all mutants to all tests', async () => {
mutants.push(mutant({ fileName: 'fileWithMutantOne' }), mutant({ fileName: 'fileWithMutantTwo' }));
initialRunResult.runResult.tests.push(testResult(), testResult());
const result = sut.matchWithMutants(mutants);
const expectedTestSelection = [{ id: 0, name: 'name' }, { id: 1, name: 'name' }];

const result = await sut.matchWithMutants(mutants);

expect(result[0].selectedTests).deep.eq(expectedTestSelection);
expect(result[1].selectedTests).deep.eq(expectedTestSelection);
});
Expand Down
Loading

0 comments on commit 45fa0f8

Please sign in to comment.