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.
  • Loading branch information
vincentbernat committed Nov 2, 2022
1 parent fe2123f commit 0d1a6b0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 1 deletion.
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 @@ -41,6 +41,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
6 changes: 5 additions & 1 deletion internal/cmd/rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,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 @@ -149,6 +150,7 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
// Abort if the file is not allowed
if !rc.Allowed() {
err = fmt.Errorf(notAllowed, rc.Path())
newEnv[DIRENV_STATUS] = "denied"
return
}

Expand Down Expand Up @@ -201,7 +203,9 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
cmd.Stderr = os.Stderr

var out []byte
if out, err = cmd.Output(); err == nil && len(out) > 0 {
if out, err = cmd.Output(); err != nil {
newEnv[DIRENV_STATUS] = "failed"
} else if len(out) > 0 {
var newEnv2 Env
newEnv2, err = LoadEnvJSON(out)
if err == nil {
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 @@ -22,6 +22,7 @@ unset DIRENV_FILE
unset DIRENV_MTIME
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 @@ -75,6 +76,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 @@ -207,11 +209,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 @@ -271,6 +275,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 @@ -17,6 +17,7 @@ unset-env DIRENV_FILE
unset-env DIRENV_MTIME
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 @@ -41,6 +41,7 @@ set -e DIRENV_FILE
set -e DIRENV_WATCHES
set -e DIRENV_MTIME
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 @@ -14,6 +14,7 @@ unsetenv DIRENV_FILE
unsetenv DIRENV_MTIME
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 0d1a6b0

Please sign in to comment.