mirror of
https://github.com/JamesIves/github-pages-deploy-action.git
synced 2026-05-31 00:12:03 +08:00
* chore: merge all Dependabot dependency updates with fixes - Update @actions/core/exec/github/io to v3/v9 (ESM-only) - Update @eslint/js to v10, @typescript-eslint/* to 8.58, eslint-plugin-jest/prettier - Update jest 29→30, typescript 5.8→6.0, @types/jest/node, prettier, rimraf, ts-jest - Update lodash 4.17→4.18 (yarn.lock only) - Add CJS test stubs for ESM-only @actions/* packages - Update tsconfig.json: target es2022, add types for node+jest - Update jest.config.js: moduleNameMapper for stubs, remove jest-circus runner - Fix new lint rules: preserve-caught-error and no-useless-assignment Agent-Logs-Url: https://github.com/JamesIves/github-pages-deploy-action/sessions/2a6d68c0-eab5-4c98-a927-2525b124ca87 Co-authored-by: JamesIves <10888441+JamesIves@users.noreply.github.com> * refactor: convert project to proper ESM - package.json: add "type": "module" for ESM runtime + nodenext compilation - tsconfig.json: module/moduleResolution → nodenext (ESM output with .js extensions) - src/*.ts: add .js extensions to all relative imports - __tests__/*.test.ts: add .js to relative imports, remove jest.mock() factories for @actions/* - jest.config.js → jest.config.cjs: CJS jest config, moduleNameMapper strips .js for ts-jest - __mocks__/@actions/*.js + package.json: proper Jest manual mocks (CJS, with functional implementations for integration tests) replacing __tests__/stubs/ workaround - eslint.config.mjs: ignore __mocks__/** instead of __tests__/stubs/** Agent-Logs-Url: https://github.com/JamesIves/github-pages-deploy-action/sessions/c73b8f61-87f7-4eab-8e99-d363a69f6e66 Co-authored-by: JamesIves <10888441+JamesIves@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: JamesIves <10888441+JamesIves@users.noreply.github.com>
498 lines
13 KiB
TypeScript
498 lines
13 KiB
TypeScript
// 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.js'
|
|
import {execute} from '../src/execute.js'
|
|
import {deploy, init} from '../src/git.js'
|
|
import fs from 'fs'
|
|
|
|
const originalAction = JSON.stringify(action)
|
|
|
|
jest.mock('fs', () => ({
|
|
existsSync: jest.fn()
|
|
}))
|
|
|
|
jest.mock('@actions/core')
|
|
|
|
jest.mock('@actions/io')
|
|
|
|
jest.mock('../src/execute', () => ({
|
|
__esModule: true,
|
|
execute: jest.fn(() => ({stdout: '', stderr: ''}))
|
|
}))
|
|
|
|
describe('git', () => {
|
|
afterEach(() => {
|
|
Object.assign(action, JSON.parse(originalAction))
|
|
})
|
|
|
|
describe('init', () => {
|
|
it('should execute commands', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(7)
|
|
})
|
|
|
|
it('should catch when a function throws an error', async () => {
|
|
;(execute as jest.Mock).mockImplementationOnce(() => {
|
|
throw new Error('Mocked throw')
|
|
})
|
|
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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 instanceof Error && 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, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(7)
|
|
})
|
|
|
|
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, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(6)
|
|
|
|
process.env.CI = undefined
|
|
})
|
|
|
|
it('should correctly continue when it cannot remove origin', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(7)
|
|
})
|
|
})
|
|
|
|
describe('deploy', () => {
|
|
it('should execute commands', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(15)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
|
|
it('should not push when asked to dryRun', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(14)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
|
|
it('should execute commands with single commit toggled', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(15)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should execute commands with single commit toggled and existing branch', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(14)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should execute commands with single commit and dryRun toggled', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(14)
|
|
expect(rmRF).toHaveBeenCalledTimes(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, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(15)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
expect(fs.existsSync).toHaveBeenCalledTimes(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, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(12)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
|
|
it('should execute commands with clean options stored as an array', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(12)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should gracefully handle target folder', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
silent: false,
|
|
folder: '.',
|
|
branch: 'branch',
|
|
token: '123',
|
|
pusher: {},
|
|
clean: true,
|
|
targetFolder: 'new_folder',
|
|
commitMessage: 'Hello!',
|
|
isTest: TestFlag.NONE
|
|
})
|
|
|
|
await deploy(action)
|
|
|
|
expect(execute).toHaveBeenCalledTimes(12)
|
|
expect(rmRF).toHaveBeenCalledTimes(1)
|
|
expect(mkdirP).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should stop early if there is nothing to commit', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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).toHaveBeenCalledTimes(12)
|
|
expect(rmRF).toHaveBeenCalledTimes(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, {
|
|
hostname: 'github.com',
|
|
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 instanceof Error && error.message).toBe(
|
|
'The deploy step encountered an error: Mocked throw ❌'
|
|
)
|
|
}
|
|
})
|
|
|
|
it('should execute commands if force is false and retry until limit is exceeded', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
silent: false,
|
|
folder: 'assets',
|
|
branch: 'branch',
|
|
force: false,
|
|
token: '123',
|
|
repositoryName: 'JamesIves/montezuma',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
try {
|
|
await deploy(action)
|
|
} catch (error) {
|
|
expect(error instanceof Error && error.message).toBe(
|
|
'The deploy step encountered an error: Attempt limit exceeded ❌'
|
|
)
|
|
}
|
|
})
|
|
|
|
it('should add a tag to the commit', async () => {
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
silent: false,
|
|
folder: 'assets',
|
|
branch: 'branch',
|
|
token: '123',
|
|
repositoryName: 'JamesIves/montezuma',
|
|
tag: 'v0.1',
|
|
pusher: {
|
|
name: 'asd',
|
|
email: 'as@cat'
|
|
},
|
|
isTest: TestFlag.HAS_CHANGED_FILES
|
|
})
|
|
|
|
const response = await deploy(action)
|
|
expect(execute).toHaveBeenCalledTimes(17)
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
|
|
it('should silently handle chmod failures on read-only folders', async () => {
|
|
let chmodCallCount = 0
|
|
;(execute as jest.Mock).mockImplementation((cmd: string) => {
|
|
// Simulate chmod failures for read-only folders
|
|
if (cmd.includes('chmod -R +rw')) {
|
|
chmodCallCount++
|
|
throw new Error('Operation not permitted')
|
|
}
|
|
return {stdout: '', stderr: ''}
|
|
})
|
|
|
|
Object.assign(action, {
|
|
hostname: 'github.com',
|
|
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)
|
|
|
|
// Verify that chmod was attempted twice (once for folderPath, once for temporaryDeploymentDirectory)
|
|
expect(chmodCallCount).toBe(2)
|
|
// Verify deployment still succeeds despite chmod failures
|
|
expect(response).toBe(Status.SUCCESS)
|
|
})
|
|
})
|
|
})
|