Skip to content

Commit

Permalink
feat: added advanced parameter generation (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaryt authored Sep 9, 2022
1 parent 7504f22 commit 33e9329
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/lib/Components/Parameters/exports/CustomParameter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Generable } from '../..';
import { GenerableType } from '../../../Config/exports/Mapping';
import { CustomParameterShape, CustomParameterContentsShape } from '../types';
import { Command } from '../../Commands/types/Command.types';
import { ReusedExecutor } from '../../Reusable';
import { CustomParameterContentsShape, CustomParameterShape } from '../types';
import { AnyParameterLiteral } from '../types/CustomParameterLiterals.types';

/**
Expand Down Expand Up @@ -42,9 +44,19 @@ export class CustomParameter<ParameterTypeLiteral extends AnyParameterLiteral>
}

generateContents(): CustomParameterContentsShape<ParameterTypeLiteral> {
let defaultValue = this.defaultValue;

if (this.type === 'executor' && defaultValue instanceof ReusedExecutor) {
defaultValue = (this.defaultValue as ReusedExecutor)?.generateContents();
} else if (this.type === 'steps' && Array.isArray(defaultValue)) {
defaultValue = (this.defaultValue as Command[]).map((step) =>
step.generate(),
);
}

return {
type: this.type,
default: this.defaultValue,
default: defaultValue,
description: this.description,
};
}
Expand Down
116 changes: 111 additions & 5 deletions tests/Parameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as CircleCI from '../src/index';
import { DockerExecutor } from '../src/lib/Components/Executors';

describe('Parse yaml pipeline parameters and validate', () => {
const expectedParameters =
describe('Use basic custom parameters', () => {
const parameterList =
new CircleCI.parameters.CustomParametersList<CircleCI.types.parameter.literals.PipelineParameterLiteral>(
[
new CircleCI.parameters.CustomEnumParameter(
Expand All @@ -14,24 +15,129 @@ describe('Parse yaml pipeline parameters and validate', () => {
CircleCI.mapping.ParameterSubtype.INTEGER,
90,
),
new CircleCI.parameters.CustomParameter(
'should-run',
CircleCI.mapping.ParameterSubtype.BOOLEAN,
true,
),
new CircleCI.parameters.CustomParameter(
'message',
CircleCI.mapping.ParameterSubtype.STRING,
'Hello world!',
),
],
);

it('Should have the correct static properties for Custom Enumerable Parameter', () => {
expect(expectedParameters.parameters[0].generableType).toBe(
expect(parameterList.parameters[0].generableType).toBe(
CircleCI.mapping.GenerableType.CUSTOM_ENUM_PARAMETER,
);
});

it('Should have the correct static properties for Custom Parameter List', () => {
expect(expectedParameters.generableType).toBe(
expect(parameterList.generableType).toBe(
CircleCI.mapping.GenerableType.CUSTOM_PARAMETERS_LIST,
);
});

it('Should have the correct static properties for Custom Parameter ', () => {
expect(expectedParameters.parameters[1].generableType).toBe(
expect(parameterList.parameters[1].generableType).toBe(
CircleCI.mapping.GenerableType.CUSTOM_PARAMETER,
);
});

const expectedOutput = {
axis: {
type: 'enum',
enum: ['x', 'y', 'z'],
default: 'x',
},
angle: {
type: 'integer',
default: 90,
},
'should-run': {
type: 'boolean',
default: true,
},
message: {
type: 'string',
default: 'Hello world!',
},
};

it('Should generate expected output', () => {
expect(parameterList.generate()).toEqual(expectedOutput);
});
});

describe('Use advanced custom parameters', () => {
const reusableExecutor = new CircleCI.reusable.ReusableExecutor(
'my-executor',
new DockerExecutor('circleci/node:10.16.0'),
new CircleCI.parameters.CustomParametersList<CircleCI.types.parameter.literals.ExecutorParameterLiteral>(
[
new CircleCI.parameters.CustomParameter(
'resource_class',
CircleCI.mapping.ParameterSubtype.STRING,
'medium',
),
],
),
);

const parameterList =
new CircleCI.parameters.CustomParametersList<CircleCI.types.parameter.literals.JobParameterLiteral>(
[
new CircleCI.parameters.CustomParameter(
'pre-install',
CircleCI.mapping.ParameterSubtype.STEPS,
[
new CircleCI.commands.workspace.Attach({ at: '/tmp' }),
new CircleCI.commands.Checkout(),
],
),
new CircleCI.parameters.CustomParameter(
'executor',
CircleCI.mapping.ParameterSubtype.EXECUTOR,
reusableExecutor.reuse({
resource_class: 'large',
}),
),
new CircleCI.parameters.CustomParameter(
'secret',
CircleCI.mapping.ParameterSubtype.ENV_VAR_NAME,
'SUPER_SECRET',
),
],
);

const expectedOutput = {
'pre-install': {
type: 'steps',
default: [
{
attach_workspace: {
at: '/tmp',
},
},
'checkout',
],
},
executor: {
type: 'executor',
default: {
name: 'my-executor',
resource_class: 'large',
},
},
secret: {
type: 'env_var_name',
default: 'SUPER_SECRET',
},
};

it('Should generate expected output', () => {
expect(parameterList.generate()).toEqual(expectedOutput);
});
});

0 comments on commit 33e9329

Please sign in to comment.