Skip to content

Commit

Permalink
Error if network is defined but not exported (#3771)
Browse files Browse the repository at this point in the history
# Motivation
`DFX_NETWORK` is typically provided to dfx build commands as an exported
environment variable. When checking that `DFX_NETWORK` is provided, we
need to check specifically that it is exported as otherwise it is
possible to get very confusing behaviour in which the network is defined
in one part of the deployment but not another.

# Changes
- Check that `DFX_NETWORK` is exported.

# Tests
- Tests have been added that the error message specifically mentions
`DFX_NETWORK` not being exported.

# Todos

- [x] Add entry to changelog (if necessary).
  • Loading branch information
bitdivine authored Nov 16, 2023
1 parent 43e7e4b commit aff6669
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ proposal is successful, the changes it released will be moved from this file to
* Update `dfx` to `v0.15.1`.
* Update the URL of the app subnet to what dfx v15 expects.
* Use a unique branch when updating the snsdemo release, didc, IC candid files or rust.
* Better checks that the network is defined.

#### Deprecated

Expand Down
9 changes: 9 additions & 0 deletions scripts/network-config
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ assert_dfx_network_var_is_set() {
echo "ERROR: DFX_NETWORK is not defined."
return 1
} >&2
# Note: ${var@a} gets variable attributes. It has been available in bash since bash 5.1, so is as old as Node14.
# 'x' is the export attribute.
# If we find that we need to support older systems, please use this instead:
# bash -c 'test -n "${DFX_NETWORK:-}"' || { ...
[[ "${DFX_NETWORK@a}" == *x* ]] || {
echo "ERROR: DFX_NETWORK is not exported."
return 1
}
}
# Gets the global network configuration. If missing, the answer is null and the return-value 1.
global_network_config() {
Expand All @@ -33,6 +41,7 @@ local_network_config() {
}
# Checks that the DFX_NETWORK is configured
assert_dfx_network_var_is_configured() {
assert_dfx_network_var_is_set || return
local_network_config >/dev/null || global_network_config >/dev/null || {
echo "ERROR: DFX_NETWORK '$DFX_NETWORK' is not defined in dfx.json or $GLOBAL_NETWORK_CONFIG_FILE"
echo "Available networks are:"
Expand Down
44 changes: 33 additions & 11 deletions scripts/network-config.test
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,41 @@ title() {
} >&2
fi
)
(
title "assert_dfx_network_var_is_set should fail when the network is unset"
mk_env
unset DFX_NETWORK
if assert_dfx_network_var_is_set 2>/dev/null; then
{
echo "FAIL"
for command in assert_dfx_network_var_is_set assert_dfx_network_var_is_configured global_network_config local_network_config network_config; do
(
title "$command should fail when the network is unset"
mk_env
unset DFX_NETWORK
if "$command" 2>/dev/null; then
{
echo "FAIL"
exit 1
} >&2
else
echo OK
fi
)
(
title "$command should fail when the network is defined but not exported"
mk_env
DFX_NETWORK=gobbeldygook
if actual_stderr="$("$command" 2>&1)"; then
{
echo "FAIL: $command should have returned non-zero."
exit 1
} >&2
else
echo OK
fi
expected_stderr="ERROR: DFX_NETWORK is not exported."
[[ "${expected_stderr}" == "${actual_stderr}" ]] || {
echo "FAIL: The error emitted by $command should state explicitly that DFX_NETWORK should be exported."
echo "Expected: $expected_stderr"
echo "Actual: $actual_stderr"
exit 1
} >&2
else
echo OK
fi
)
)
done
(
title "global_network_config should be able to get a globally defined network"
mk_env
Expand Down

0 comments on commit aff6669

Please sign in to comment.