diff --git a/_unit-test/merge-env-file-test.sh b/_unit-test/merge-env-file-test.sh new file mode 100755 index 00000000000..156110d0060 --- /dev/null +++ b/_unit-test/merge-env-file-test.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# This is a test file for a part of `_lib.sh`, where we read `.env.custom` file if there is one. +# We only want to give very minimal value to the `.env.custom` file, and expect that it would +# be merged with the original `.env` file, with the `.env.custom` file taking precedence. +cat <".env.custom" +SENTRY_EVENT_RETENTION_DAYS=10 +EOF + +# The `_test_setup.sh` script sources `install/_lib.sh`, so.. finger crossed this should works. +source _unit-test/_test_setup.sh + +rm -f .env.custom + +echo "Expecting SENTRY_EVENT_RETENTION_DAYS to be 10, got ${SENTRY_EVENT_RETENTION_DAYS}" +test "$SENTRY_EVENT_RETENTION_DAYS" == "10" +echo "Pass" +echo "Expecting SENTRY_BIND to be 9000, got ${SENTRY_BIND}" +test "$SENTRY_BIND" == "9000" +echo "Pass" +echo "Expecting COMPOSE_PROJECT_NAME to be sentry-self-hosted, got ${COMPOSE_PROJECT_NAME}" +test "$COMPOSE_PROJECT_NAME" == "sentry-self-hosted" +echo "Pass" + +report_success diff --git a/install/_lib.sh b/install/_lib.sh index 3a76d8624f0..73ef0237df9 100644 --- a/install/_lib.sh +++ b/install/_lib.sh @@ -16,8 +16,16 @@ else _ENV=.env fi +# Reading .env.custom has to come first. The value won't be overriden, instead +# it would persist because of `export -p> >"$t"` later, which exports current +# environment variables to a temporary file with a `declare -x KEY=value` format. +# The new values on `.env` would be set only if they are not already set. +if [[ "$_ENV" == ".env.custom" ]]; then + q=$(mktemp) && export -p >"$q" && set -a && . ".env.custom" && set +a && . "$q" && rm "$q" && unset q +fi + # Read .env for default values with a tip o' the hat to https://stackoverflow.com/a/59831605/90297 -t=$(mktemp) && export -p >"$t" && set -a && . $_ENV && set +a && . "$t" && rm "$t" && unset t +t=$(mktemp) && export -p >"$t" && set -a && . ".env" && set +a && . "$t" && rm "$t" && unset t if [ "${GITHUB_ACTIONS:-}" = "true" ]; then _group="::group::"