From a20763c608ec7c8e68991f74f032d2c8c72941d4 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Wed, 15 Nov 2023 19:59:04 +0100 Subject: [PATCH 1/4] Add tests for clear error messages --- scripts/network-config | 5 +++++ scripts/network-config.test | 44 +++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/scripts/network-config b/scripts/network-config index 4d87d7c9d0d..fdaa88975e9 100755 --- a/scripts/network-config +++ b/scripts/network-config @@ -15,6 +15,10 @@ assert_dfx_network_var_is_set() { echo "ERROR: DFX_NETWORK is not defined." return 1 } >&2 + [[ "${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() { @@ -33,6 +37,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:" diff --git a/scripts/network-config.test b/scripts/network-config.test index 47bf58a6eaa..dd4d70c5b44 100755 --- a/scripts/network-config.test +++ b/scripts/network-config.test @@ -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 From 6592ef57382a0a31f3a2aa6fe1ff108882a1a724 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Thu, 16 Nov 2023 05:30:13 +0100 Subject: [PATCH 2/4] chnagelog --- CHANGELOG-Nns-Dapp-unreleased.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-Nns-Dapp-unreleased.md b/CHANGELOG-Nns-Dapp-unreleased.md index 65697678b23..98ab1031b62 100644 --- a/CHANGELOG-Nns-Dapp-unreleased.md +++ b/CHANGELOG-Nns-Dapp-unreleased.md @@ -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 From 04b4c23b461cc1ab5fbd75ca23073cd5d142e051 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Thu, 16 Nov 2023 05:57:55 +0100 Subject: [PATCH 3/4] comment --- scripts/network-config | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/network-config b/scripts/network-config index fdaa88975e9..3add291ec85 100755 --- a/scripts/network-config +++ b/scripts/network-config @@ -15,6 +15,7 @@ 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 2020, so is as old as Node14. [[ "${DFX_NETWORK@a}" == *x* ]] || { echo "ERROR: DFX_NETWORK is not exported." return 1 From 96ca3fc3efe2917ddc31cf591407007bbd2eb689 Mon Sep 17 00:00:00 2001 From: Max Murphy Date: Thu, 16 Nov 2023 06:05:48 +0100 Subject: [PATCH 4/4] Do we need to support old bash? --- scripts/network-config | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/network-config b/scripts/network-config index 3add291ec85..f5e1be77a2c 100755 --- a/scripts/network-config +++ b/scripts/network-config @@ -15,7 +15,10 @@ 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 2020, so is as old as Node14. + # 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