github-pages-deploy-action/__tests__/git.test.ts
James Ives 442182742b
[Release] Version 4 (#589)
* Stop checking out workspace (#515)

* Stop checking out base branch before deployment, drop option.

* Don't check out default branch, as we don't check out base branch, drop option.

* Don't stash/unstash as we don't update the workdir, drop preserve option.

* Don't init the workspace

* Only fetch the remote branch if it exists, only with depth 1.

* Rely on previous checkouts to have handled lfs files correctly, drop option.

* Update README, action.yml, integration tests

* Set up eslint for test files. (#517)

* Add DRY_RUN option, passing --dry-run to git push. (#526)

See #499 for the proposal.

* Simplifies Token Setup (#530)

* Token simplification

* Access Token / Github Token -> Token

* Oops

* Typos

* Update README.md

* Update README.md

* Update action.yml

Co-authored-by: Axel Hecht <axel@pike.org>

* Update README.md

Co-authored-by: Axel Hecht <axel@pike.org>

* Update README.md

Co-authored-by: Axel Hecht <axel@pike.org>

* Adjust codeql action to latest recommendations (#540)

Also, add the dev and release branches, and drop master.

* Add workflow to update build and node_modules on release branches (#541)

* Stores username/email in secrets

* Removing stale bot integration

* Test current code base as an integration test for PRs and pushes (#505)

* 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

* Update worktree.ts

* Fix a few nits in tests and automation. Don't try to wordcount ls-rem… (#546)

* Fix a few nits in tests and automation. Don't try to wordcount ls-remote.

Nits in tests are around undoing changes made to the environment,
and to not modify the checkout.

* Describe suite with empty SHA

* Lowercase Inputs (#547)

* Lowercases inputs

* Adjusts workflow tests and deployment_status

* Use multi-line string for clean-exclude patterns. (#553)

As this change is subtle, I'm taking the opportunity to change
the underscore for the hyphen, which makes it less likely that
users of this action will just pass in an old json array.

* Hyphenate inputs and outputs, add step output, fix #558 (#559)

* Hyphenate inputs and outputs, add step output, fix #558

I've also tried to make the clean docs a bit clearer, and consistent
about clean being on my default. Still not totally happy with the intro
of the docs there, though.

* Add testing of step outputs to build integration tests

* Security Docs

* Integration tests

* Revert "Integration tests"

This reverts commit 639ff537d5.

* Native SSH Key Support (#569)

* SSH Key Support 🔑

* Update ssh.ts

* Update src/ssh.ts

Co-authored-by: Axel Hecht <axel@pike.org>

* README fixes/etc

* Unit Tests & README

* ssh key

* Update README.md

* Update ssh.test.ts

* Update ssh.test.ts

* Update ssh.test.ts

* Update ssh.test.ts

* Update ssh.test.ts

* Update ssh.test.ts

* Update integration.yml

Co-authored-by: Axel Hecht <axel@pike.org>

* Deployment Issues (#583)

* Update git.ts

* Tests

* Update git.ts

* Formatting

* Update src/git.ts

Co-authored-by: Axel Hecht <axel@pike.org>

* TestFlag

* Logging

* Update git.ts

Co-authored-by: Axel Hecht <axel@pike.org>

* Codespace Support (#584)

* Add files via upload

* Update README.md

* Add files via upload

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* SSH Issues (#588)

* Unsets Persisted Credentials (#587)

* Persist

* Config Setup/Tests

* Assets

* Update git.ts

* Spacing

* Update integration.yml

* Update README.md

Co-authored-by: Axel Hecht <axel@pike.org>
2021-02-05 22:05:18 -05:00

414 lines
10 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('fs', () => ({
existsSync: jest.fn()
}))
jest.mock('@actions/core', () => ({
setFailed: jest.fn(),
getInput: jest.fn(),
setOutput: 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 execute commands', async () => {
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
})
await init(action)
expect(execute).toBeCalledTimes(5)
})
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 ❌'
)
}
})
it('should correctly continue when it cannot unset a git config value', async () => {
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.UNABLE_TO_UNSET_GIT_CONFIG
})
await init(action)
expect(execute).toBeCalledTimes(5)
})
it('should not unset git config if a user is using ssh', async () => {
// Sets and unsets the CI condition.
process.env.CI = 'true'
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
sshKey: true,
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: false
})
await init(action)
expect(execute).toBeCalledTimes(4)
process.env.CI = undefined
})
it('should correctly continue when it cannot remove origin', async () => {
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
branch: 'branch',
folder: '.',
pusher: {
name: 'asd',
email: 'as@cat'
},
isTest: TestFlag.UNABLE_TO_REMOVE_ORIGIN
})
await init(action)
expect(execute).toBeCalledTimes(5)
})
})
describe('deploy', () => {
it('should execute commands', async () => {
Object.assign(action, {
silent: false,
folder: 'assets',
branch: 'branch',
token: '123',
repositoryName: 'JamesIves/montezuma',
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 () => {
;(fs.existsSync as jest.Mock)
.mockImplementationOnce(() => {
return true
})
.mockImplementationOnce(() => {
return true
})
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
})
const response = await deploy(action)
// Includes the call to generateWorktree
expect(execute).toBeCalledTimes(11)
expect(rmRF).toBeCalledTimes(1)
expect(fs.existsSync).toBeCalledTimes(2)
expect(response).toBe(Status.SUCCESS)
})
describe('with empty GITHUB_SHA', () => {
const oldSha = process.env.GITHUB_SHA
afterAll(() => {
process.env.GITHUB_SHA = oldSha
})
it('should execute commands with clean options', 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,
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', 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 target folder', async () => {
Object.assign(action, {
silent: false,
folder: '.',
branch: 'branch',
token: '123',
pusher: {},
clean: true,
targetFolder: 'new_folder',
commitMessage: 'Hello!',
isTest: TestFlag.NONE
})
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 ❌'
)
}
})
})
})