Skip to content

Commit 747d6b7

Browse files
committed
refactor(env): unify truthy env var checks in scripts
Introduce a common `is_truthy` function in Bash scripts and a Python `is_truthy_env_var` helper to standardize environment variable checks for truthy values. Refactor all relevant Bash and Python scripts to use these helpers, improving consistency and maintainability across the codebase.
1 parent de26354 commit 747d6b7

File tree

8 files changed

+49
-23
lines changed

8 files changed

+49
-23
lines changed

.github/common.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
is_truthy() {
4+
local val="${1:-}"
5+
val=${val,,}
6+
7+
case "$val" in
8+
1 | true | yes | on | enabled )
9+
return 0
10+
;;
11+
*)
12+
return 1
13+
;;
14+
esac
15+
}

.github/node_upgrade_pytest.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export REPORTS_DIR="${REPORTS_DIR:-".reports"}"
2222
rm -rf "${REPORTS_DIR:?}"
2323
mkdir -p "$REPORTS_DIR"
2424

25+
# shellcheck disable=SC1090,SC1091
26+
. .github/common.sh
27+
2528
#
2629
# STEP1 - start local cluster and run smoke tests for the first time
2730
#
@@ -50,7 +53,7 @@ if [ "$1" = "step1" ]; then
5053
export PATH="${BASE_REV_BIN}:${PATH}"
5154
fi
5255

53-
if [ "${CI_BYRON_CLUSTER:-"false"}" != "false" ]; then
56+
if is_truthy "${CI_BYRON_CLUSTER:-}"; then
5457
: "${TESTNET_VARIANT:="${CLUSTER_ERA}_slow"}"
5558
else
5659
: "${TESTNET_VARIANT:="${CLUSTER_ERA}_fast"}"

.github/regression.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ cd "$REPODIR"
1919
export WORKDIR="$REPODIR/run_workdir"
2020
export PATH_PREPEND="${PWD}/.bin"
2121

22+
# shellcheck disable=SC1090,SC1091
23+
. .github/common.sh
24+
2225
# shellcheck disable=SC1090,SC1091
2326
. .github/stop_cluster_instances.sh
2427

@@ -68,14 +71,10 @@ fi
6871

6972
if [ -n "${TESTNET_VARIANT:-}" ]; then
7073
export TESTNET_VARIANT
71-
elif [ "${CI_BYRON_CLUSTER:-"false"}" != "false" ]; then
74+
elif is_truthy "${CI_BYRON_CLUSTER:-}"; then
7275
export TESTNET_VARIANT="${CLUSTER_ERA:-conway}_slow"
7376
fi
7477

75-
if [ "${ALLOW_UNSTABLE_ERROR_MESSAGES:-"false"}" == "false" ]; then
76-
unset ALLOW_UNSTABLE_ERROR_MESSAGES
77-
fi
78-
7978
export CARDANO_NODE_SOCKET_PATH_CI="$WORKDIR/state-cluster0/bft1.socket"
8079

8180
# assume we run tests on testnet when `BOOTSTRAP_DIR` is set

.github/source_dbsync.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export TEST_THREADS CLUSTERS_COUNT
77
_origpwd="$PWD"
88
cd "$WORKDIR" || exit 1
99

