Skip to content

Commit 701fa39

Browse files
committed
tests: add a direnv integration test
Test that simply evaluating a shell does not trigger direnv to reload immediately.
1 parent 7b6624e commit 701fa39

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

tests/direnv/.test.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Test the direnv integration
2+
#
3+
# Our main concern is that `devenv shell` should not trigger direnv to immediately reload.
4+
# Because direnv only checks the modification time of watched files, we need to take extra care not to "carelessly" write to such files.
5+
set -xeuo pipefail
6+
7+
# Install direnv
8+
export PATH="$(nix build nixpkgs#direnv --print-out-paths --no-out-link)/bin:$PATH"
9+
10+
export TMPDIR=$(mktemp -d)
11+
export XDG_CONFIG_HOME=${TMPDIR}/config
12+
export XDG_DATA_HOME=${TMPDIR}/data
13+
14+
direnv_eval() {
15+
eval "$(direnv export bash)"
16+
}
17+
18+
# Setup direnv
19+
mkdir -p $XDG_CONFIG_HOME/.config/direnv/
20+
cat > $XDG_CONFIG_HOME/.config/direnv/direnv.toml << 'EOF'
21+
[global]
22+
strict_env = true
23+
EOF
24+
25+
# Initialize direnv
26+
cat > .envrc << 'EOF'
27+
eval "$(devenv direnvrc)"
28+
use devenv
29+
EOF
30+
31+
# Load the environment
32+
direnv allow
33+
direnv_eval
34+
35+
# Enter shell and capture initial watches
36+
DIRENV_WATCHES_BEFORE=$DIRENV_WATCHES
37+
38+
# Execute some operations that should not cause direnv to reload
39+
echo "Running commands that should not trigger direnv reload..." >&2
40+
devenv shell "echo 'Hello from devenv shell'"
41+
42+
direnv_eval
43+
44+
# Capture watches after
45+
DIRENV_WATCHES_AFTER=$DIRENV_WATCHES
46+
47+
echo "Checking whether direnv reload was triggered..." >&2
48+
if [[ "$DIRENV_WATCHES_BEFORE" == "$DIRENV_WATCHES_AFTER" ]]; then
49+
echo "PASS: DIRENV_WATCHES remained unchanged" >&2
50+
exit 0
51+
else
52+
echo "FAIL: DIRENV_WATCHES changed, indicating unwanted reload" >&2
53+
echo "Before: $DIRENV_WATCHES_BEFORE" >&2
54+
echo "After: $DIRENV_WATCHES_AFTER" >&2
55+
exit 1
56+
fi

tests/direnv/devenv.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ }

0 commit comments

Comments
 (0)