From 33e9329c8cdab633e7ce94038b0ed02c7cb7d0a4 Mon Sep 17 00:00:00 2001 From: Jaryt Bustard Date: Fri, 9 Sep 2022 16:05:37 -0400 Subject: [PATCH] feat: added advanced parameter generation (#138) --- .../Parameters/exports/CustomParameter.ts | 16 ++- tests/Parameters.test.ts | 116 +++++++++++++++++- 2 files changed, 125 insertions(+), 7 deletions(-) diff --git a/src/lib/Components/Parameters/exports/CustomParameter.ts b/src/lib/Components/Parameters/exports/CustomParameter.ts index 0b04a52..f0f9929 100644 --- a/src/lib/Components/Parameters/exports/CustomParameter.ts +++ b/src/lib/Components/Parameters/exports/CustomParameter.ts @@ -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'; /** @@ -42,9 +44,19 @@ export class CustomParameter } generateContents(): CustomParameterContentsShape { + 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, }; } diff --git a/tests/Parameters.test.ts b/tests/Parameters.test.ts index 9feb8f2..fceb235 100644 --- a/tests/Parameters.test.ts +++ b/tests/Parameters.test.ts @@ -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( [ new CircleCI.parameters.CustomEnumParameter( @@ -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( + [ + new CircleCI.parameters.CustomParameter( + 'resource_class', + CircleCI.mapping.ParameterSubtype.STRING, + 'medium', + ), + ], + ), + ); + + const parameterList = + new CircleCI.parameters.CustomParametersList( + [ + 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); + }); });