Skip to content

Commit

Permalink
fix(bazel): integration test env variable expansion not respecting cu…
Browse files Browse the repository at this point in the history
…stom variables

The integration test rule currently does not respect custom variables
that have been configured as part of the target. e.g. the following does
not work:

```bzl
integration_test(
  environment = {
    "TEST_ENV": "$(location blabla)"
  },
  commands = [
    "yarn test $${TEST_ENV}
  ],
)
```

This commit fixes that such custom variables are available for
expansion when commands are executed.
  • Loading branch information
devversion committed Nov 5, 2021
1 parent e34f1a6 commit 4c02c1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 6 additions & 3 deletions bazel/integration/test_runner/process_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ const environmentVariableSubstitutionRegex = /\${(\w+)}/g;
*
* @throws An error if a substituted environment variable does not exist.
*/
export function expandEnvironmentVariableSubstitutions(args: string[]): string[] {
export function expandEnvironmentVariableSubstitutions(
args: string[],
env: NodeJS.ProcessEnv,
): string[] {
return args.map((content) => {
return content.replace(environmentVariableSubstitutionRegex, (_, variableName) => {
if (process.env[variableName] === undefined) {
if (env[variableName] === undefined) {
throw new Error(`Could not find substituted environment variable: ${variableName}`);
}
return process.env[variableName]!;
return env[variableName]!;
});
});
}
Expand Down
5 changes: 4 additions & 1 deletion bazel/integration/test_runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ export class TestRunner {
const resolvedBinary = binary.containsExpandedValue
? await resolveBinaryWithRunfiles(binary.value)
: binary.value;
const evaluatedArgs = expandEnvironmentVariableSubstitutions(args.map((v) => v.value));
const evaluatedArgs = expandEnvironmentVariableSubstitutions(
args.map((v) => v.value),
commandEnv,
);
const success = await runCommandInChildProcess(
resolvedBinary,
evaluatedArgs,
Expand Down

0 comments on commit 4c02c1b

Please sign in to comment.