Finish up unit tests

This commit is contained in:
James Ives 2019-11-08 10:34:29 -05:00
parent 712ece2f34
commit f6cce8dd71
4 changed files with 111 additions and 30 deletions

View File

@ -1,19 +1,13 @@
// Initial env variable setup for tests.
process.env["INPUT_FOLDER"] = "build";
import { execute } from "../src/util";
import { init } from "../src/git";
import { init, generateBranch, deploy } from "../src/git";
import {action} from '../src/constants'
import {cp} from '@actions/io';
import _ from 'lodash';
jest.mock("../src/constants", () => ({
build: 'dist',
action: {
pusher: {
name: 'montezuma',
email: 'best@cat',
},
gitHubToken: 'exists',
}
}));
const originalAction = _.cloneDeep(action);
jest.mock("../src/util", () => ({
execute: jest.fn()
@ -24,16 +18,104 @@ jest.mock("@actions/io", () => ({
}));
describe("git", () => {
afterEach(() => {
_.assignIn(action, originalAction);
});
describe("init", () => {
it("should execute three commands", async () => {
it("should execute three commands if a GitHub token is provided", async () => {
await init();
Object.assign(action, {
build: 'build',
gitHubToken: '123',
pusher: {
name: 'asd',
email: 'as@cat'
}})
const call = await init();
expect(execute).toBeCalledTimes(3);
expect(call).toBe('Initialization step complete...')
});
it("should fail if the dpeloyment folder begins with /", async () => {
// TODO:
it("should execute three commands if a Access token is provided", async () => {
Object.assign(action, {
build: 'build',
accessToken: '123',
pusher: {
name: 'asd',
email: 'as@cat'
}})
const call = await init();
expect(execute).toBeCalledTimes(3);
expect(call).toBe('Initialization step complete...')
});
it("should fail if there is no provided GitHub Token or Access Token", async () => {
Object.assign(action, {
build: 'build',
pusher: {
name: 'asd',
email: 'as@cat'
}})
const call = await init()
expect(execute).toBeCalledTimes(0);
expect(call).toBe('Initialization step complete...')
})
it("should fail if the build folder begins with a /", async () => {
Object.assign(action, {
accessToken: '123',
build: '/',
pusher: {
name: 'asd',
email: 'as@cat'
}})
const call = await init()
expect(execute).toBeCalledTimes(0);
expect(call).toBe('Initialization step complete...')
})
it("should fail if the build folder begins with a ./", async () => {
Object.assign(action, {
accessToken: '123',
build: './',
pusher: {
name: 'asd',
email: 'as@cat'
}})
const call = await init()
expect(execute).toBeCalledTimes(0);
expect(call).toBe('Initialization step complete...')
})
});
describe('generateBranch', () => {
it('should execute five commands', async () => {
const call = await generateBranch();
expect(execute).toBeCalledTimes(5);
expect(call).toBe('Deployment branch creation step complete...')
})
})
describe('deploy', () => {
it('should execute five commands', async () => {
const call = await deploy();
// Includes the call to generateBranch
expect(execute).toBeCalledTimes(13);
expect(cp).toBeCalledTimes(1)
expect(call).toBe('Commit step complete...')
})
})
});

View File

@ -42,9 +42,10 @@
"@types/node": "^12.11.0",
"jest": "^24.8.0",
"jest-circus": "^24.7.1",
"ts-jest": "^24.0.2",
"typescript": "^3.5.1",
"lodash": "^4.17.15",
"prettier": "^1.18.2",
"tslint": "^5.20.0"
"ts-jest": "^24.0.2",
"tslint": "^5.20.0",
"typescript": "^3.5.1"
}
}

View File

@ -5,11 +5,9 @@ const { pusher, repository } = github.context.payload;
export const workspace: any = process.env.GITHUB_WORKSPACE;
// The build folder that stores the deployment data.
export const build = core.getInput("FOLDER", { required: true });
// Required action data.
export const action = {
build: core.getInput("FOLDER", { required: true }),
gitHubRepository: repository ? repository.full_name : "",
gitHubToken: core.getInput("GITHUB_TOKEN"),
accessToken: core.getInput("ACCESS_TOKEN"),

View File

@ -1,7 +1,7 @@
import * as core from "@actions/core";
import { cp } from "@actions/io";
import { execute } from "./util";
import { workspace, build, action, repositoryPath } from "./constants";
import { workspace, action, repositoryPath } from "./constants";
/** Generates the branch if it doesn't exist on the remote.
* @returns {Promise}
@ -9,13 +9,13 @@ import { workspace, build, action, repositoryPath } from "./constants";
export async function init(): Promise<any> {
try {
if (!action.accessToken && !action.gitHubToken) {
core.setFailed(
return core.setFailed(
"You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy."
);
}
if (build.startsWith("/") || build.startsWith("./")) {
core.setFailed(
if (action.build.startsWith("/") || action.build.startsWith("./")) {
return core.setFailed(
`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`
);
}
@ -26,7 +26,7 @@ export async function init(): Promise<any> {
} catch (error) {
core.setFailed(`There was an error initializing the repository: ${error}`);
} finally {
Promise.resolve("Initializion step complete...");
return Promise.resolve("Initialization step complete...");
}
}
@ -49,7 +49,7 @@ export async function generateBranch(): Promise<any> {
`There was an error creating the deployment branch: ${error}`
);
} finally {
Promise.resolve("Deployment branch creation step complete...");
return Promise.resolve("Deployment branch creation step complete...");
}
}
@ -82,9 +82,9 @@ export async function deploy(): Promise<any> {
);
/*
Pushes all of the build files into the deployment directory.
Pushes all of the action.build files into the deployment directory.
Allows the user to specify the root if '.' is provided. */
await cp(`${build}/.`, temporaryDeploymentDirectory, {
await cp(`${action.build}/.`, temporaryDeploymentDirectory, {
recursive: true,
force: true
});
@ -104,5 +104,5 @@ export async function deploy(): Promise<any> {
temporaryDeploymentDirectory
);
Promise.resolve("Commit step complete...");
return Promise.resolve("Commit step complete...");
}