Skip to content

Commit

Permalink
Adds support for custom script parameters (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecampidoglio committed Oct 30, 2020
1 parent d5ad706 commit 49c1c65
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 28 deletions.
130 changes: 130 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import * as core from '@actions/core';
import { when } from 'jest-when';
import * as action from '../src/action';
import { CakeArgument } from '../src/cakeParameter';

jest.mock('@actions/core');

describe('When getting the Cake input arguments from the action', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;

beforeAll(() => {
when(fakeGetInput).calledWith('script-path').mockReturnValue('path/to/script.cake');
when(fakeGetInput).calledWith('cake-version').mockReturnValue('the.version.number');
when(fakeGetInput).calledWith('cake-bootstrap').mockReturnValue('true');
when(fakeGetInput).calledWith('arguments').mockReturnValue('');
});

test('it should return the argument for the script-path parameter', () => {
expect(action.getInputs().scriptPath).toBe('path/to/script.cake');
});

test('it should return the argument for the cake-version parameter', () => {
expect(action.getInputs().cakeVersion).toBe('the.version.number');
});

test('it should return the argument for the cake-bootstrap parameter', () => {
expect(action.getInputs().cakeBootstrap).toBe(true);
});
});

describe('When getting the documented script input arguments from the action', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;

beforeAll(() => {
when(fakeGetInput).calledWith('target').mockReturnValue('Task-To-Run');
when(fakeGetInput).calledWith('verbosity').mockReturnValue('Verbosity-Level');
when(fakeGetInput).calledWith('arguments').mockReturnValue('');
});

test('it should return the argument for the target script parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('target', 'Task-To-Run'));
});

test('it should return the argument for the verbosity script parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('verbosity', 'Verbosity-Level'));
});
});

describe('When getting custom script input arguments from the action', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;

beforeAll(() => {
when(fakeGetInput).calledWith('arguments').mockReturnValue(`
string-parameter: 'value'
numeric-parameter: 3
boolean-parameter: true
`);
});

test('it should return the argument for a custom string parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('string-parameter', '\'value\''));
});

test('it should return the argument for a custom numeric parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('numeric-parameter', '3'));
});

test('it should return the argument for a custom boolean parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('boolean-parameter', 'true'));
});
});

describe('When getting improperly formatted custom script input arguments from the action', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;

beforeAll(() => {
when(fakeGetInput).calledWith('arguments').mockReturnValue(`
--name=value
-name=value
name=value
--name value
-name value
name value
--nameOnly
-nameOnly
nameOnly
`);
});

test('it should not parse the invalid parameter names and values', () => {
expect(action.getInputs().scriptArguments).not.toContainEqual(new CakeArgument('name', 'value'));
});

test('it should not parse the invalid parameter names', () => {
expect(action.getInputs().scriptArguments).not.toContainEqual(new CakeArgument('name', ''));
});
});

describe('When getting no input arguments from the action', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;

beforeAll(() => {
when(fakeGetInput).calledWith('script-path').mockReturnValue('');
when(fakeGetInput).calledWith('cake-version').mockReturnValue('');
when(fakeGetInput).calledWith('cake-bootstrap').mockReturnValue('');
when(fakeGetInput).calledWith('target').mockReturnValue('');
when(fakeGetInput).calledWith('verbosity').mockReturnValue('');
when(fakeGetInput).calledWith('arguments').mockReturnValue('');
});

test('it should return an empty string for the script-path parameter', () => {
expect(action.getInputs().scriptPath).toBe('');
});

test('it should return an empty string for the cake-version parameter', () => {
expect(action.getInputs().cakeVersion).toBe('');
});

test('it should return false for the cake-bootstrap parameter', () => {
expect(action.getInputs().cakeBootstrap).toBe(false);
});

test('it should return an empty string for the target script parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('target', ''));
});

test('it should return an empty string for the verbosity script parameter', () => {
expect(action.getInputs().scriptArguments).toContainEqual(new CakeArgument('verbosity', ''));
});
});
82 changes: 68 additions & 14 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import * as core from '@actions/core';
import { when } from 'jest-when';
import { run } from '../src/main';
import { ToolsDirectory } from '../src/toolsDirectory';
import * as action from '../src/action';
import * as dotnet from '../src/dotnet';
import * as cake from '../src/cake';
import { CakeArgument } from '../src/cakeParameter';

jest.mock('@actions/core');
jest.mock('../src/toolsDirectory');
jest.mock('../src/action');
jest.mock('../src/dotnet');
jest.mock('../src/cake');

