diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 3cde86de..847b4071 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -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...') + }) + }) }); diff --git a/package.json b/package.json index 5dc2fea0..a440b1ad 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/constants.ts b/src/constants.ts index 2558705d..604c7cae 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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"), diff --git a/src/git.ts b/src/git.ts index 1367d99a..64b68df3 100644 --- a/src/git.ts +++ b/src/git.ts @@ -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 { 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 { } 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 { `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 { ); /* - 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 { temporaryDeploymentDirectory ); - Promise.resolve("Commit step complete..."); + return Promise.resolve("Commit step complete..."); }