Files
crosscode-platform/scripts/tests/envtpl.bats
T

99 lines
4.7 KiB
Bash

#!/usr/bin/env bats
# .env files from a product's env template (lib/envtpl.cjs, AGENT-1.0-PLAN §5): written whole on
# install, only appended to on deploy, nothing written when a required value is missing.
setup() {
TMP="$BATS_TEST_TMPDIR"
TPL_JS="$BATS_TEST_DIRNAME/../lib/envtpl.cjs"
APP="$TMP/app"
mkdir -p "$APP/backend" "$APP/frontend"
cat > "$TMP/template.json" <<'EOF'
{ "schema": "crosscode.env-template/1", "product": "events", "files": {
"backend/.env": [
{ "name": "DATABASE_URL", "layer": "generated", "required": true },
{ "name": "PORT", "layer": "install", "from": "{apiPort}", "required": true },
{ "name": "FRONTEND_URL", "layer": "install", "from": "https://{domain}", "required": true },
{ "name": "TRUST_PROXY", "layer": "server", "required": true },
{ "name": "HUB_URL", "layer": "defaults", "default": "https://hub.crosscode.co.za" },
{ "name": "SENTRY_DSN", "layer": "defaults" },
{ "name": "HUB_LICENCE_KEY", "layer": "install" }
],
"frontend/.env.production": [
{ "name": "NEXT_PUBLIC_API_URL", "layer": "install", "from": "https://{domain}", "required": true }
] } }
EOF
cat > "$TMP/config.json" <<'EOF'
{ "server": { "TRUST_PROXY": "loopback" }, "defaults": {}, "install": { "events": { "HUB_LICENCE_KEY": "CEH-AAAA-BBBB" } } }
EOF
export CROSSCODE_GEN_DATABASE_URL='postgresql://hope_events:s3cr3t@localhost:5432/hope_events'
}
envtpl() { node "$TPL_JS" --template "$TMP/template.json" --dir "$APP" --product events --var domain=events.hope.co.za --var apiPort=20011 "$@"; }
@test "install: writes every file from the layers, mode 600, values never printed" {
run envtpl --config "$TMP/config.json" --release events@2.3.0
[ "$status" -eq 0 ]
[[ "$output" == *"backend/.env: wrote DATABASE_URL, PORT, FRONTEND_URL, TRUST_PROXY, HUB_URL, HUB_LICENCE_KEY"* ]]
[[ "$output" != *"s3cr3t"* && "$output" != *"CEH-AAAA"* ]]
grep -qx 'DATABASE_URL=postgresql://hope_events:s3cr3t@localhost:5432/hope_events' "$APP/backend/.env"
grep -qx 'PORT=20011' "$APP/backend/.env"
grep -qx 'FRONTEND_URL=https://events.hope.co.za' "$APP/backend/.env"
grep -qx 'TRUST_PROXY=loopback' "$APP/backend/.env"
grep -qx 'HUB_URL=https://hub.crosscode.co.za' "$APP/backend/.env"
! grep -q SENTRY_DSN "$APP/backend/.env"
grep -qx 'NEXT_PUBLIC_API_URL=https://events.hope.co.za' "$APP/frontend/.env.production"
# File modes only mean something on Linux (the servers); Windows file systems ignore them.
[ "$(uname -s)" != Linux ] || [ "$(stat -c %a "$APP/backend/.env")" = 600 ]
}
@test "a required value missing anywhere: nothing is written" {
run envtpl --release events@2.3.0
[ "$status" -eq 3 ]
[[ "$output" == *"TRUST_PROXY (server, backend/.env)"* ]]
[ ! -e "$APP/backend/.env" ]
[ ! -e "$APP/frontend/.env.production" ]
}
@test "--fallback fills a value no layer gives" {
run envtpl --fallback TRUST_PROXY=1
[ "$status" -eq 0 ]
grep -qx 'TRUST_PROXY=1' "$APP/backend/.env"
}
@test "deploy: only new variables are appended; existing lines stay byte for byte" {
printf '# by hand\nDATABASE_URL=postgresql://keep\nPORT=5000\nFRONTEND_URL="https://old.example"\nTRUST_PROXY=1\nCUSTOM=x\n' > "$APP/backend/.env"
cp "$APP/backend/.env" "$TMP/before"
echo 'NEXT_PUBLIC_API_URL=https://old.example' > "$APP/frontend/.env.production"
run envtpl --config "$TMP/config.json" --release events@2.4.0
[ "$status" -eq 0 ]
[[ "$output" == *"backend/.env: added HUB_URL, HUB_LICENCE_KEY"* ]]
[[ "$output" != *"frontend"* ]]
head -c "$(stat -c %s "$TMP/before")" "$APP/backend/.env" | cmp - "$TMP/before"
grep -q '^# Added by crosscode .* (events@2.4.0)$' "$APP/backend/.env"
grep -qx 'HUB_URL=https://hub.crosscode.co.za' "$APP/backend/.env"
[ "$(grep -c '^PORT=' "$APP/backend/.env")" = 1 ]
}
@test "--check says what would be added and changes nothing" {
printf 'DATABASE_URL=x\nPORT=1\nFRONTEND_URL=y\nTRUST_PROXY=1\n' > "$APP/backend/.env"
cp "$APP/backend/.env" "$TMP/before"
run envtpl --check
[ "$status" -eq 0 ]
[[ "$output" == *"backend/.env: would add HUB_URL"* ]]
cmp "$APP/backend/.env" "$TMP/before"
[ ! -e "$APP/frontend/.env.production" ]
}
@test "values with spaces or quotes are quoted the way dotenv reads them" {
run node -e 'const t = require(process.argv[1]); console.log(t.line("A", "plain/value:1")); console.log(t.line("B", "two words \"q\""))' "$TPL_JS"
[ "${lines[0]}" = 'A=plain/value:1' ]
[ "${lines[1]}" = 'B="two words \"q\""' ]
}
@test "a file name that leaves the product folder is refused" {
echo '{ "schema": "crosscode.env-template/1", "product": "events", "files": { "../evil": [ { "name": "A", "layer": "defaults", "default": "1" } ] } }' > "$TMP/bad.json"
run node "$TPL_JS" --template "$TMP/bad.json" --dir "$APP"
[ "$status" -ne 0 ]
[ ! -e "$TMP/evil" ]
}