summaryrefslogtreecommitdiff
path: root/tests/test-dotenv-precedence.sh
blob: f2cc760b3105bca6de015fec219840aa70dfcbc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env bash

set -euo pipefail

project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
tmp_dir="$(mktemp -d)"
trap 'rm -rf "${tmp_dir}"' EXIT

grep -Fqx 'set dotenv-filename := ".env"' "${project_root}/Justfile"
grep -Fqx '.env' "${project_root}/.gitignore"
if rg -F -- '--dotenv-path image-template.env' "${project_root}/.github/workflows"; then
  echo 'CI must not load image-template.env as a dotenv file' >&2
  exit 1
fi

printf '%s\n' \
  'set dotenv-load' \
  'value := env_var("VALUE")' \
  'show:' \
  '    @echo "{{value}}"' > "${tmp_dir}/justfile"
printf '%s\n' 'VALUE=from-dotenv' > "${tmp_dir}/.env"

[[ "$(cd "${tmp_dir}" && just show)" == 'from-dotenv' ]]
[[ "$(cd "${tmp_dir}" && VALUE=from-runtime just show)" == 'from-runtime' ]]

mkdir "${tmp_dir}/without-dotenv"
cp "${tmp_dir}/justfile" "${tmp_dir}/without-dotenv/justfile"
[[ "$(cd "${tmp_dir}/without-dotenv" && VALUE=from-runtime just show)" == 'from-runtime' ]]