Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if network is defined but not exported #3771

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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