Files
github-pages-deploy-action/__tests__/worktree.test.ts
Copilot 70f9f52642 chore: merge all open Dependabot dependency updates (#1967)
* 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>
2026-04-05 14:33:48 -04:00

216 lines
5.9 KiB
TypeScript

import {rmRF} from '@actions/io'
import {TestFlag} from '../src/constants.js'
import {generateWorktree} from '../src/worktree.js'
import {execute} from '../src/execute.js'
import fs from 'fs'
import os from 'os'
import path from 'path'
jest.mock('@actions/core')
/*
Test generateWorktree against a known git repository.
The upstream repository `origin` is set up once for the test suite,
and for each test run, a new clone is created.
See worktree.error.test.ts for testing mocked errors from git.
*/
describe('generateWorktree', () => {
let tempdir: string | null = null
let clonedir: string | null = null
beforeAll(async () => {
// Set up origin repository
const silent = true
tempdir = fs.mkdtempSync(path.join(os.tmpdir(), 'gh-deploy-'))
const origin = path.join(tempdir, 'origin')
await execute('git init origin', tempdir, silent)
await execute('git config user.email "you@example.com"', origin, silent)
await execute('git config user.name "Jane Doe"', origin, silent)
await execute('git checkout -b main', origin, silent)
fs.writeFileSync(path.join(origin, 'f1'), 'hello world\n')
await execute('git add .', origin, silent)
await execute('git commit -mc0', origin, silent)
fs.writeFileSync(path.join(origin, 'f1'), 'hello world\nand planets\n')
await execute('git add .', origin, silent)
await execute('git commit -mc1', origin, silent)
await execute('git checkout --orphan gh-pages', origin, silent)
await execute('git reset --hard', origin, silent)
await fs.promises.writeFile(path.join(origin, 'gh1'), 'pages content\n')
await execute('git add .', origin, silent)
await execute('git commit -mgh0', origin, silent)
await fs.promises.writeFile(
path.join(origin, 'gh1'),
'pages content\ngoes on\n'
)
await execute('git add .', origin, silent)
await execute('git commit -mgh1', origin, silent)
})
beforeEach(async () => {
// Clone origin to our workspace for each test
const silent = true
clonedir = path.join(tempdir as string, 'clone')
await execute('git init clone', tempdir as string, silent)
await execute('git config user.email "you@example.com"', clonedir, silent)
await execute('git config user.name "Jane Doe"', clonedir, silent)
await execute(
`git remote add origin ${path.join(tempdir as string, 'origin')}`,
clonedir,
silent
)
await execute('git fetch --depth=1 origin main', clonedir, silent)
await execute('git checkout main', clonedir, silent)
})
afterEach(async () => {
// Tear down workspace
await rmRF(clonedir as string)
})
afterAll(async () => {
// Tear down origin repository
if (tempdir) {
await rmRF(tempdir)
}
})
describe('with existing branch and new commits', () => {
it('should check out the latest commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: false,
branch: 'gh-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
true
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries.sort((a, b) => a.localeCompare(b))).toEqual([
'.git',
'gh1'
])
const commitMessages = await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
expect(commitMessages.stdout).toBe('gh1')
})
})
describe('with missing branch and new commits', () => {
it('should create initial commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: false,
branch: 'no-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
false
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries).toEqual(['.git'])
const commitMessages = await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
expect(commitMessages.stdout).toBe('Initial no-pages commit')
})
})
describe('with existing branch and singleCommit', () => {
it('should check out the latest commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: true,
branch: 'gh-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
true
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries.sort((a, b) => a.localeCompare(b))).toEqual([
'.git',
'gh1'
])
return expect(async () => {
await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
}).rejects.toThrow()
})
})
describe('with missing branch and singleCommit', () => {
it('should create initial commit', async () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: true,
branch: 'no-pages',
folder: '',
silent: true,
isTest: TestFlag.NONE
},
'worktree',
false
)
const dirEntries = await fs.promises.readdir(
path.join(workspace, 'worktree')
)
expect(dirEntries).toEqual(['.git'])
return expect(async () => {
await execute(
'git log --format=%s',
path.join(workspace, 'worktree'),
true
)
}).rejects.toThrow()
})
})
})