From a5096eee299a93d070a6ae68f33082c059663c56 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Mon, 18 Jun 2018 17:06:42 +0200 Subject: [PATCH] feat(generic): add env var support to generic module type --- src/plugins/container.ts | 9 +++++++-- src/plugins/generic.ts | 17 ++++++++++++++--- src/util/util.ts | 6 ++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/plugins/container.ts b/src/plugins/container.ts index c8045379c0..137b758ec5 100644 --- a/src/plugins/container.ts +++ b/src/plugins/container.ts @@ -11,6 +11,7 @@ import * as childProcess from "child-process-promise" import { Module, ModuleConfig, + ModuleSpec, } from "../types/module" import { LogSymbolType } from "../logger/types" import { @@ -46,6 +47,7 @@ import { genericModuleSpecSchema, GenericModuleSpec, GenericTestSpec, + genericTestSchema, } from "./generic" export interface ServiceEndpointSpec { @@ -176,15 +178,16 @@ const serviceSchema = baseServiceSchema .description("List of volumes that should be mounted when deploying the container."), }) -export interface ContainerModuleSpec extends GenericModuleSpec { +export interface ContainerModuleSpec extends ModuleSpec { buildArgs: PrimitiveMap, image?: string, services: ContainerServiceSpec[], + tests: GenericTestSpec[], } export type ContainerModuleConfig = ModuleConfig -export const containerModuleSpecSchema = genericModuleSpecSchema +export const containerModuleSpecSchema = Joi.object() .keys({ buildArgs: Joi.object() .pattern(/.+/, joiPrimitive()) @@ -200,6 +203,8 @@ export const containerModuleSpecSchema = genericModuleSpecSchema services: joiArray(serviceSchema) .unique("name") .description("List of services to deploy from this container module."), + tests: joiArray(genericTestSchema) + .description("A list of tests to run in the module."), }) .description("Configuration for a container module.") diff --git a/src/plugins/generic.ts b/src/plugins/generic.ts index b5eff6b709..0f992f0bf1 100644 --- a/src/plugins/generic.ts +++ b/src/plugins/generic.ts @@ -10,6 +10,8 @@ import { exec } from "child-process-promise" import * as Joi from "joi" import { joiArray, + joiEnvVars, + PrimitiveMap, validate, } from "../types/common" import { @@ -55,11 +57,13 @@ export const genericTestSchema = baseTestSpecSchema .description("The test specification of a generic module.") export interface GenericModuleSpec extends ModuleSpec { + env: PrimitiveMap, tests: GenericTestSpec[], } export const genericModuleSpecSchema = Joi.object() .keys({ + env: joiEnvVars(), tests: joiArray(genericTestSchema) .description("A list of tests to run in the module."), }) @@ -86,7 +90,7 @@ export async function parseGenericModule( } } -export async function buildGenericModule({ module }: BuildModuleParams): Promise { +export async function buildGenericModule({ module }: BuildModuleParams): Promise { // By default we run the specified build command in the module root, if any. // TODO: Keep track of which version has been built (needs local data store/cache). const config: ModuleConfig = module.config @@ -95,7 +99,7 @@ export async function buildGenericModule({ module }: BuildModuleParams): Promise const buildPath = await module.getBuildPath() const result = await exec(config.build.command, { cwd: buildPath, - env: { ...process.env }, + env: { ...process.env, ...module.spec.env }, }) return { @@ -110,8 +114,15 @@ export async function buildGenericModule({ module }: BuildModuleParams): Promise export async function testGenericModule({ module, testConfig }: TestModuleParams): Promise { const startedAt = new Date() const command = testConfig.spec.command + const result = await spawn( - command[0], command.slice(1), { cwd: module.path, ignoreError: true }, + command[0], + command.slice(1), + { + cwd: module.path, + env: { ...process.env, ...module.spec.env }, + ignoreError: true, + }, ) return { diff --git a/src/util/util.ts b/src/util/util.ts index 03b4c44f42..73f577cd5e 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -28,6 +28,7 @@ import highlight from "cli-highlight" import chalk from "chalk" import hasAnsi = require("has-ansi") import { safeDump } from "js-yaml" +import { PrimitiveMap } from "../types/common" // shim to allow async generator functions if (typeof (Symbol as any).asyncIterator === "undefined") { @@ -148,6 +149,7 @@ export interface SpawnParams { cwd?: string data?: Buffer ignoreError?: boolean + env?: object } export interface SpawnPtyParams extends SpawnParams { @@ -166,9 +168,9 @@ export interface SpawnOutput { export function spawn( cmd: string, args: string[], - { timeout = 0, cwd, data, ignoreError = false }: SpawnParams = {}, + { timeout = 0, cwd, data, ignoreError = false, env }: SpawnParams = {}, ) { - const proc = _spawn(cmd, args, { cwd }) + const proc = _spawn(cmd, args, { cwd, env }) const result: SpawnOutput = { code: 0,