github-pages-deploy-action/__tests__/git.test.ts

425 lines
10 KiB
TypeScript
Raw Normal View History

// Initial env variable setup for tests.
2020-03-05 21:19:45 +08:00
process.env['INPUT_FOLDER'] = 'build'
process.env['GITHUB_SHA'] = '123'
import {mkdirP, rmRF} from '@actions/io'
2020-05-15 05:56:56 +08:00
import {action, Status} from '../src/constants'
2020-03-05 21:19:45 +08:00
import {execute} from '../src/execute'
import {deploy, generateBranch, init, switchToBaseBranch} from '../src/git'
import fs from 'fs'
2020-03-05 21:19:45 +08:00
const originalAction = JSON.stringify(action)
2020-03-05 21:19:45 +08:00
jest.mock('@actions/core', () => ({
setFailed: jest.fn(),
2020-03-28 22:35:26 +08:00
getInput: jest.fn(),
isDebug: jest.fn(),
info: jest.fn()
2020-03-05 21:19:45 +08:00
}))
jest.mock('@actions/io', () => ({
rmRF: jest.fn(),
mkdirP: jest.fn()
}))
2020-03-05 21:19:45 +08:00
jest.mock('../src/execute', () => ({
2020-10-17 23:47:44 +08:00
__esModule: true,
2020-10-17 23:48:06 +08:00
execute: jest.fn()
2020-03-05 21:19:45 +08:00
}))
2020-03-05 21:19:45 +08:00
describe('git', () => {
afterEach(() => {
2020-03-05 21:19:45 +08:00
Object.assign(action, JSON.parse(originalAction))
})
2020-03-05 21:19:45 +08:00
describe('init', () => {
it('should stash changes if preserve is true', async () => {
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
accessToken: '123',
branch: 'branch',
folder: '.',
preserve: true,
isTest: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
await init(action)
expect(execute).toBeCalledTimes(7)
})
2020-10-17 23:47:44 +08:00
it('should catch when a function throws an error', async () => {
2020-10-17 23:48:06 +08:00
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
})
2020-10-17 23:47:44 +08:00
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
accessToken: '123',
branch: 'branch',
folder: '.',
preserve: true,
isTest: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
try {
await init(action)
} catch (error) {
2020-10-17 23:48:06 +08:00
expect(error.message).toBe(
'There was an error initializing the repository: Mocked throw ❌'
)
2020-10-17 23:47:44 +08:00
}
})
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
describe('generateBranch', () => {
it('should execute six commands', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-03-05 21:19:45 +08:00
accessToken: '123',
branch: 'branch',
folder: '.',
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
}
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
await generateBranch(action)
expect(execute).toBeCalledTimes(6)
})
2020-10-17 23:47:44 +08:00
it('should catch when a function throws an error', async () => {
2020-10-17 23:48:06 +08:00
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
})
2020-10-17 23:47:44 +08:00
Object.assign(action, {
silent: false,
accessToken: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
}
})
try {
await generateBranch(action)
} catch (error) {
2020-10-17 23:48:06 +08:00
expect(error.message).toBe(
'There was an error creating the deployment branch: There was an error switching to the base branch: Mocked throw ❌ ❌'
)
2020-10-17 23:47:44 +08:00
}
})
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
describe('switchToBaseBranch', () => {
it('should execute one command', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-03-05 21:19:45 +08:00
accessToken: '123',
branch: 'branch',
folder: '.',
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
}
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
await switchToBaseBranch(action)
expect(execute).toBeCalledTimes(1)
})
2020-03-05 21:19:45 +08:00
it('should execute one command if using custom baseBranch', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-03-05 21:19:45 +08:00
baseBranch: '123',
accessToken: '123',
branch: 'branch',
folder: '.',
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
}
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
await switchToBaseBranch(action)
expect(execute).toBeCalledTimes(1)
})
2020-10-17 23:47:44 +08:00
it('should catch when a function throws an error', async () => {
2020-10-17 23:48:06 +08:00
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
})
2020-10-17 23:47:44 +08:00
Object.assign(action, {
silent: false,
baseBranch: '123',
accessToken: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
}
})
try {
await switchToBaseBranch(action)
} catch (error) {
2020-10-17 23:48:06 +08:00
expect(error.message).toBe(
'There was an error switching to the base branch: Mocked throw ❌'
)
2020-10-17 23:47:44 +08:00
}
})
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
describe('deploy', () => {
it('should execute commands', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-06-06 22:55:49 +08:00
folder: 'assets',
2020-03-05 21:19:45 +08:00
branch: 'branch',
gitHubToken: '123',
2020-09-13 05:33:19 +08:00
lfs: true,
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
}
2020-03-05 21:19:45 +08:00
})
2020-05-15 05:56:56 +08:00
const response = await deploy(action)
// Includes the call to generateBranch
2020-09-13 05:33:19 +08:00
expect(execute).toBeCalledTimes(13)
expect(rmRF).toBeCalledTimes(1)
2020-05-15 05:56:56 +08:00
expect(response).toBe(Status.SUCCESS)
2020-03-05 21:19:45 +08:00
})
it('should execute stash apply commands if preserve is true', async () => {
Object.assign(action, {
silent: false,
folder: 'assets',
2020-10-18 00:23:52 +08:00
folderPath: 'assets',
branch: 'branch',
gitHubToken: '123',
lfs: true,
preserve: true,
isTest: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
const response = await deploy(action)
// Includes the call to generateBranch
expect(execute).toBeCalledTimes(14)
expect(rmRF).toBeCalledTimes(1)
expect(response).toBe(Status.SUCCESS)
})
2020-10-18 00:23:52 +08:00
it('should appropriately move along if git stash errors', async () => {
;(execute as jest.Mock).mockImplementation(cmd => {
if (cmd === 'git stash apply') {
// Mocks the case where git stash apply errors.
throw new Error()
}
})
Object.assign(action, {
silent: false,
folder: 'assets',
2020-10-18 00:23:52 +08:00
folderPath: 'assets',
branch: 'branch',
gitHubToken: '123',
2020-10-18 00:23:52 +08:00
lfs: true,
preserve: true,
isTest: true,
pusher: {
name: 'asd',
email: 'as@cat'
2020-10-18 00:23:52 +08:00
}
})
const response = await deploy(action)
// Includes the call to generateBranch
2020-10-18 00:23:52 +08:00
expect(execute).toBeCalledTimes(14)
expect(rmRF).toBeCalledTimes(1)
expect(response).toBe(Status.SUCCESS)
})
it('should execute commands with single commit toggled', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-10-18 00:23:52 +08:00
folder: 'other',
folderPath: 'other',
branch: 'branch',
gitHubToken: '123',
singleCommit: true,
pusher: {
name: 'asd',
email: 'as@cat'
2020-10-18 00:23:52 +08:00
},
clean: true
})
await deploy(action)
// Includes the call to generateBranch
2020-05-25 00:38:27 +08:00
expect(execute).toBeCalledTimes(18)
expect(rmRF).toBeCalledTimes(1)
})
2020-10-18 00:23:52 +08:00
it('should not ignore CNAME or nojekyll if they exist in the deployment folder', async () => {
Object.assign(action, {
silent: false,
folder: 'assets',
folderPath: 'assets',
branch: 'branch',
gitHubToken: '123',
pusher: {
name: 'asd',
email: 'as@cat'
},
clean: true
})
fs.createWriteStream('assets/.nojekyll')
fs.createWriteStream('assets/CNAME')
const response = await deploy(action)
// Includes the call to generateBranch
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
expect(response).toBe(Status.SUCCESS)
})
2020-03-05 21:19:45 +08:00
it('should execute commands with clean options, ommits sha commit message', async () => {
process.env.GITHUB_SHA = ''
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-10-18 00:23:52 +08:00
folder: 'other',
folderPath: 'other',
2020-03-05 21:19:45 +08:00
branch: 'branch',
gitHubToken: '123',
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
},
clean: true,
2020-10-18 00:23:52 +08:00
cleanExclude: '["cat", "montezuma"]',
workspace: 'other'
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
await deploy(action)
// Includes the call to generateBranch
2020-05-25 00:38:27 +08:00
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
it('should execute commands with clean options stored as an array instead', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-06-06 22:55:49 +08:00
folder: 'assets',
2020-10-18 00:23:52 +08:00
folderPath: 'assets',
2020-03-05 21:19:45 +08:00
branch: 'branch',
gitHubToken: '123',
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
},
clean: true,
2020-03-05 21:19:45 +08:00
cleanExclude: ['cat', 'montezuma']
})
2020-03-05 21:19:45 +08:00
await deploy(action)
// Includes the call to generateBranch
2020-05-25 00:38:27 +08:00
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
it('should gracefully handle incorrectly formatted clean exclude items', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-03-05 21:19:45 +08:00
folder: '.',
branch: 'branch',
gitHubToken: '123',
2020-01-20 05:16:28 +08:00
pusher: {},
clean: true,
2020-03-05 21:19:45 +08:00
targetFolder: 'new_folder',
commitMessage: 'Hello!',
2020-01-20 05:16:28 +08:00
isTest: true,
cleanExclude: '["cat, "montezuma"]' // There is a syntax errror in the string.
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
await deploy(action)
2020-05-25 00:38:27 +08:00
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
expect(mkdirP).toBeCalledTimes(1)
2020-03-05 21:19:45 +08:00
})
2020-03-05 21:19:45 +08:00
it('should stop early if there is nothing to commit', async () => {
Object.assign(action, {
2020-06-26 20:18:23 +08:00
silent: false,
2020-06-06 22:55:49 +08:00
folder: 'assets',
2020-03-05 21:19:45 +08:00
branch: 'branch',
gitHubToken: '123',
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
},
isTest: false // Setting this env variable to false means there will never be anything to commit and the action will exit early.
2020-03-05 21:19:45 +08:00
})
2020-05-15 05:56:56 +08:00
const response = await deploy(action)
2020-05-25 00:20:25 +08:00
expect(execute).toBeCalledTimes(13)
expect(rmRF).toBeCalledTimes(1)
2020-05-15 05:56:56 +08:00
expect(response).toBe(Status.SKIPPED)
2020-03-05 21:19:45 +08:00
})
2020-10-17 23:47:44 +08:00
it('should catch when a function throws an error', async () => {
2020-10-17 23:48:06 +08:00
;(execute as jest.Mock).mockImplementationOnce(() => {
throw new Error('Mocked throw')
})
2020-10-17 23:47:44 +08:00
Object.assign(action, {
silent: false,
folder: 'assets',
branch: 'branch',
gitHubToken: '123',
lfs: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
try {
await deploy(action)
} catch (error) {
2020-10-17 23:48:06 +08:00
expect(error.message).toBe(
'The deploy step encountered an error: Mocked throw ❌'
)
2020-10-17 23:47:44 +08:00
}
})
2020-03-05 21:19:45 +08:00
})
})