feat: added support for providing optional checksums for ZIP validation (#107)

Signed-off-by: Kim <kim.peter.wagner@gmail.com>
This commit is contained in:
Kim Wagner authored and GitHub committed 2026-04-13 16:46:38 -03:00
1 parent 03a9076efe
commit 4bcfe8cded
6 files changed
+153 -7

No files matched your search

+63 -1
View File
@@ -14,6 +14,7 @@ import { join } from 'path';
// Set up mocks BEFORE dynamic imports
jest.unstable_mockModule('@actions/core', () => ({
getInput: jest.fn(),
getMultilineInput: jest.fn(),
setOutput: jest.fn(),
setFailed: jest.fn(),
debug: jest.fn(),
@@ -41,12 +42,17 @@ jest.unstable_mockModule('../releases.js', () => ({
Release: jest.fn()
}));
jest.unstable_mockModule('../util.js', () => ({
fileSHA256: jest.fn()
}));
// Dynamic imports pick up the mocks
const { getInput, warning } = await import('@actions/core');
const { getInput, getMultilineInput, warning } = await import('@actions/core');
const tc = await import('@actions/tool-cache');
const io = await import('@actions/io');
const { getRelease } = await import('../releases.js');
const { default: setup } = await import('../setup-tofu.js');
const { fileSHA256 } = await import('../util.js');
// Set up global test fixtures
const fallbackVersion = 'latest';
@@ -94,6 +100,13 @@ describe('setup-tofu', () => {
return defaults[name] || '';
});
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: []
};
return defaults[name] || [];
});
// Mock environment variables
process.env.GITHUB_TOKEN = 'mock-github-token';
});
@@ -278,4 +291,53 @@ describe('setup-tofu', () => {
expect(tc.cacheDir).not.toHaveBeenCalled();
});
});
describe('checksum validation', () => {
const mockChecksum = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6';
const mockChecksum2 = 'fcfac0a865b57e0711cb8bae47c8fc70ad93198fa6146a40020ea459715fda83';
const mockInvalidChecksum = 'invalidchecksuminvalidchecksuminvalidchecksuminvalidchecksum';
fileSHA256.mockResolvedValue(mockChecksum);
it('should validate checksum when checksums are provided', async () => {
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: [mockInvalidChecksum, mockChecksum]
};
return defaults[name] ?? [];
});
await setup();
expect(fileSHA256).toHaveBeenCalled();
});
it('should not validate checksum when no checksums are provided', async () => {
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: []
};
return defaults[name] ?? [];
});
await setup();
expect(fileSHA256).not.toHaveBeenCalled();
});
it('should throw error when checksum validation fails', async () => {
fileSHA256.mockResolvedValue(mockInvalidChecksum);
getMultilineInput.mockImplementation((name) => {
const defaults = {
checksums: [mockChecksum, mockChecksum2]
};
return defaults[name] ?? [];
});
await expect(setup()).rejects.toMatchObject(new Error(
`Failed to validate OpenTofu CLI zip checksum. Received: ${mockInvalidChecksum}. Valid checksums: ${mockChecksum}, ${mockChecksum2}`));
expect(fileSHA256).toHaveBeenCalled();
});
});
});