mirror of
https://github.com/JamesIves/github-pages-deploy-action.git
synced 2023-12-15 20:03:39 +08:00
4e40ddd3f5
* Add a build step to create lib and node_modules artifact * Run integration test with built dist and current SHA as base For pull requests, the github.sha is the sha of the merge to the target branch, not the head of the PR. Special case that. * Use v2 checkout, and DRY_RUN for the integration test. I also made the branches more generic, as there are now more of them. * Fix #536, don't push at all on dryRun Also add tests for dryRun and singleCommit and generateBranch code flows. * Try to fix dryRun on new remote branches, refactor fetch * Try to fix dryRun, only fetch if origin branch exists * Refactor worktree setup to include branch generation and setup for singleCommit This is a continuation of the no-checkout work, and sadly suggested pretty intensive changes. * Set up git config to fix tests, also make debugging easier * Add matrix for existing and non-existing branch * Add matrix for singleCommit and not * Drop GITHUB_TOKEN, add DRY_RUN to action.yml * When deploying existing branch, add a modifcation and deploy again * Force branch checkout to work in redeployment scenarios * Make singleCommit easier to see in job descriptions * Review comments * Add a test-only property to action to test code paths with remote branch. * Introduce TestFlag enum to signal different test scenarios to unit tests * Fix util.test.ts
321 lines
8.1 KiB
TypeScript
321 lines
8.1 KiB
TypeScript
/* eslint-disable import/first */
|
|
// Initial env variable setup for tests.
|
|
process.env['INPUT_FOLDER'] = 'build'
|
|
process.env['GITHUB_SHA'] = '123'
|
|
|
|
import {mkdirP, rmRF} from '@actions/io'
|
|
import {action, Status, TestFlag} from '../src/constants'
|
|
import {execute} from '../src/execute'
|
|
import {deploy, init} from '../src/git'
|
|
import fs from 'fs'
|
|
|
|
const originalAction = JSON.stringify(action)
|
|
|
|
jest.mock('@actions/core', () => ({
|
|
setFailed: jest.fn(),
|
|
getInput: jest.fn(),
|
|
isDebug: jest.fn(),
|
|
info: jest.fn()
|
|
}))
|
|
|
|
jest.mock('@actions/io', () => ({
|
|
rmRF: jest.fn(),
|
|
mkdirP: jest.fn()
|
|
}))
|
|
|
|
jest.mock('../src/execute', () => ({
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
__esModule: true,
|
|
execute: jest.fn()
|
|
}))
|
|
|
|
describe('git', () => {
|
|
afterEach(() => {
|
|
Object.assign(action, JSON.parse(originalAction))
|
|
})
|
|
|
|
describe('init', () => {
|
|
it('should catch when a function throws an error', async () => {
|
|
;(execute as jest.Mock).mockImplementationOnce(() => {
|
|
throw new Error('Mocked throw')
|
|
})
|
|
|
|
Object.assign(action, {
|
|
silent: false,
|
|
repositoryPath: 'JamesIves/github-pages-deploy-action',
|
|
token: '123',
|
|
branch: 'branch',
|
|
folder: '.',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
try {
|
|
await init(action)
|
|
} catch (error) {
|
|
expect(error.message).toBe(
|
|
'There was an error initializing the repository: Mocked throw ❌'
|
|
)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('deploy', () => {
|
|
it('should execute commands', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'assets',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
const response = await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(11)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
|
|
it('should not push when asked to dryRun', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
dryRun: true,
|
|
folder: 'assets',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
const response = await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(10)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
|
|
it('should execute commands with single commit toggled', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'other',
|
|
folderPath: 'other',
|
|
branch: 'branch',
|
|
token: '123',
|
|
singleCommit: true,
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
clean: true,
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(10)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
})
|
|
|
|
it('should execute commands with single commit toggled and existing branch', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'other',
|
|
folderPath: 'other',
|
|
branch: 'branch',
|
|
token: '123',
|
|
singleCommit: true,
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
clean: true,
|
|
isTest: TestFlag.HAS_CHANGED_FILES | TestFlag.HAS_REMOTE_BRANCH
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(9)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
})
|
|
|
|
it('should execute commands with single commit and dryRun toggled', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'other',
|
|
folderPath: 'other',
|
|
branch: 'branch',
|
|
gitHubToken: '123',
|
|
singleCommit: true,
|
|
dryRun: true,
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
clean: true,
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(9)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
})
|
|
|
|
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',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
clean: true,
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
fs.createWriteStream('assets/.nojekyll')
|
|
fs.createWriteStream('assets/CNAME')
|
|
|
|
const response = await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(11)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
|
|
it('should execute commands with clean options, commits sha commit message', async () => {
|
|
process.env.GITHUB_SHA = ''
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'other',
|
|
folderPath: 'other',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
clean: true,
|
|
cleanExclude: '["cat", "montezuma"]',
|
|
workspace: 'other',
|
|
isTest: TestFlag.NONE
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(8)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
})
|
|
|
|
it('should execute commands with clean options stored as an array instead', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'assets',
|
|
folderPath: 'assets',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
clean: true,
|
|
cleanExclude: ['cat', 'montezuma'],
|
|
isTest: TestFlag.NONE
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
// Includes the call to generateWorktree
|
|
expect(execute).toBeCalledTimes(8)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
})
|
|
|
|
it('should gracefully handle incorrectly formatted clean exclude items', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: '.',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {},
|
|
clean: true,
|
|
targetFolder: 'new_folder',
|
|
commitMessage: 'Hello!',
|
|
isTest: TestFlag.NONE,
|
|
cleanExclude: '["cat, "montezuma"]' // There is a syntax errror in the string.
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
expect(execute).toBeCalledTimes(8)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
expect(mkdirP).toBeCalledTimes(1)
|
|
})
|
|
|
|
it('should stop early if there is nothing to commit', async () => {
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'assets',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.NONE // Setting this flag to None means there will never be anything to commit and the action will exit early.
|
|
})
|
|
|
|
const response = await deploy(action)
|
|
expect(execute).toBeCalledTimes(8)
|
|
expect(rmRF).toBeCalledTimes(1)
|
|
expect(response).toBe(Status.SKIPPED)
|
|
})
|
|
|
|
it('should catch when a function throws an error', async () => {
|
|
;(execute as jest.Mock).mockImplementationOnce(() => {
|
|
throw new Error('Mocked throw')
|
|
})
|
|
|
|
Object.assign(action, {
|
|
silent: false,
|
|
folder: 'assets',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
try {
|
|
await deploy(action)
|
|
} catch (error) {
|
|
expect(error.message).toBe(
|
|
'The deploy step encountered an error: Mocked throw ❌'
|
|
)
|
|
}
|
|
})
|
|
})
|
|
})
|