2024-12-08 14:19:31 -05:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as main from '../main';
|
|
|
|
import * as reporter from '../reporter';
|
|
|
|
import {getDockerfilePath} from '../context';
|
|
|
|
|
|
|
|
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,
|
2025-08-01 14:40:43 -04:00
|
|
|
reportBuildPushActionFailure: jest.fn().mockResolvedValue(undefined),
|
|
|
|
reportBuild: jest.fn()
|
2024-12-20 17:25:34 -05:00
|
|
|
};
|
|
|
|
});
|
2024-12-08 14:19:31 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
describe('reportBuildMetrics', () => {
|
2024-12-08 14:19:31 -05:00
|
|
|
let mockInputs;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2025-04-09 17:30:07 -04:00
|
|
|
mockInputs = {
|
|
|
|
setupOnly: false,
|
|
|
|
platforms: []
|
|
|
|
};
|
2024-12-08 14:19:31 -05:00
|
|
|
});
|
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
test('should handle missing dockerfile path', async () => {
|
2024-12-08 14:19:31 -05:00
|
|
|
(getDockerfilePath as jest.Mock).mockReturnValue(null);
|
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
const result = await main.reportBuildMetrics(mockInputs);
|
2024-12-08 14:19:31 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
expect(result).toBeNull();
|
2025-08-03 15:33:27 -04:00
|
|
|
expect(core.warning).toHaveBeenCalledWith('Error when reporting build metrics: Failed to resolve dockerfile path');
|
|
|
|
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
|
2024-12-08 14:19:31 -05:00
|
|
|
});
|
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
test('should successfully report build start', async () => {
|
|
|
|
const mockBuildId = 'test-build-id';
|
2024-12-08 14:19:31 -05:00
|
|
|
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
|
2025-08-03 15:33:27 -04:00
|
|
|
(reporter.reportBuild as jest.Mock).mockResolvedValue({docker_build_id: mockBuildId});
|
2024-12-08 14:19:31 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
const result = await main.reportBuildMetrics(mockInputs);
|
2024-12-08 14:19:31 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
expect(result).toBe(mockBuildId);
|
2025-08-01 14:40:43 -04:00
|
|
|
expect(reporter.reportBuild).toHaveBeenCalledWith('/path/to/Dockerfile');
|
2025-08-01 14:25:57 -04:00
|
|
|
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
|
2024-12-08 14:19:31 -05:00
|
|
|
});
|
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
test('should handle reportBuildStart returning null', async () => {
|
2024-12-08 14:19:31 -05:00
|
|
|
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
|
2025-08-01 14:40:43 -04:00
|
|
|
(reporter.reportBuild as jest.Mock).mockResolvedValue(null);
|
2024-12-08 14:19:31 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
const result = await main.reportBuildMetrics(mockInputs);
|
2024-12-08 16:36:08 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
expect(result).toBeNull();
|
2025-08-01 14:40:43 -04:00
|
|
|
expect(reporter.reportBuild).toHaveBeenCalledWith('/path/to/Dockerfile');
|
2024-12-08 19:19:24 -05:00
|
|
|
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
|
2024-12-08 16:36:08 -05:00
|
|
|
});
|
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
test('should handle error in reportBuildStart', async () => {
|
2024-12-08 16:36:08 -05:00
|
|
|
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
|
2025-08-01 14:40:43 -04:00
|
|
|
(reporter.reportBuild as jest.Mock).mockRejectedValue(new Error('API error'));
|
2024-12-08 16:36:08 -05:00
|
|
|
|
2025-08-01 14:25:57 -04:00
|
|
|
const result = await main.reportBuildMetrics(mockInputs);
|
|
|
|
|
|
|
|
expect(result).toBeNull();
|
2025-08-01 14:40:43 -04:00
|
|
|
expect(core.warning).toHaveBeenCalledWith('Error reporting build start: API error');
|
|
|
|
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
|
2024-12-08 16:36:08 -05:00
|
|
|
});
|
2024-12-08 14:19:31 -05:00
|
|
|
});
|