Skip to content

Commit

Permalink
rc: add DIRENV_STATUS with status of direnv loading
Browse files Browse the repository at this point in the history
It can be "denied" if the environment loading was denied. "failed" if
the environment failed to load. "allowed" if everything was successful.
When nothing is imported, it is considered a failure.
  • Loading branch information
vincentbernat committed Dec 4, 2023
1 parent e517d3a commit a93c531
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cmd/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
DIRENV_FILE = "DIRENV_FILE"
DIRENV_WATCHES = "DIRENV_WATCHES"
DIRENV_DIFF = "DIRENV_DIFF"
DIRENV_STATUS = "DIRENV_STATUS"

DIRENV_DUMP_FILE_PATH = "DIRENV_DUMP_FILE_PATH"
)
1 change: 1 addition & 0 deletions internal/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (env Env) CleanContext() {
delete(env, DIRENV_FILE)
delete(env, DIRENV_DUMP_FILE_PATH)
delete(env, DIRENV_WATCHES)
delete(env, DIRENV_STATUS)
}

// LoadEnv unmarshals the env back from a gzenv string
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
direnv := config.SelfPath
newEnv = previousEnv.Copy()
newEnv[DIRENV_WATCHES] = rc.times.Marshal()
newEnv[DIRENV_STATUS] = "allowed"
defer func() {
// Record directory changes even if load is disallowed or fails
newEnv[DIRENV_DIR] = "-" + filepath.Dir(rc.path)
Expand All @@ -218,9 +219,11 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
switch rc.Allowed() {
case NotAllowed:
err = fmt.Errorf(notAllowed, rc.Path())
newEnv[DIRENV_STATUS] = "denied"
return
case Allowed:
case Denied:
newEnv[DIRENV_STATUS] = "denied"
return
}

Expand Down Expand Up @@ -282,9 +285,11 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
newEnv2, err = LoadEnvJSON(out)
if err == nil {
newEnv = newEnv2
return
}
}

newEnv[DIRENV_STATUS] = "failed"
return
}

Expand Down
13 changes: 13 additions & 0 deletions test/direnv-test-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ unset DIRENV_DIR
unset DIRENV_FILE
unset DIRENV_WATCHES
unset DIRENV_DIFF
unset DIRENV_STATUS

mkdir -p "${XDG_CONFIG_HOME}/direnv"
touch "${XDG_CONFIG_HOME}/direnv/direnvrc"
Expand Down Expand Up @@ -74,6 +75,7 @@ test_start base
echo "Setting up"
direnv_eval
test_eq "$HELLO" "world"
test_eq "${DIRENV_STATUS:-}" "allowed"

WATCHES=$DIRENV_WATCHES

Expand Down Expand Up @@ -206,11 +208,13 @@ test_start "failure"
# fails.
test_eq "${DIRENV_DIFF:-}" ""
test_eq "${DIRENV_WATCHES:-}" ""
test_eq "${DIRENV_STATUS:-}" ""

direnv_eval

test_neq "${DIRENV_DIFF:-}" ""
test_neq "${DIRENV_WATCHES:-}" ""
test_eq "${DIRENV_STATUS:-}" "failed"
test_stop

test_start "watch-dir"
Expand Down Expand Up @@ -280,6 +284,15 @@ if has python; then
test_stop
fi

test_start "deny"
direnv deny
direnv_eval
test_neq "${DIRENV_DIFF:-}" ""
test_neq "${DIRENV_WATCHES:-}" ""
test_eq "${DIRENV_STATUS:-}" "denied"
test -z "${HELLO}"
test_stop

test_start "aliases"
direnv deny
# check that allow/deny aliases work
Expand Down
1 change: 1 addition & 0 deletions test/direnv-test.elv
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ unset-env DIRENV_DIR
unset-env DIRENV_FILE
unset-env DIRENV_WATCHES
unset-env DIRENV_DIFF
unset-env DIRENV_STATUS

mkdir -p $E:XDG_CONFIG_HOME/direnv
touch $E:XDG_CONFIG_HOME/direnv/direnvrc
Expand Down
1 change: 1 addition & 0 deletions test/direnv-test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set -e DIRENV_DIR
set -e DIRENV_FILE
set -e DIRENV_WATCHES
set -e DIRENV_DIFF
set -e DIRENV_STATUS

function direnv_eval
#direnv export fish # for debugging
Expand Down
1 change: 1 addition & 0 deletions test/direnv-test.tcsh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ unsetenv DIRENV_DIR
unsetenv DIRENV_FILE
unsetenv DIRENV_WATCHES
unsetenv DIRENV_DIFF
unsetenv DIRENV_STATUS

alias direnv_eval 'eval `direnv export tcsh`'

Expand Down
1 change: 1 addition & 0 deletions test/scenarios/deny/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export HELLO=world

0 comments on commit a93c531

Please sign in to comment.