Skip to content

Commit

Permalink
[Patch]: Fixing push if badges do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jpb06 committed Apr 27, 2021
1 parent 9f51469 commit b0a7ff7
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ jobs:
run: yarn test:ci
[...]
- name: Generating coverage badges
- uses: actions/jest-badges-action@v1.2.5
- uses: actions/jest-badges-action@v1.2.6
```
54 changes: 43 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.pushBadges = void 0;
const core_1 = __nccwpck_require__(5537);
const exec_1 = __nccwpck_require__(4909);
const hasCoverageEvolved_1 = __nccwpck_require__(4438);
const pushBadges = () => __awaiter(void 0, void 0, void 0, function* () {
const hasEvolved = yield hasCoverageEvolved_1.hasCoverageEvolved();
if (!hasEvolved) {
core_1.info("> Coverage has not evolved, no action required.");
return;
}
yield exec_1.exec("git add", ["./badges"]);
yield exec_1.exec("git commit", ["-m", "Updating coverage badges"]);
yield exec_1.exec("git push");
Expand Down Expand Up @@ -65,6 +58,39 @@ const setGitConfig = () => __awaiter(void 0, void 0, void 0, function* () {
exports.setGitConfig = setGitConfig;


/***/ }),

/***/ 6439:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.doBadgesExist = void 0;
const fs_extra_1 = __nccwpck_require__(4982);
const doBadgesExist = () => __awaiter(void 0, void 0, void 0, function* () {
const files = [
"coverage-branches.svg",
"coverage-functions.svg",
"coverage-global coverage.svg",
"coverage-lines.svg",
"coverage-statements.svg",
];
const exist = yield Promise.all(files.map((file) => fs_extra_1.pathExists(`./badges/${file}`)));
return exist.every((el) => el === true);
});
exports.doBadgesExist = doBadgesExist;


/***/ }),

/***/ 4438:
Expand All @@ -83,11 +109,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.hasCoverageEvolved = void 0;
const fs_extra_1 = __nccwpck_require__(4982);
const exec_1 = __nccwpck_require__(4909);
const hasCoverageEvolved = () => __awaiter(void 0, void 0, void 0, function* () {
const folderExists = yield fs_extra_1.pathExists("./coverage");
if (!folderExists) {
const hasCoverageEvolved = (badgesExist) => __awaiter(void 0, void 0, void 0, function* () {
if (!badgesExist) {
return true;
}
const code = yield exec_1.exec("git diff", ["--quiet", "badges"], {
Expand Down Expand Up @@ -174,15 +198,23 @@ const node_jest_badges_1 = __nccwpck_require__(7017);
const core_1 = __nccwpck_require__(5537);
const pushBadges_1 = __nccwpck_require__(3474);
const setGitConfig_1 = __nccwpck_require__(2967);
const doBadgesExist_1 = __nccwpck_require__(6439);
const hasCoverageEvolved_1 = __nccwpck_require__(4438);
const isJestCoverageReportAvailable_1 = __nccwpck_require__(8949);
const actionWorkflow = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const isReportAvailable = yield isJestCoverageReportAvailable_1.isJestCoverageReportAvailable();
if (!isReportAvailable) {
return core_1.setFailed("> Coverage report is missing. Did you forget to run tests or to add `json-summary` to coverageReporters in jest config?");
}
const badgesExist = yield doBadgesExist_1.doBadgesExist();
core_1.info("> Generating badges");
yield node_jest_badges_1.generateBadges();
const hasEvolved = yield hasCoverageEvolved_1.hasCoverageEvolved(badgesExist);
if (!hasEvolved) {
core_1.info("> Coverage has not evolved, no action required.");
return;
}
core_1.info("> Pushing badges to the repo");
yield setGitConfig_1.setGitConfig();
yield pushBadges_1.pushBadges();
Expand Down
21 changes: 2 additions & 19 deletions src/logic/git/pushBadges.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
import { mocked } from "ts-jest/utils";
import { exec } from '@actions/exec';

import { info } from "@actions/core";
import { exec } from "@actions/exec";
import { pushBadges } from './pushBadges';

import { pushBadges } from "./pushBadges";
import { hasCoverageEvolved } from "../jest/hasCoverageEvolved";

jest.mock("@actions/core");
jest.mock("@actions/exec");
jest.mock("../jest/hasCoverageEvolved");

describe("pushBadges function", () => {
beforeEach(() => jest.clearAllMocks());

it("should not push if there is no changes", async () => {
mocked(hasCoverageEvolved).mockResolvedValueOnce(false);

await pushBadges();

expect(exec).toHaveBeenCalledTimes(0);
expect(info).toHaveBeenCalledTimes(1);
});

it("should push changes", async () => {
mocked(hasCoverageEvolved).mockResolvedValueOnce(true);

await pushBadges();

expect(exec).toHaveBeenCalledTimes(3);
Expand Down
10 changes: 1 addition & 9 deletions src/logic/git/pushBadges.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { info } from "@actions/core";
import { exec } from "@actions/exec";
import { hasCoverageEvolved } from "../jest/hasCoverageEvolved";
import { exec } from '@actions/exec';

export const pushBadges = async (): Promise<void> => {
const hasEvolved = await hasCoverageEvolved();
if (!hasEvolved) {
info("> Coverage has not evolved, no action required.");
return;
}

await exec("git add", ["./badges"]);
await exec("git commit", ["-m", "Updating coverage badges"]);
await exec("git push");
Expand Down
34 changes: 34 additions & 0 deletions src/logic/jest/doBadgesExist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { pathExists } from 'fs-extra';
import { mocked } from 'ts-jest/utils';

import { doBadgesExist } from './doBadgesExist';

jest.mock("fs-extra");

describe("doBadgesExist function", () => {
it("should return false if one file does not exist", async () => {
mocked(pathExists)
.mockImplementationOnce(() => false)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true);

const result = await doBadgesExist();

expect(result).toBe(false);
});

it("should return true if all files exist", async () => {
mocked(pathExists)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true)
.mockImplementationOnce(() => true);

const result = await doBadgesExist();

expect(result).toBe(true);
});
});
17 changes: 17 additions & 0 deletions src/logic/jest/doBadgesExist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { pathExists } from 'fs-extra';

export const doBadgesExist = async (): Promise<boolean> => {
const files = [
"coverage-branches.svg",
"coverage-functions.svg",
"coverage-global coverage.svg",
"coverage-lines.svg",
"coverage-statements.svg",
];

const exist = await Promise.all(
files.map((file) => pathExists(`./badges/${file}`))
);

return exist.every((el) => el === true);
};
12 changes: 3 additions & 9 deletions src/logic/jest/hasCoverageEvolved.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
import { pathExists } from 'fs-extra';
import { mocked } from 'ts-jest/utils';

import { exec } from '@actions/exec';

import { hasCoverageEvolved } from './hasCoverageEvolved';

jest.mock("@actions/exec");
jest.mock("fs-extra");

describe("hasCoverageEvolved function", () => {
it("should return true if coverage folder does not exist", async () => {
mocked(pathExists).mockImplementationOnce(() => false);

const result = await hasCoverageEvolved();
const result = await hasCoverageEvolved(false);

expect(result).toBe(true);
});

it("should return true if diff returns one", async () => {
mocked(pathExists).mockImplementationOnce(() => true);
mocked(exec).mockResolvedValueOnce(1);

const result = await hasCoverageEvolved();
const result = await hasCoverageEvolved(true);

expect(result).toBe(true);
});

it("should return false if diff returns zero", async () => {
mocked(pathExists).mockImplementationOnce(() => true);
mocked(exec).mockResolvedValueOnce(0);

const result = await hasCoverageEvolved();
const result = await hasCoverageEvolved(true);

expect(result).toBe(false);
});
Expand Down
9 changes: 4 additions & 5 deletions src/logic/jest/hasCoverageEvolved.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { pathExists } from 'fs-extra';

import { exec } from '@actions/exec';

export const hasCoverageEvolved = async (): Promise<boolean> => {
const folderExists = await pathExists("./coverage");
if (!folderExists) {
export const hasCoverageEvolved = async (
badgesExist: boolean
): Promise<boolean> => {
if (!badgesExist) {
return true;
}

Expand Down
37 changes: 29 additions & 8 deletions src/workflow/actionWorkflow.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { generateBadges } from "node-jest-badges";
import { mocked } from "ts-jest/utils";
import { generateBadges } from 'node-jest-badges';
import { mocked } from 'ts-jest/utils';

import { info, setFailed } from "@actions/core";
import { info, setFailed } from '@actions/core';

import { pushBadges } from "../logic/git/pushBadges";
import { setGitConfig } from "../logic/git/setGitConfig";
import { isJestCoverageReportAvailable } from "../logic/jest/isJestCoverageReportAvailable";
import { actionWorkflow } from "./actionWorkflow";
import { pushBadges } from '../logic/git/pushBadges';
import { setGitConfig } from '../logic/git/setGitConfig';
import { doBadgesExist } from '../logic/jest/doBadgesExist';
import { hasCoverageEvolved } from '../logic/jest/hasCoverageEvolved';
import { isJestCoverageReportAvailable } from '../logic/jest/isJestCoverageReportAvailable';
import { actionWorkflow } from './actionWorkflow';

jest.mock("@actions/core");
jest.mock("node-jest-badges");
jest.mock("../logic/git/pushBadges");
jest.mock("../logic/git/setGitConfig");
jest.mock("../logic/jest/isJestCoverageReportAvailable");
jest.mock("../logic/jest/doBadgesExist");
jest.mock("../logic/jest/hasCoverageEvolved");

describe("actionWorkflow function", () => {
afterEach(() => jest.resetAllMocks());
afterEach(() => jest.clearAllMocks());

it("should fail the task if there is no coverage report", async () => {
mocked(isJestCoverageReportAvailable).mockResolvedValueOnce(false);
Expand All @@ -32,8 +36,25 @@ describe("actionWorkflow function", () => {
);
});

it("should do nothing if coverage has not evolved", async () => {
mocked(isJestCoverageReportAvailable).mockResolvedValueOnce(true);
mocked(doBadgesExist).mockResolvedValueOnce(true);
mocked(hasCoverageEvolved).mockResolvedValueOnce(false);

await actionWorkflow();

expect(generateBadges).toHaveBeenCalledTimes(1);
expect(setGitConfig).toHaveBeenCalledTimes(0);
expect(pushBadges).toHaveBeenCalledTimes(0);

expect(info).toHaveBeenCalledTimes(2);
expect(setFailed).toHaveBeenCalledTimes(0);
});

it("should generate badges and push them", async () => {
mocked(isJestCoverageReportAvailable).mockResolvedValueOnce(true);
mocked(doBadgesExist).mockResolvedValueOnce(true);
mocked(hasCoverageEvolved).mockResolvedValueOnce(true);

await actionWorkflow();

Expand Down
20 changes: 15 additions & 5 deletions src/workflow/actionWorkflow.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { generateBadges } from "node-jest-badges";
import { generateBadges } from 'node-jest-badges';

import { info, setFailed } from "@actions/core";
import { info, setFailed } from '@actions/core';

import { pushBadges } from "../logic/git/pushBadges";
import { setGitConfig } from "../logic/git/setGitConfig";
import { isJestCoverageReportAvailable } from "../logic/jest/isJestCoverageReportAvailable";
import { pushBadges } from '../logic/git/pushBadges';
import { setGitConfig } from '../logic/git/setGitConfig';
import { doBadgesExist } from '../logic/jest/doBadgesExist';
import { hasCoverageEvolved } from '../logic/jest/hasCoverageEvolved';
import { isJestCoverageReportAvailable } from '../logic/jest/isJestCoverageReportAvailable';

export const actionWorkflow = async (): Promise<void> => {
try {
Expand All @@ -15,9 +17,17 @@ export const actionWorkflow = async (): Promise<void> => {
);
}

const badgesExist = await doBadgesExist();

info("> Generating badges");
await generateBadges();

const hasEvolved = await hasCoverageEvolved(badgesExist);
if (!hasEvolved) {
info("> Coverage has not evolved, no action required.");
return;
}

info("> Pushing badges to the repo");
await setGitConfig();
await pushBadges();
Expand Down

0 comments on commit b0a7ff7

Please sign in to comment.