Skip to content

Commit

Permalink
feat: Allow use on branch without changed js and ts file (#36)
Browse files Browse the repository at this point in the history
* chore: Remove '@stryker-mutator/api' as peer dependency

* chore: Obligate use of node >= v12

* chore(package): Avoid unnecessary download of 'snyk' dependency on lib installation

* feat: Allow use on branch without changed js and ts file

If there is no altered file, stryker-diff-runner will be aborted and
will not result in a break, allowing use in the pipeline.

It will also validate if the branch passed in the '--branch' argument
exists, otherwise the execution will be aborted in error.

closes #34

* chore(index): change log to warn

* test: validate output when there is no file to be mutated

* tests: increase code coverage

* chore(package): change test script to show all tests on log

Co-authored-by: Thomas VERHOKEN <thomas.verhoken@gmail.com>
  • Loading branch information
PauloGoncalvesBH and tverhoken authored Jan 12, 2021
1 parent e880315 commit 151a143
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"clean": "rm -r dist",
"build": "npm run clean || tsc -p tsconfig.prod.json",
"lint": "tslint --project .",
"test": "jest --coverage",
"test": "jest --coverage --silent --verbose",
"test:ci": "npm run test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"test:watch": "jest --watch",
"release": "git add dist && standard-version --commit-all --releaseCommitMessageFormat=\"chore(release): %s [ci skip]\"",
Expand Down
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ export default function run(commandArgs: string[]) {
}

exec(
`git diff origin/${branch || "master"} --name-only | grep -E -v '.*\\.test.*' | grep -e 'src/.*\\.[jt]s'`,
(error, stdout, stderr) => {
`git rev-parse --verify origin/${branch || "master"}`, (error) => {
if (error) {
console.error(error.message);
console.error(stderr);
console.error(`Stryker-diff-runner:\n\t"origin/${branch || "master"}" branch was not found.\n\tStryker-diff-runner will be aborted.\n`);
process.exit(1);
}
}
);

exec(
`git diff origin/${branch || "master"} --name-only | grep -E -v '.*.test.*' | grep -e 'src/.*.[jt]s'`,
(error, stdout, stderr) => {
if (error) {
console.warn('Stryker-diff-runner:\n\tNo files found in the current branch to be mutated.')
process.exit(0);
}

const filesToMutate = stdout.split("\n");
filesToMutate.splice(filesToMutate.length - 1, 1);
Expand Down
54 changes: 49 additions & 5 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,70 @@ describe("Stryker diff runner", () => {
jest.restoreAllMocks();
});

it('Should execute git rev-parse on "origin/master" by default.', () => {
const exitMock = jest.spyOn(process, "exit").mockImplementation();

run(["node", "exec"]);

(exec as any).mock.calls[0][1](null, "")

expect((exec as any).mock.calls[0][0]).toMatch('git rev-parse --verify origin/master');
expect(exitMock).not.toHaveBeenCalled();
});

it('Should execute git rev-parse on "origin/test" when "--branch" arg is provided.', () => {
run(["node", "exec", "--branch", "test"]);

expect((exec as any).mock.calls[0][0]).toMatch('git rev-parse --verify origin/test');
});

it('Should execute git diff on "origin/master" by default.', () => {
run(["node", "exec"]);

expect((exec as any).mock.calls[0][0]).toMatch(/^git diff origin\/master .*$/);
expect((exec as any).mock.calls[1][0]).toMatch(/^git diff origin\/master .*$/);
});

it('Should execute git diff on "origin/test" when "--branch" arg is provided.', () => {
run(["node", "exec", "--branch", "test"]);

expect((exec as any).mock.calls[0][0]).toMatch(/^git diff origin\/test .*$/);
expect((exec as any).mock.calls[1][0]).toMatch(/^git diff origin\/test .*$/);
});

it("Should exit when file diff command gives an error.", () => {
it("Should exit with branch message not found when branch 'non-existent' doesn't exist.", () => {
const branch = "non-existent";
const exitMock = jest.spyOn(process, "exit").mockImplementation();
const consoleSpy = jest.spyOn(console, "error");

run(["node", "exec", "--branch", branch]);

(exec as any).mock.calls[0][1](new Error("NO BRANCH NON-EXISTENT"), "")

expect(exitMock).toHaveBeenCalledWith(1);
expect(consoleSpy).toHaveBeenCalledWith(`Stryker-diff-runner:\n\t"origin/${branch}" branch was not found.\n\tStryker-diff-runner will be aborted.\n`)
});

it("Should exit with branch message not found when default branch doesn't exist.", () => {
const exitMock = jest.spyOn(process, "exit").mockImplementation();
const consoleSpy = jest.spyOn(console, "error");

run(["node", "exec"]);

runFileDiffCommandCallback(new Error("NOPE"), "");
(exec as any).mock.calls[0][1](new Error("NO BRANCH MASTER"), "")

expect(exitMock).toHaveBeenCalledWith(1);
expect(consoleSpy).toHaveBeenCalledWith(`Stryker-diff-runner:\n\t"origin/master" branch was not found.\n\tStryker-diff-runner will be aborted.\n`)
});

it("should exit with a message that no files will be mutated when there is no changed file in the branch.", () => {
const exitMock = jest.spyOn(process, "exit").mockImplementation();
const consoleSpy = jest.spyOn(console, "warn");

run(["node", "exec"]);

runFileDiffCommandCallback(new Error("NO FILE"), "");

expect(exitMock).toHaveBeenCalledWith(0);
expect(consoleSpy).toHaveBeenCalledWith('Stryker-diff-runner:\n\tNo files found in the current branch to be mutated.')
});

it("Should run mutation test with default loaded configuration when no args are provided to the run.", (done) => {
Expand Down Expand Up @@ -234,6 +278,6 @@ describe("Stryker diff runner", () => {
}

function runFileDiffCommandCallback(error: Error | null, fileDiffList: string) {
(exec as any).mock.calls[0][1](error, fileDiffList);
(exec as any).mock.calls[1][1](error, fileDiffList);
}
});

0 comments on commit 151a143

Please sign in to comment.