describe('When running the action without any input arguments', () => {
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeToolsDirectory = ToolsDirectory as jest.MockedClass<typeof ToolsDirectory>;

beforeAll(() => {
fakeGetInputs.mockReturnValue({
scriptArguments: []
});
});

test('it should create the tools directory', async () => {
await run();
expect(fakeToolsDirectory.prototype.create).toHaveBeenCalled();
Expand Down Expand Up @@ -41,11 +49,14 @@ describe('When running the action without any input arguments', () => {
});

describe('When running the action with the script path input argument', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeRunScript = cake.runScript as jest.MockedFunction<typeof cake.runScript>;

beforeAll(() => {
when(fakeGetInput).calledWith('script-path').mockReturnValue('path/to/script.cake');
fakeGetInputs.mockReturnValue({
scriptPath: 'path/to/script.cake',
scriptArguments: []
});
});

test('it should run the specified Cake script', async () => {
Expand All @@ -55,12 +66,15 @@ describe('When running the action with the script path input argument', () => {
});

describe('When running the action with the Cake version input argument', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeInstallLocalCakeTool =
dotnet.installLocalCakeTool as jest.MockedFunction<typeof dotnet.installLocalCakeTool>;

beforeAll(() => {
when(fakeGetInput).calledWith('cake-version').mockReturnValue('the.version.number');
fakeGetInputs.mockReturnValue({
cakeVersion: 'the.version.number',
scriptArguments: []
});
});

test('it should install the specified version of Cake', async () => {
Expand All @@ -70,11 +84,13 @@ describe('When running the action with the Cake version input argument', () => {
});

describe('When running the action with the target input argument', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeRunScript = cake.runScript as jest.MockedFunction<typeof cake.runScript>;

beforeAll(() => {
when(fakeGetInput).calledWith('target').mockReturnValue('Task-To-Run');
fakeGetInputs.mockReturnValue({
scriptArguments: [new CakeArgument('target', 'Task-To-Run')]
});
});

test('it should run script with the specified target', async () => {
Expand All @@ -85,26 +101,61 @@ describe('When running the action with the target input argument', () => {
});

describe('When running the action with the verbosity input argument', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeRunScript = cake.runScript as jest.MockedFunction<typeof cake.runScript>;

beforeAll(() => {
when(fakeGetInput).calledWith('verbosity').mockReturnValue('Verbosity-Level');
fakeGetInputs.mockReturnValue({
scriptArguments: [new CakeArgument('verbosity', 'Verbosity-Level')]
});
});

test('it should run the script with the specified verbosity level', async () => {
await run();
expect(fakeRunScript.mock.calls[0][3]).toMatchObject(
expect(fakeRunScript.mock.calls[0][2]).toMatchObject(
new CakeArgument('verbosity', 'Verbosity-Level'));
});
});

describe('When running the action with custom script input arguments', () => {
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeRunScript = cake.runScript as jest.MockedFunction<typeof cake.runScript>;

beforeAll(() => {
fakeGetInputs.mockReturnValue({
scriptArguments: [
new CakeArgument('string-parameter', '\'value\''),
new CakeArgument('numeric-parameter', '3'),
new CakeArgument('boolean-parameter', 'true'),
]
});
});

test('it should run the script with the specified custom string parameter', async () => {
await run();
expect(fakeRunScript.mock.calls[0][2]).toMatchObject(
new CakeArgument('string-parameter', '\'value\''));
});

test('it should run the script with the specified custom numeric parameter', async () => {
await run();
expect(fakeRunScript.mock.calls[0][3]).toMatchObject(
new CakeArgument('numeric-parameter', '3'));
});

test('it should run the script with the specified custom boolean parameter', async () => {
await run();
expect(fakeRunScript.mock.calls[0][4]).toMatchObject(
new CakeArgument('boolean-parameter', 'true'));
});
});

describe('When the script fails to run', () => {
const fakeSetFailed = core.setFailed as jest.MockedFunction<typeof core.setFailed>;
const fakeRunScript = cake.runScript as jest.MockedFunction<typeof cake.runScript>;

beforeAll(() => {
when(fakeRunScript).calledWith(expect.anything()).mockImplementation(async () => {
fakeRunScript.mockImplementation(async () => {
throw new Error('the error message');
});
});
Expand All @@ -116,11 +167,15 @@ describe('When the script fails to run', () => {
});

describe('When running the action with the cake-bootstrap input argument', () => {
const fakeGetInput = core.getInput as jest.MockedFunction<typeof core.getInput>;
const fakeGetInputs = action.getInputs as jest.MockedFunction<typeof action.getInputs>;
const fakeBootstrapScript = cake.bootstrapScript as jest.MockedFunction<typeof cake.bootstrapScript>;

beforeAll(() => {
when(fakeGetInput).calledWith('cake-bootstrap').mockReturnValue('true');
fakeGetInputs.mockReturnValue({
scriptPath: 'custom.cake',
cakeBootstrap: true,
scriptArguments: []
});
});

test('it should bootstrap the default Cake script', async () => {
Expand All @@ -129,7 +184,6 @@ describe('When running the action with the cake-bootstrap input argument', () =>
});

test('it should bootstrap the specified Cake script', async () => {
when(fakeGetInput).calledWith('script-path').mockReturnValue('custom.cake');
await run();
expect(fakeBootstrapScript).toHaveBeenCalledWith(
'custom.cake',
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ inputs:
description: 'The verbosity level with which to run the script. Valid values are: Quiet, Minimal, Normal, Verbose or Diagnostic.'
required: false
default: 'Normal'
arguments:
description: 'Any custom parameters to pass to the script. Multiple parameters are defined on separate lines.'
required: false
cake-version:
description: 'The version of Cake to install.'
required: false
Expand Down

0 comments on commit 49c1c65

Please sign in to comment.