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

build(deps): bump weapon-regex from 0.3.0 to 0.4.1 #2796

Merged
merged 3 commits into from
Mar 11, 2021
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
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"]
}