Skip to content

Commit 77ec396

Browse files
committed
[ENG-14822][steps] Coerce env in steps to string
1 parent c6d4fae commit 77ec396

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

packages/steps/src/BuildConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ const BuildFunctionCallSchema = Joi.object({
188188
name: Joi.string(),
189189
workingDirectory: Joi.string(),
190190
shell: Joi.string(),
191-
env: Joi.object().pattern(Joi.string(), Joi.string().allow('')),
191+
env: Joi.object().pattern(
192+
Joi.string(),
193+
Joi.alternatives()
194+
.try(Joi.number(), Joi.string().allow(''))
195+
.custom((value) => String(value))
196+
),
192197
if: Joi.string(),
193198
timeout_minutes: Joi.number().positive(),
194199
// Internal field for metrics collection. Not documented publicly.

packages/steps/src/__tests__/BuildConfig-test.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ describe(validateConfig, () => {
258258

259259
expect(() => {
260260
validateConfig(BuildConfigSchema, buildConfig);
261-
}).toThrowError(/"build.steps\[0\].run.env.ENV1" must be a string/);
261+
}).toThrowError(/"build.steps\[0\].run.env.ENV1" must be one of \[number, string\]/);
262262
});
263263
test('invalid env type', () => {
264264
const buildConfig = {
@@ -278,7 +278,27 @@ describe(validateConfig, () => {
278278

279279
expect(() => {
280280
validateConfig(BuildConfigSchema, buildConfig);
281-
}).toThrowError(/"build.steps\[0\].run.env.ENV1" must be a string/);
281+
}).toThrowError(/"build.steps\[0\].run.env.ENV1" must be one of \[number, string\]/);
282+
});
283+
test('env number coerced to string', () => {
284+
const buildConfig = {
285+
build: {
286+
steps: [
287+
{
288+
run: {
289+
command: 'echo 123',
290+
env: {
291+
HOMEBREW_NO_AUTO_UPDATE: 1,
292+
},
293+
},
294+
},
295+
],
296+
},
297+
};
298+
299+
const config = validateConfig(BuildConfigSchema, buildConfig);
300+
assert(isBuildStepCommandRun(config.build.steps[0]));
301+
expect(config.build.steps[0].run.env).toEqual({ HOMEBREW_NO_AUTO_UPDATE: '1' });
282302
});
283303
test('valid timeout_minutes', () => {
284304
const buildConfig = {
@@ -471,6 +491,36 @@ describe(validateConfig, () => {
471491
validateConfig(BuildConfigSchema, buildConfig);
472492
}).not.toThrowError();
473493
});
494+
test('call with env number coerced to string', () => {
495+
const buildConfig = {
496+
build: {
497+
steps: [
498+
{
499+
say_hi: {
500+
env: {
501+
HOMEBREW_NO_AUTO_UPDATE: 1,
502+
PORT: 3000,
503+
VERBOSE: 'true',
504+
},
505+
},
506+
},
507+
],
508+
},
509+
functions: {
510+
say_hi: {
511+
command: 'echo "Hi!"',
512+
},
513+
},
514+
};
515+
516+
const config = validateConfig(BuildConfigSchema, buildConfig);
517+
const step = config.build.steps[0] as BuildStepFunctionCall;
518+
expect(step['say_hi'].env).toEqual({
519+
HOMEBREW_NO_AUTO_UPDATE: '1',
520+
PORT: '3000',
521+
VERBOSE: 'true',
522+
});
523+
});
474524
test('call with if statement', () => {
475525
const buildConfig = {
476526
build: {
@@ -518,7 +568,7 @@ describe(validateConfig, () => {
518568

519569
expect(() => {
520570
validateConfig(BuildConfigSchema, buildConfig);
521-
}).toThrowError(/"build.steps\[0\].say_hi.env.ENV1" must be a string/);
571+
}).toThrowError(/"build.steps\[0\].say_hi.env.ENV1" must be one of \[number, string\]/);
522572
});
523573
test('invalid env structure', () => {
524574
const buildConfig = {
@@ -544,7 +594,7 @@ describe(validateConfig, () => {
544594

545595
expect(() => {
546596
validateConfig(BuildConfigSchema, buildConfig);
547-
}).toThrowError(/"build.steps\[0\].say_hi.env.ENV1" must be a string/);
597+
}).toThrowError(/"build.steps\[0\].say_hi.env.ENV1" must be one of \[number, string\]/);
548598
});
549599
test('call with inputs boolean', () => {
550600
const buildConfig = {

0 commit comments

Comments
 (0)