Skip to content

Commit

Permalink
Fail action if PHPUnit failed (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodore Messinezis committed Feb 6, 2020
1 parent 04ae9c1 commit 3c5443c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/Phpcov.ts
@@ -1,9 +1,10 @@
import deploy from "./Deploy";
import Metrix from "./Metrix";
import { dirSync } from "tmp";
import { exec } from "@actions/exec";
import * as core from "@actions/core";
import Config from "./types/PhpcovConfig";
import PhpcovOutput from "./types/PhpcovOutput";
import Metrix from "./Metrix";

/**
* Runs the PHPCov Action that this package implements.
Expand All @@ -20,11 +21,14 @@ export default async (config: Config): Promise<PhpcovOutput> => {

// Run PHPUnit to produce an code coverage reports.
const command = `${config.phpunit} ${html} ${clover}`.split(" ");
await exec(command[0], command.splice(1), {
const code = await exec(command[0], command.splice(1), {
cwd: config.workdir,
silent: (config.silent || false),
});

// Fails if status code was not 0.
if (code !== 0) core.setFailed("PHPUnit test suite failed.");

// Deploy the HTML report using Now.
const deployment = await deploy(
config.now_project,
Expand Down
17 changes: 17 additions & 0 deletions tests/Phpcov.spec.ts
Expand Up @@ -3,12 +3,14 @@ import Deploy from "../src/Deploy";
import Phpcov from "../src/Phpcov";
import Metrix from "../src/Metrix";
import { exec } from "@actions/exec";
import * as core from "@actions/core";
import { Deployment } from "now-client";
import PhpcovConfig from "../src/types/PhpcovConfig";
import PhpcovOutput from "../src/types/PhpcovOutput";
import CoverageMetrix from "../src/types/CoverageMetrix";

jest.mock("tmp");
jest.mock("@actions/core");
jest.mock("@actions/exec");
jest.mock("../src/Deploy");
jest.mock("../src/Metrix");
Expand All @@ -24,6 +26,8 @@ const default_config: PhpcovConfig = {
};

test("it produces coverage reports", async () => {
// Arrange: exec Deploy to return success.
(<jest.Mock>exec).mockReturnValue(0);
// Predefine Phpcov config paths.
const binary = "./vendor/bin/phpunit --testdox";
const workdir = "/home/theomessin";
Expand All @@ -47,6 +51,8 @@ test("it produces coverage reports", async () => {
expect(exec).toHaveBeenCalledTimes(1);
// Assert: GitHub Actions exec was called with correct arguments.
expect(exec).toHaveBeenCalledWith(command, expect.arrayContaining(args), opts);
// Assert: core set failed was called.
expect(core.setFailed).not.toBeCalled();
});

test("it uses temporary directory if none given", async () => {
Expand Down Expand Up @@ -119,3 +125,14 @@ test("it returns the coverage metrix", async () => {
// Assert: deploy was called just once.
expect(output).toMatchObject(__output);
});

test("it sets core to failed if phpunit fails", async () => {
// Arrange: mock Deploy to return a pre-set url.
(<jest.Mock>exec).mockReturnValue(1);

// Act: run Phpcov with test config.
await Phpcov(default_config);

// Assert: deploy was called just once.
expect(core.setFailed).toBeCalledWith("PHPUnit test suite failed.");
});

0 comments on commit 3c5443c

Please sign in to comment.