Files
actions-hugo/__tests__/installer.test.ts
T
Shohei Ueda 187a5efe81 feat: support renamed Hugo release assets (#687)
## Summary
- Support multiple Hugo release asset names for Linux, macOS, and
Windows so renamed upstream assets no longer fail with a plain 404.
- Preserve compatibility with legacy Hugo assets that used filename
forms such as `hugo_v0.20.3_*`, macOS `.zip` archives, and
`Linux_ARM`-style names.
- Add retry handling for missing candidate assets and macOS `.pkg`
extraction via `pkgutil --expand-full`, with focused URL and installer
tests plus the rebuilt `lib/index.js` bundle.

## References
- Fixes https://github.com/peaceiris/actions-hugo/issues/652
- `hugo-version: latest` still resolves through Homebrew; this change
does not fall back to an older Hugo version when the resolved release
has no compatible asset.
- Review note: `lib/index.js` is generated by `npm run build` and
contains bundled dependency code.

## Test plan
- [x] `RUNNER_TEMP=/tmp npm run all`
- [x] `npm run build`


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Tests**
* Broadened test coverage for asset URL generation, download retry
behavior, and extraction across OSes and architectures.

* **Bug Fixes**
* More resilient installer with multiple candidate download URLs and
retry handling for transient failures.
* Improved extraction logic to correctly handle platform- and
format-specific archives.

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/peaceiris/actions-hugo/pull/687)
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Codex <noreply@openai.com>
2026-05-10 22:40:55 +09:00

137 lines
4.2 KiB
TypeScript

import * as tc from '@actions/tool-cache';
import * as io from '@actions/io';
import * as exec from '@actions/exec';
import path from 'path';
import {downloadHugoAsset, extractHugoAsset, isWindowsAsset} from '../src/installer';
jest.mock('@actions/tool-cache', () => ({
downloadTool: jest.fn(),
extractTar: jest.fn(),
extractZip: jest.fn()
}));
jest.mock('@actions/io', () => ({
mv: jest.fn()
}));
jest.mock('@actions/exec', () => ({
exec: jest.fn()
}));
describe('downloadHugoAsset()', () => {
const mockedTC = tc as jest.Mocked<typeof tc>;
beforeEach(() => {
jest.clearAllMocks();
});
test('retry 404 candidates and return the first downloaded asset', async () => {
mockedTC.downloadTool
.mockRejectedValueOnce(new Error('Unexpected HTTP response: 404'))
.mockResolvedValueOnce('/tmp/hugo');
const result = await downloadHugoAsset([
'https://example.com/missing.tar.gz',
'https://example.com/hugo.tar.gz'
]);
expect(result).toEqual({
path: '/tmp/hugo',
url: 'https://example.com/hugo.tar.gz'
});
expect(mockedTC.downloadTool).toHaveBeenCalledTimes(2);
});
test('throw a clear error when all candidates return 404', async () => {
mockedTC.downloadTool.mockRejectedValue(new Error('Unexpected HTTP response: 404'));
await expect(
downloadHugoAsset([
'https://example.com/missing-1.tar.gz',
'https://example.com/missing-2.tar.gz'
])
).rejects.toThrow('Unable to find a compatible Hugo release asset');
});
test('rethrow non-404 download failures without retrying', async () => {
const error = new Error('socket hang up');
mockedTC.downloadTool.mockRejectedValueOnce(error);
await expect(
downloadHugoAsset(['https://example.com/hugo.tar.gz', 'https://example.com/fallback.tar.gz'])
).rejects.toBe(error);
expect(mockedTC.downloadTool).toHaveBeenCalledTimes(1);
});
});
describe('extractHugoAsset()', () => {
const mockedTC = tc as jest.Mocked<typeof tc>;
const mockedIO = io as jest.Mocked<typeof io>;
const mockedExec = exec as jest.Mocked<typeof exec>;
beforeEach(() => {
jest.clearAllMocks();
});
test('extract a tar.gz asset', async () => {
mockedTC.extractTar.mockResolvedValue('/tmp/extracted');
await extractHugoAsset('/tmp/tool', 'https://example.com/hugo.tar.gz', '/tmp/temp', '/tmp/bin');
expect(mockedTC.extractTar).toHaveBeenCalledWith('/tmp/tool', '/tmp/temp');
expect(mockedIO.mv).toHaveBeenCalledWith(path.join('/tmp/extracted', 'hugo'), '/tmp/bin');
});
test('extract a zip asset', async () => {
mockedTC.extractZip.mockResolvedValue('/tmp/extracted');
await extractHugoAsset(
'/tmp/tool',
'https://example.com/hugo_0.58.2_Windows-64bit.zip',
'/tmp/temp',
'/tmp/bin'
);
expect(mockedTC.extractZip).toHaveBeenCalledWith('/tmp/tool', '/tmp/temp');
expect(mockedIO.mv).toHaveBeenCalledWith(path.join('/tmp/extracted', 'hugo.exe'), '/tmp/bin');
});
test('extract a macOS zip asset', async () => {
mockedTC.extractZip.mockResolvedValue('/tmp/extracted');
await extractHugoAsset(
'/tmp/tool',
'https://example.com/hugo_0.20.2_macOS-64bit.zip',
'/tmp/temp',
'/tmp/bin'
);
expect(mockedTC.extractZip).toHaveBeenCalledWith('/tmp/tool', '/tmp/temp');
expect(mockedIO.mv).toHaveBeenCalledWith(path.join('/tmp/extracted', 'hugo'), '/tmp/bin');
});
test('extract a macOS pkg asset', async () => {
mockedExec.exec.mockResolvedValue(0);
await extractHugoAsset('/tmp/tool', 'https://example.com/hugo.pkg', '/tmp/temp', '/tmp/bin');
expect(mockedExec.exec).toHaveBeenCalledWith('pkgutil', [
'--expand-full',
'/tmp/tool',
path.join('/tmp/temp', 'pkg')
]);
expect(mockedIO.mv).toHaveBeenCalledWith(
path.join('/tmp/temp', 'pkg', 'Payload', 'hugo'),
'/tmp/bin'
);
});
});
describe('isWindowsAsset()', () => {
test('detect Windows asset URLs', () => {
expect(isWindowsAsset('https://example.com/hugo_0.58.2_Windows-64bit.zip')).toBe(true);
expect(isWindowsAsset('https://example.com/hugo_0.119.0_windows-amd64.zip')).toBe(true);
expect(isWindowsAsset('https://example.com/hugo_0.20.2_macOS-64bit.zip')).toBe(false);
});
});