10+
# shellcheck disable=SC1090,SC1091
11+
. .github/common.sh
12+
1013
stop_postgres() {
1114
echo "Stopping postgres"
1215

@@ -111,7 +114,7 @@ else
111114
|| exit 1
112115

113116
# Build cardano-smash-server
114-
if [ "${SMASH:-"false"}" != "false" ]; then
117+
if is_truthy "${SMASH:-}"; then
115118
nix build --accept-flake-config .#cardano-smash-server -o "${WORKDIR}/smash-server" || exit 1
116119
fi
117120

@@ -127,7 +130,7 @@ if [ ! -e "${WORKDIR}/db-sync-node/bin/cardano-db-sync" ]; then
127130
echo "The \`cardano-db-sync\` binary not found, line $LINENO in sourced db-sync setup" >&2 # assert
128131
exit 1
129132
fi
130-
if [ "${SMASH:-"false"}" != "false" ] && [ ! -e "${WORKDIR}/smash-server/bin/cardano-smash-server" ]; then
133+
if is_truthy "${SMASH:-}" && [ ! -e "${WORKDIR}/smash-server/bin/cardano-smash-server" ]; then
131134
echo "The \`cardano-smash-server\` binary not found, line $LINENO in sourced db-sync setup" >&2 # assert
132135
exit 1
133136
fi

cardano_node_tests/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def pytest_configure(config: tp.Any) -> None:
124124

125125
testrun_name = os.environ.get("CI_TESTRUN_NAME")
126126
if testrun_name:
127-
skip_passed = os.environ.get("CI_SKIP_PASSED") == "true"
127+
skip_passed = helpers.is_truthy_env_var("CI_SKIP_PASSED")
128128
config.stash[metadata_key]["CI_TESTRUN_NAME"] = testrun_name
129129
config.stash[metadata_key]["CI_SKIP_PASSED"] = str(skip_passed)
130130

cardano_node_tests/tests/test_reconnect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
LOGGER = logging.getLogger(__name__)
2323

24-
TEST_RECONNECT = os.environ.get("TEST_RECONNECT") is not None
25-
TEST_METRICS_RECONNECT = os.environ.get("TEST_METRICS_RECONNECT") is not None
24+
TEST_RECONNECT = helpers.is_truthy_env_var("TEST_RECONNECT")
25+
TEST_METRICS_RECONNECT = helpers.is_truthy_env_var("TEST_METRICS_RECONNECT")
2626

2727

2828
@common.SKIPIF_ON_TESTNET

cardano_node_tests/utils/configuration.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import pathlib as pl
55

6+
from cardano_node_tests.utils import helpers
7+
68
LAUNCH_PATH = pl.Path.cwd()
79

810
NETWORK_MAGIC_LOCAL = 42
@@ -14,13 +16,12 @@
1416
# See `cat /proc/sys/net/ipv4/ip_local_port_range`.
1517
PORTS_BASE = int(os.environ.get("PORTS_BASE") or 23000)
1618

17-
# Used also in startup scripts as `if [ -n "$VAR" ]...`
18-
ENABLE_LEGACY = (os.environ.get("ENABLE_LEGACY") or "") != ""
19-
# Used also in startup scripts as `if [ -n "$VAR" ]...`
20-
MIXED_P2P = (os.environ.get("MIXED_P2P") or "") != ""
19+
# Used also in startup scripts
20+
ENABLE_LEGACY = helpers.is_truthy_env_var("ENABLE_LEGACY")
21+
# Used also in startup scripts
22+
MIXED_P2P = helpers.is_truthy_env_var("MIXED_P2P")
2123

22-
# Used also in startup scripts as `if [ -n "$VAR" ]...`
23-
HAS_CC = (os.environ.get("NO_CC") or "") == ""
24+
HAS_CC = not helpers.is_truthy_env_var("NO_CC")
2425

2526
# Number of new blocks before the Tx is considered confirmed. Use default value if set to 0.
2627
CONFIRM_BLOCKS_NUM = int(os.environ.get("CONFIRM_BLOCKS_NUM") or 0)
@@ -65,8 +66,8 @@
6566
CLUSTERS_COUNT = int(os.environ.get("CLUSTERS_COUNT") or 0)
6667
CLUSTERS_COUNT = int(CLUSTERS_COUNT or (min(XDIST_WORKERS_COUNT, 9)) or 1)
6768

68-
DEV_CLUSTER_RUNNING = bool(os.environ.get("DEV_CLUSTER_RUNNING"))
69-
FORBID_RESTART = bool(os.environ.get("FORBID_RESTART"))
69+
DEV_CLUSTER_RUNNING = helpers.is_truthy_env_var("DEV_CLUSTER_RUNNING")
70+
FORBID_RESTART = helpers.is_truthy_env_var("FORBID_RESTART")
7071

7172
BOOTSTRAP_DIR = os.environ.get("BOOTSTRAP_DIR") or ""
7273

@@ -76,15 +77,15 @@
7677
raise RuntimeError(msg)
7778

7879
HAS_DBSYNC = bool(os.environ.get("DBSYNC_SCHEMA_DIR"))
79-
HAS_SMASH = HAS_DBSYNC and bool(os.environ.get("SMASH"))
80+
HAS_SMASH = HAS_DBSYNC and helpers.is_truthy_env_var("SMASH")
8081

81-
DONT_OVERWRITE_OUTFILES = bool(os.environ.get("DONT_OVERWRITE_OUTFILES"))
82+
DONT_OVERWRITE_OUTFILES = helpers.is_truthy_env_var("DONT_OVERWRITE_OUTFILES")
8283

8384
# Allow unstable error messages in tests
84-
ALLOW_UNSTABLE_ERROR_MESSAGES = bool(os.environ.get("ALLOW_UNSTABLE_ERROR_MESSAGES"))
85+
ALLOW_UNSTABLE_ERROR_MESSAGES = helpers.is_truthy_env_var("ALLOW_UNSTABLE_ERROR_MESSAGES")
8586

8687
# Cluster instances are kept running after tests finish
87-
KEEP_CLUSTERS_RUNNING = bool(os.environ.get("KEEP_CLUSTERS_RUNNING"))
88+
KEEP_CLUSTERS_RUNNING = helpers.is_truthy_env_var("KEEP_CLUSTERS_RUNNING")
8889

8990
# Determine what scripts to use to start the cluster
9091
TESTNET_VARIANT = os.environ.get("TESTNET_VARIANT") or ""

cardano_node_tests/utils/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def environ(env: dict) -> tp.Iterator[None]:
7575
os.environ[key] = value
7676

7777

78+
def is_truthy_env_var(var: str) -> bool:
79+
"""Check if an environment variable is set to a truthy value."""
80+
return (os.environ.get(var) or "").lower() in ("1", "true", "yes", "on", "enabled")
81+
82+
7883
def run_command(
7984
command: str | list,
8085
*,

0 commit comments

Comments
 (0)