# Tests related to setting the environment for devbox run.

# Parent shell vars should leak into the run environment
env HOMETEST=/home/test
env USER=test-user
env FOO=bar
exec devbox run echo '$HOMETEST'
stdout '/home/test'
exec devbox run echo '$USER'
stdout 'test-user'
exec devbox run echo '$FOO'
stdout 'bar'

# Fixed/hard-coded vars are set
exec devbox run echo '$__ETC_PROFILE_NIX_SOURCED'
stdout '1'

# DEVBOX_* vars are passed through
env DEVBOX_FOO=baz
exec devbox run echo '$DEVBOX_FOO'
stdout 'baz'

# Vars defined in devbox.json are passed through
env DEVBOX_FEATURE_ENV_CONFIG=1
exec devbox run echo '$CONFIG_VAR1'
stdout 'abc'

# Vars defined in devbox.json that reference another variable are set
env DEVBOX_FEATURE_ENV_CONFIG=1
env DEVBOX_FOO=baz
exec devbox run echo '$CONFIG_VAR2'
stdout 'baz'

# Vars in devbox that refer to $PWD should get the project dir
env PWD=/test-pwd
exec devbox run echo '$CONFIG_VAR3'
! stdout '/test-pwd'

# Variables are applied in order: nix vars, DEVBOX_*, leaked, leakedForShell, fixed/hard-coded vars,
# plugin vars, and config vars. It really only makes sense to test for plugin and config vars order.
# Note that the nginx plugin defines NGINX_CONFDIR, NGINX_PATH_PREFIX, and NGINX_TMPDIR.
env NGINX_TMPDIR="to-be-overwritten-by-plugin"
exec devbox run echo '$NGINX_TMPDIR'
! stdout 'to-be-overwritten-by-plugin'
stdout '/nginx/temp'

exec devbox run echo '$NGINX_CONFDIR'
stdout 'devbox-json-override'

-- devbox.json --
{
  "packages": ["nginx@latest"],
  "env": {
    "CONFIG_VAR1": "abc",
    "CONFIG_VAR2": "$DEVBOX_FOO",
    "CONFIG_VAR3": "${PWD}",
    "NGINX_CONFDIR": "devbox-json-override"
  }
}
