Skip to content

Commit

Permalink
test: add test coverage for npm_package_bin configuration_env_vars
Browse files Browse the repository at this point in the history
And test coverage for using --action_env=FOO=BAR to pass env variables to npm_package_bin & nodejs_binary
  • Loading branch information
gregmagolan committed Jan 14, 2020
1 parent 3ccfd3c commit 6789f72
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ build --noincompatible_no_support_tools_in_action_inputs
# Define environment value used by some tests such as //internal/npm_install/test:bazel_bin_test
build --define=SOME_TEST_ENV=some_value
test --define=SOME_TEST_ENV=some_value
build --action_env=SOME_OTHER_ENV=some_other_value

###############################
# Remote Build Execution support
Expand Down
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
## IMPORTANT
# If you change the `default_docker_image` version, also change the `cache_key` version
var_1: &default_docker_image circleci/node:10.16
var_3: &cache_key node-0.16-{{ checksum "yarn.lock" }}
var_3: &cache_key node-0.16-{{ checksum "yarn.lock" }}-v2

var_4: &init_environment
run:
Expand Down
6 changes: 5 additions & 1 deletion internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ nodejs_binary(

nodejs_test(
name = "define_var",
configuration_env_vars = ["SOME_TEST_ENV"],
configuration_env_vars = [
"SOME_TEST_ENV",
"SOME_OTHER_ENV",
],
data = glob(["*.spec.js"]),
entry_point = ":define.spec.js",
)
Expand Down Expand Up @@ -271,6 +274,7 @@ npm_package_bin(
"$@",
"$(SOME_TEST_ENV)",
],
configuration_env_vars = ["SOME_OTHER_ENV"],
tool = ":expand_variables",
)

Expand Down
8 changes: 8 additions & 0 deletions internal/node/test/define.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ if (process.env['SOME_TEST_ENV'] !== 'some_value') {
console.error('should accept vars; must be run with --define=SOME_TEST_ENV=some_value');
process.exitCode = 1;
}

// Similar to above but testing another env variable that is expected to be specified with
// `--action_env=SOME_OTHER_ENV=some_other_value`.
if (process.env['SOME_OTHER_ENV'] !== 'some_other_value') {
console.error(
'should accept vars; must be run with --action_env=SOME_OTHER_ENV=some_other_value');
process.exitCode = 1;
}
3 changes: 2 additions & 1 deletion internal/node/test/expand_variables.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[
"some_value"
"some_value",
"some_other_value"
]
1 change: 1 addition & 0 deletions internal/node/test/expand_variables.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const args = process.argv.slice(2);
const outfile = args.shift();
args.push(process.env['SOME_OTHER_ENV']);
fs.writeFileSync(outfile, JSON.stringify(args, null, 2), 'utf-8');
5 changes: 3 additions & 2 deletions internal/npm_install/test/bazel_bin_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# --- end runfiles.bash initialization v2 ---

readonly OUT=$($(rlocation "npm/testy/bin/testy.sh"))
readonly EXPECTED="Hello some_value && some_other_value"

if [ "$OUT" != "Hello some_value" ]; then
echo "Expected output 'Hello world' but was '$OUT'"
if [ "${OUT}" != "${EXPECTED}" ]; then
echo "Expected output '${EXPECTED}' but was '${OUT}'"
exit 1
fi
2 changes: 1 addition & 1 deletion tools/npm_packages/testy/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log(`Hello ${process.env['SOME_TEST_ENV']}`);
console.log(`Hello ${process.env['SOME_TEST_ENV']} && ${process.env['SOME_OTHER_ENV']}`);
2 changes: 1 addition & 1 deletion tools/npm_packages/testy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bazelBin": {
"testy": {
"additionalAttributes": {
"configuration_env_vars": "[\"SOME_TEST_ENV\"]"
"configuration_env_vars": "[\"SOME_TEST_ENV\", \"SOME_OTHER_ENV\"]"
}
}
}
Expand Down

0 comments on commit 6789f72

Please sign in to comment.