mirror of
https://github.com/docker/build-push-action.git
synced 2025-05-07 05:59:31 +02:00
*: refactor methods to support mocking
Additionally, write some tests to ensure the driver method `startBlacksmithBuilder` handles all exceptions correctly in both nofallback=true and nofallback=false configurations.
This commit is contained in:
parent
15e5beff2d
commit
c71ad2dbef
15 changed files with 712 additions and 1380 deletions
80
src/__tests__/blacksmith-builder.test.ts
Normal file
80
src/__tests__/blacksmith-builder.test.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as main from '../main';
|
||||
import * as reporter from '../reporter';
|
||||
import {getDockerfilePath} from '../context';
|
||||
import { getBuilderAddr } 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()
|
||||
}));
|
||||
|
||||
jest.mock('../reporter', () => ({
|
||||
reportBuilderCreationFailed: jest.fn().mockResolvedValue(undefined)
|
||||
}));
|
||||
|
||||
jest.mock('../setup_builder', () => ({
|
||||
getBuilderAddr: jest.fn()
|
||||
}));
|
||||
|
||||
describe('startBlacksmithBuilder', () => {
|
||||
let mockInputs;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
mockInputs = {nofallback: false};
|
||||
});
|
||||
|
||||
test('should handle missing dockerfile path with nofallback=false', async () => {
|
||||
(getDockerfilePath as jest.Mock).mockReturnValue(null);
|
||||
|
||||
const result = await main.startBlacksmithBuilder(mockInputs);
|
||||
|
||||
expect(result).toEqual({addr: null, buildId: null, exposeId: ''});
|
||||
expect(core.warning).toHaveBeenCalledWith('Error during Blacksmith builder setup: Failed to resolve dockerfile path. Falling back to a local build.');
|
||||
expect(reporter.reportBuilderCreationFailed).toHaveBeenCalledWith(new Error('Failed to resolve dockerfile path'));
|
||||
});
|
||||
|
||||
test('should handle missing dockerfile path with nofallback=true', async () => {
|
||||
(getDockerfilePath as jest.Mock).mockReturnValue(null);
|
||||
mockInputs.nofallback = true;
|
||||
|
||||
await expect(main.startBlacksmithBuilder(mockInputs)).rejects.toThrow('Failed to resolve dockerfile path');
|
||||
expect(core.warning).not.toHaveBeenCalled();
|
||||
expect(reporter.reportBuilderCreationFailed).toHaveBeenCalledWith(new Error('Failed to resolve dockerfile path'));
|
||||
});
|
||||
|
||||
test('should handle error in getBuilderAddr with nofallback=false', async () => {
|
||||
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
|
||||
(getBuilderAddr as jest.Mock).mockRejectedValue(new Error('Failed to obtain Blacksmith builder'));
|
||||
|
||||
mockInputs.nofallback = false;
|
||||
const result = await main.startBlacksmithBuilder(mockInputs);
|
||||
|
||||
expect(result).toEqual({addr: null, buildId: null, exposeId: ''});
|
||||
expect(core.warning).toHaveBeenCalledWith('Error during Blacksmith builder setup: Failed to obtain Blacksmith builder. Falling back to a local build.');
|
||||
expect(reporter.reportBuilderCreationFailed).toHaveBeenCalledWith(new Error('Failed to obtain Blacksmith builder'));
|
||||
});
|
||||
|
||||
test('should handle error in getBuilderAddr with nofallback=true', async () => {
|
||||
(getDockerfilePath as jest.Mock).mockReturnValue('/path/to/Dockerfile');
|
||||
const error = new Error('Failed to obtain Blacksmith builder');
|
||||
(getBuilderAddr as jest.Mock).mockRejectedValue(error);
|
||||
mockInputs.nofallback = true;
|
||||
|
||||
await expect(main.startBlacksmithBuilder(mockInputs)).rejects.toThrow(error);
|
||||
expect(core.warning).not.toHaveBeenCalled();
|
||||
expect(reporter.reportBuilderCreationFailed).toHaveBeenCalledWith(error);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue