diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 39812deb..88961097 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -183,7 +183,7 @@ describe("git", () => { }); describe("deploy", () => { - it("should execute five commands", async () => { + it("should execute commands", async () => { Object.assign(action, { build: "build", branch: "branch", @@ -197,11 +197,11 @@ describe("git", () => { const call = await deploy(); // Includes the call to generateBranch - expect(execute).toBeCalledTimes(18); + expect(execute).toBeCalledTimes(12); expect(call).toBe("Commit step complete..."); }); - it("should execute five commands with clean options", async () => { + it("should execute commands with clean options", async () => { Object.assign(action, { build: "build", branch: "branch", @@ -217,27 +217,26 @@ describe("git", () => { const call = await deploy(); // Includes the call to generateBranch - expect(execute).toBeCalledTimes(18); + expect(execute).toBeCalledTimes(12); expect(call).toBe("Commit step complete..."); }); it("should gracefully handle incorrectly formatted clean exclude items", async () => { Object.assign(action, { - build: "build", + build: ".", branch: "branch", gitHubToken: "123", - pusher: { - name: "asd", - email: "as@cat" - }, + pusher: {}, clean: true, + targetFolder: "new_folder", + commitMessage: "Hello!", + isTest: true, cleanExclude: '["cat, "montezuma"]' // There is a syntax errror in the string. }); const call = await deploy(); - // Includes the call to generateBranch - expect(execute).toBeCalledTimes(18); + expect(execute).toBeCalledTimes(12); expect(call).toBe("Commit step complete..."); }); diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts new file mode 100644 index 00000000..72d1bdf3 --- /dev/null +++ b/__tests__/main.test.ts @@ -0,0 +1,59 @@ +// Initial env variable setup for tests. +process.env["INPUT_FOLDER"] = "build"; +process.env["GITHUB_SHA"] = "123"; + +import { action } from "../src/constants"; +import main from "../src/main"; +import { execute } from "../src/execute"; +import { setFailed } from "@actions/core"; + +const originalAction = JSON.stringify(action); + +jest.mock("../src/execute", () => ({ + execute: jest.fn() +})); + +jest.mock("@actions/core", () => ({ + setFailed: jest.fn(), + getInput: jest.fn() +})); + +describe("main", () => { + afterEach(() => { + Object.assign(action, JSON.parse(originalAction)); + }); + + it("should run through the commands", async () => { + Object.assign(action, { + build: "build", + branch: "branch", + gitHubToken: "123", + pusher: { + name: "asd", + email: "as@cat" + }, + isTest: true + }); + await main(); + expect(execute).toBeCalledTimes(30); + }); + + it("should throw if an error is encountered", async () => { + Object.assign(action, { + build: "build", + branch: "branch", + baseBranch: "master", + gitHubToken: null, + ssh: null, + accessToken: null, + pusher: { + name: "asd", + email: "as@cat" + }, + isTest: true + }); + await main(); + expect(execute).toBeCalledTimes(12); + expect(setFailed).toBeCalledTimes(1); + }); +}); diff --git a/jest.config.js b/jest.config.js index ae3bf419..9eaa7c54 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,5 +9,6 @@ module.exports = { }, verbose: true, setupFiles: ["/__tests__/env.js"], - collectCoverage: true + collectCoverage: true, + collectCoverageFrom: ['src/*.ts','!src/constants.ts'] } \ No newline at end of file diff --git a/lib/constants.js b/lib/constants.js index c6920843..c8106a78 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -42,7 +42,7 @@ exports.action = { }; // Token Types exports.tokenType = exports.action.ssh - ? "SSH" + ? "SSH Deploy Key" : exports.action.accessToken ? "Access Token" : exports.action.gitHubToken diff --git a/lib/git.js b/lib/git.js index 461c108a..7ce745a2 100644 --- a/lib/git.js +++ b/lib/git.js @@ -23,11 +23,11 @@ function init() { util_1.isNullOrUndefined(constants_1.action.gitHubToken) && util_1.isNullOrUndefined(constants_1.action.ssh)) { core_1.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true."); - throw "No deployment token/method was provided."; + throw Error("No deployment token/method was provided."); } if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) { core_1.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`); - throw "Incorrectly formatted build folder."; + throw Error("Incorrectly formatted build folder."); } console.log(`Deploying using ${constants_1.tokenType}... 🔑`); yield execute_1.execute(`git init`, constants_1.workspace); @@ -63,7 +63,7 @@ function generateBranch() { return __awaiter(this, void 0, void 0, function* () { try { if (util_1.isNullOrUndefined(constants_1.action.branch)) { - throw "Branch is required."; + throw Error("Branch is required."); } console.log(`Creating ${constants_1.action.branch} branch... 🔧`); yield switchToBaseBranch(); @@ -94,7 +94,7 @@ function deploy() { If the branch doesn't exist it gets created here as an orphan. */ const branchExists = yield execute_1.execute(`git ls-remote --heads ${constants_1.repositoryPath} ${constants_1.action.branch} | wc -l`, constants_1.workspace); - if (!branchExists) { + if (!branchExists && !constants_1.action.isTest) { console.log("Deployment branch does not exist. Creating...."); yield generateBranch(); } diff --git a/lib/main.js b/lib/main.js index f9741828..c5ae5a32 100644 --- a/lib/main.js +++ b/lib/main.js @@ -8,29 +8,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); -const core = __importStar(require("@actions/core")); +const core_1 = require("@actions/core"); const git_1 = require("./git"); /** Initializes and runs the action. */ -(function () { +function main() { return __awaiter(this, void 0, void 0, function* () { try { yield git_1.init(); yield git_1.deploy(); } catch (error) { + /* istanbul ignore next */ console.log("The deployment encountered an error. ❌"); - core.setFailed(error.message); + /* istanbul ignore next */ + core_1.setFailed(error); } finally { console.log("Completed Deployment ✅"); } }); -})(); +} +exports.default = main; +// Init +main(); diff --git a/src/constants.ts b/src/constants.ts index fa972c37..cb4af14c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -40,7 +40,7 @@ export const action = { // Token Types export const tokenType = action.ssh - ? "SSH" + ? "SSH Deploy Key" : action.accessToken ? "Access Token" : action.gitHubToken diff --git a/src/git.ts b/src/git.ts index 8a381d9c..ba70d192 100644 --- a/src/git.ts +++ b/src/git.ts @@ -23,7 +23,7 @@ export async function init(): Promise { "You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true." ); - throw "No deployment token/method was provided."; + throw Error("No deployment token/method was provided."); } if (action.build.startsWith("/") || action.build.startsWith("./")) { @@ -31,7 +31,7 @@ export async function init(): Promise { `The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.` ); - throw "Incorrectly formatted build folder."; + throw Error("Incorrectly formatted build folder."); } console.log(`Deploying using ${tokenType}... 🔑`); @@ -68,7 +68,7 @@ export async function switchToBaseBranch(): Promise { export async function generateBranch(): Promise { try { if (isNullOrUndefined(action.branch)) { - throw "Branch is required."; + throw Error("Branch is required."); } console.log(`Creating ${action.branch} branch... 🔧`); @@ -102,7 +102,7 @@ export async function deploy(): Promise { `git ls-remote --heads ${repositoryPath} ${action.branch} | wc -l`, workspace ); - if (!branchExists) { + if (!branchExists && !action.isTest) { console.log("Deployment branch does not exist. Creating...."); await generateBranch(); } diff --git a/src/main.ts b/src/main.ts index 3e940487..04440e81 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,15 +1,20 @@ -import * as core from "@actions/core"; +import { setFailed } from "@actions/core"; import { init, deploy } from "./git"; /** Initializes and runs the action. */ -(async function() { +export default async function main() { try { await init(); await deploy(); } catch (error) { + /* istanbul ignore next */ console.log("The deployment encountered an error. ❌"); - core.setFailed(error.message); + /* istanbul ignore next */ + setFailed(error); } finally { console.log("Completed Deployment ✅"); } -})(); +} + +// Init +main();