Skip to content

Commit

Permalink
build(deps): bump weapon-regex from 0.3.0 to 0.4.1 (#2796)
Browse files Browse the repository at this point in the history
Bumps [weapon-regex](https://github.com/stryker-mutator/weapon-regex) from 0.3.0 to 0.4.1.
- [Release notes](https://github.com/stryker-mutator/weapon-regex/releases)
- [Changelog](https://github.com/stryker-mutator/weapon-regex/blob/main/release.sbt)
- [Commits](https://github.com/stryker-mutator/weapon-regex/commits/v0.4.1)
- test(weapon-regex): test unhappy flow scenario with a stub

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Nico Jansen <jansennico@gmail.com>
  • Loading branch information
dependabot-preview[bot] and nicojs authored Mar 11, 2021
1 parent a6ca0f2 commit dda6fb6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/instrumenter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@stryker-mutator/api": "4.5.0",
"@stryker-mutator/util": "4.5.0",
"angular-html-parser": "~1.7.0",
"weapon-regex": "~0.3.0"
"weapon-regex": "~0.4.1"
},
"devDependencies": {
"@babel/preset-react": "~7.12.1",
Expand Down
32 changes: 17 additions & 15 deletions packages/instrumenter/src/mutators/regex-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,14 @@ function isObviousRegexString(path: NodePath<types.StringLiteral>) {
}
const weaponRegexOptions: weaponRegex.Options = { mutationLevels: [1] };

function mutatePattern(pattern: string): string[] {
if (pattern.length) {
try {
return weaponRegex.mutate(pattern, weaponRegexOptions).map((mutant) => mutant.pattern);
} catch (err) {
console.error(
`[RegexMutator]: The Regex parser of weapon-regex couldn't parse this regex pattern: "${pattern}". Please report this issue at https://github.com/stryker-mutator/weapon-regex/issues. Inner error: ${err.message}`
);
}
}
return [];
}

export class RegexMutator implements NodeMutator {
public name = 'Regex';

constructor(private readonly weaponRegexMutateImpl = weaponRegex.mutate) {}

public mutate(path: NodePath): NodeMutation[] {
if (path.isRegExpLiteral()) {
return mutatePattern(path.node.pattern).map((replacementPattern) => {
return this.mutatePattern(path.node.pattern).map((replacementPattern) => {
const replacement = types.cloneNode(path.node, false);
replacement.pattern = replacementPattern;
return {
Expand All @@ -49,7 +38,7 @@ export class RegexMutator implements NodeMutator {
};
});
} else if (path.isStringLiteral() && isObviousRegexString(path)) {
return mutatePattern(path.node.value).map((replacementPattern) => {
return this.mutatePattern(path.node.value).map((replacementPattern) => {
const replacement = types.cloneNode(path.node, false);
replacement.value = replacementPattern;
return {
Expand All @@ -60,4 +49,17 @@ export class RegexMutator implements NodeMutator {
}
return [];
}

private mutatePattern(pattern: string): string[] {
if (pattern.length) {
try {
return this.weaponRegexMutateImpl(pattern, weaponRegexOptions).map((mutant) => mutant.pattern);
} catch (err) {
console.error(
`[RegexMutator]: The Regex parser of weapon-regex couldn't parse this regex pattern: "${pattern}". Please report this issue at https://github.com/stryker-mutator/weapon-regex/issues. Inner error: ${err.message}`
);
}
}
return [];
}
}
18 changes: 13 additions & 5 deletions packages/instrumenter/test/unit/mutators/regex-mutator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,44 @@ import { RegexMutator } from '../../../src/mutators/regex-mutator';
import { expectJSMutation } from '../../helpers/expect-mutation';

describe(RegexMutator.name, () => {
let sut: RegexMutator;
beforeEach(() => {
sut = new RegexMutator();
});

it('should have name "Regex"', () => {
const sut = new RegexMutator();
expect(sut.name).eq('Regex');
});

it('should not mutate normal string literals', () => {
const sut = new RegexMutator();
expectJSMutation(sut, '""');
});

it('should mutate a regex literal', () => {
const sut = new RegexMutator();
expectJSMutation(sut, '/\\d{4}/', '/\\d/', '/\\D{4}/');
});

it("should not crash if a regex couldn't be parsed", () => {
// Arrange
const weaponRegexStub = sinon.stub();
weaponRegexStub.throws(new Error('[Error] Parser: Position 1:1, found "[[]]"'));
const sut = new RegexMutator(weaponRegexStub);
const errorStub = sinon.stub(console, 'error');

// Act
expectJSMutation(sut, '/[[]]/');

// Assert
expect(errorStub).calledWith(
'[RegexMutator]: The Regex parser of weapon-regex couldn\'t parse this regex pattern: "[[]]". Please report this issue at https://github.com/stryker-mutator/weapon-regex/issues. Inner error: [Error] Parser: Position 1:1, found "[[]]"'
);
});

it('should mutate obvious Regex string literals', () => {
const sut = new RegexMutator();
expectJSMutation(sut, 'new RegExp("\\\\d{4}")', 'new RegExp("\\\\d")', 'new RegExp("\\\\D{4}")');
});

it('should not mutate the flags of a new RegExp constructor', () => {
const sut = new RegexMutator();
expectJSMutation(sut, 'new RegExp("", "\\\\d{4}")');
});
});
2 changes: 1 addition & 1 deletion packages/instrumenter/tsconfig.src.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"path": "../util/tsconfig.src.json"
}
],
"include": ["src"]
"include": ["src", "typings"]
}
4 changes: 1 addition & 3 deletions packages/instrumenter/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@
"path": "../test-helpers/tsconfig.src.json"
}
],
"include": [
"test"
]
"include": ["test", "typings"]
}

0 comments on commit dda6fb6

Please sign in to comment.