2
0
Fork 0
mirror of https://code.forgejo.org/docker/build-push-action.git synced 2025-08-17 17:20:53 +00:00
docker-build-push-action/src/__tests__/blacksmith-builder.test.ts

91 lines
3 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core';
import * as main from '../main';
import * as reporter from '../reporter';
import {getDockerfilePath} from '../context';
import * as setupBuilder from '../setup_builder';
jest.mock('@actions/core', () => ({
debug: jest.fn(),
warning: jest.fn(),
info: jest.fn(),
saveState: jest.fn(),
getState: jest.fn(),
setOutput: jest.fn(),
setFailed: jest.fn(),
error: jest.fn()
}));
jest.mock('../context', () => ({
getDockerfilePath: jest.fn(),
Inputs: jest.fn()
}));
2024-12-20 17:25:34 -05:00
jest.mock('../reporter', () => {
const actual = jest.requireActual('../reporter');
return {
...actual,
reportBuildPushActionFailure: jest.fn().mockResolvedValue(undefined)
2024-12-20 17:25:34 -05:00
};
});
jest.mock('../setup_builder', () => ({
reportBuildStart: jest.fn()
}));
describe('reportBuildMetrics', () => {
let mockInputs;
beforeEach(() => {
jest.clearAllMocks();
mockInputs = {
nofallback: false,
setupOnly: false,
platforms: []
};
});
test('should handle missing dockerfile path', async () => {
(getDockerfilePath as jest.Mock).mockReturnValue(null);
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBeNull();
expect(core.warning).toHaveBeenCalledWith('Error during build metrics reporting: Failed to resolve dockerfile path');
expect(reporter.reportBuildPushActionFailure).toHaveBeenCalledWith(new Error('Failed to resolve dockerfile path'), 'reporting build metrics');
});
test('should successfully report build start', async () => {
const mockBuildId = 'test-build-id';
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
(setupBuilder.reportBuildStart as jest.Mock).mockResolvedValue({ docker_build_id: mockBuildId });
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBe(mockBuildId);
expect(setupBuilder.reportBuildStart).toHaveBeenCalledWith('/path/to/Dockerfile');
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});
test('should handle reportBuildStart returning null', async () => {
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
(setupBuilder.reportBuildStart as jest.Mock).mockResolvedValue(null);
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBeNull();
expect(setupBuilder.reportBuildStart).toHaveBeenCalledWith('/path/to/Dockerfile');
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});
test('should handle error in reportBuildStart', async () => {
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
(setupBuilder.reportBuildStart as jest.Mock).mockRejectedValue(new Error('API error'));
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBeNull();
expect(core.warning).toHaveBeenCalledWith('Error during build metrics reporting: API error');
expect(reporter.reportBuildPushActionFailure).toHaveBeenCalledWith(new Error('API error'), 'reporting build metrics');
});
});