Skip to content

Commit

Permalink
feat: add addEnvVar to Executor
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleTryon committed Sep 1, 2022
1 parent f94b9a0 commit 08cc7d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/lib/Components/Executors/exports/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class Executor<
> implements Generable
{
resource_class: ResourceClass;
parameters?: ExecutableParameters;
parameters: ExecutableParameters;

/**
* @param resource_class - The resource class of the environment
Expand All @@ -29,7 +29,7 @@ export abstract class Executor<
parameters?: Exclude<ExecutableParameters, 'resource_class'>,
) {
this.resource_class = resource_class;
this.parameters = parameters;
this.parameters = parameters || {};
}
abstract get generableType(): GenerableType;
abstract get executorLiteral(): ExecutorLiteral;
Expand All @@ -52,4 +52,25 @@ export abstract class Executor<
): ReusableExecutor {
return new ReusableExecutor(name, this, parameters);
}

/**
* Add an environment variable to the Executor.
* This will be set in plain-text via the exported config file.
* Consider using project-level environment variables or a context for sensitive information.
* @see {@link https://circleci.com/docs/env-vars}
* @example
* ```
* myExecutor.addEnvVar('MY_VAR', 'my value');
* ```
*/
addEnvVar(name: string, value: string): this {
if (!this.parameters.environment) {
this.parameters.environment = {
[name]: value,
};
} else {
this.parameters.environment[name] = value;
}
return this;
}
}
14 changes: 14 additions & 0 deletions tests/Executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ describe('Instantiate Docker Executor', () => {
resource_class: 'medium',
};

const dockerWithEnv = new CircleCI.executors.DockerExecutor('cimg/node:lts');
dockerWithEnv.addEnvVar('MY_VAR', 'my value');
const expectedShapeWithEnv = {
docker: [{ image: 'cimg/node:lts' }],
resource_class: 'medium',
environment: {
MY_VAR: 'my value',
},
};

it('Should match the expected output', () => {
expect(docker.generate()).toEqual(expectedShape);
});

it('Should match the expected output with env var', () => {
expect(dockerWithEnv.generate()).toEqual(expectedShapeWithEnv);
});

it('Docker executor should be instance of an Executor', () => {
expect(docker instanceof CircleCI.executors.Executor).toBeTruthy();
});
Expand Down

0 comments on commit 08cc7d8

Please sign in to comment.