2
0
Fork 0
mirror of https://code.forgejo.org/docker/build-push-action.git synced 2025-08-11 17:10:54 +00:00
docker-build-push-action/src/__tests__/blacksmith-builder.test.ts
Claude 99fce0de47 fix: resolve TypeScript build errors and lint issues
- Fix axios-retry type compatibility issues in reporter.ts
- Remove unused imports (Context, Metric_MetricType)
- Update test expectations to match current implementation
- Fix ESLint errors and apply formatting

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-03 15:33:27 -04:00

85 lines
2.7 KiB
TypeScript

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()
}));
jest.mock('../reporter', () => {
const actual = jest.requireActual('../reporter');
return {
...actual,
reportBuildPushActionFailure: jest.fn().mockResolvedValue(undefined),
reportBuild: jest.fn()
};
});
describe('reportBuildMetrics', () => {
let mockInputs;
beforeEach(() => {
jest.clearAllMocks();
mockInputs = {
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 when reporting build metrics: Failed to resolve dockerfile path');
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});
test('should successfully report build start', async () => {
const mockBuildId = 'test-build-id';
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
(reporter.reportBuild as jest.Mock).mockResolvedValue({docker_build_id: mockBuildId});
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBe(mockBuildId);
expect(reporter.reportBuild).toHaveBeenCalledWith('/path/to/Dockerfile');
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});
test('should handle reportBuildStart returning null', async () => {
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
(reporter.reportBuild as jest.Mock).mockResolvedValue(null);
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBeNull();
expect(reporter.reportBuild).toHaveBeenCalledWith('/path/to/Dockerfile');
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});
test('should handle error in reportBuildStart', async () => {
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
(reporter.reportBuild as jest.Mock).mockRejectedValue(new Error('API error'));
const result = await main.reportBuildMetrics(mockInputs);
expect(result).toBeNull();
expect(core.warning).toHaveBeenCalledWith('Error reporting build start: API error');
expect(reporter.reportBuildPushActionFailure).not.toHaveBeenCalled();
});
});