More Test Coverage / Imps

This commit is contained in:
JamesIves 2020-01-19 16:16:28 -05:00
parent 109bba8860
commit 4e8c644240
9 changed files with 99 additions and 37 deletions

View File

@ -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...");
});

59
__tests__/main.test.ts Normal file
View File

@ -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);
});
});

View File

@ -9,5 +9,6 @@ module.exports = {
},
verbose: true,
setupFiles: ["<rootDir>/__tests__/env.js"],
collectCoverage: true
collectCoverage: true,
collectCoverageFrom: ['src/*.ts','!src/constants.ts']
}

View File

@ -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

View File

@ -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();
}

View File

@ -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();

View File

@ -40,7 +40,7 @@ export const action = {
// Token Types
export const tokenType = action.ssh
? "SSH"
? "SSH Deploy Key"
: action.accessToken
? "Access Token"
: action.gitHubToken

View File

@ -23,7 +23,7 @@ export async function init(): Promise<any> {
"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<any> {
`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<any> {
export async function generateBranch(): Promise<any> {
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<any> {
`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();
}

View File

@ -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();