diff --git a/packages/web-integration/src/yaml/utils.ts b/packages/web-integration/src/yaml/utils.ts index f6a8b818..3c69d0bf 100644 --- a/packages/web-integration/src/yaml/utils.ts +++ b/packages/web-integration/src/yaml/utils.ts @@ -11,11 +11,22 @@ import type { MidsceneYamlFlowItemSleep, } from '@midscene/core'; +function interpolateEnvVars(content: string): string { + return content.replace(/\$\{([^}]+)\}/g, (_, envVar) => { + const value = process.env[envVar.trim()]; + if (value === undefined) { + throw new Error(`Environment variable "${envVar.trim()}" is not defined`); + } + return value; + }); +} + export function parseYamlScript( content: string, filePath?: string, ): MidsceneYamlScript { - const obj = yaml.load(content) as MidsceneYamlScript; + const interpolatedContent = interpolateEnvVars(content); + const obj = yaml.load(interpolatedContent) as MidsceneYamlScript; const pathTip = filePath ? `, failed to load ${filePath}` : ''; assert(obj.target, `property "target" is required in yaml script${pathTip}`); assert( diff --git a/packages/web-integration/tests/unit-test/yaml/utils.test.ts b/packages/web-integration/tests/unit-test/yaml/utils.test.ts index d40766d3..3a56af44 100644 --- a/packages/web-integration/tests/unit-test/yaml/utils.test.ts +++ b/packages/web-integration/tests/unit-test/yaml/utils.test.ts @@ -1,4 +1,4 @@ -import { buildYaml, flowItemBrief } from '@/yaml'; +import { buildYaml, flowItemBrief, parseYamlScript } from '@/yaml'; import { describe, expect, test } from 'vitest'; describe('utils', () => { @@ -14,4 +14,34 @@ describe('utils', () => { flowItemBrief({ aiWaitFor: 'wait for something' }), ).toMatchSnapshot(); }); + + describe('parseYamlScript', () => { + test('interpolates environment variables', () => { + process.env.TEST_URL = 'https://example.com'; + process.env.TEST_PATH = '/test/path'; + + const yamlContent = ` +target: + url: "\${TEST_URL}\${TEST_PATH}" +tasks: + - sleep: 1000 +`; + + const result = parseYamlScript(yamlContent); + expect(result.target.url).toBe('https://example.com/test/path'); + }); + + test('throws error for undefined environment variables', () => { + const yamlContent = ` +target: + url: "\${UNDEFINED_ENV_VAR}" +tasks: + - sleep: 1000 +`; + + expect(() => parseYamlScript(yamlContent)).toThrow( + 'Environment variable "UNDEFINED_ENV_VAR" is not defined', + ); + }); + }); });