Compare commits

..

1 Commits

Author SHA1 Message Date
a498992c2a Fix bugs
* Fix env diff for variables containing leading/trailing whitespace
* Run whole script in 'clean' (non-direnv) environment (fixes prepending/appending)
2019-11-27 14:03:56 +01:00
6 changed files with 18 additions and 96 deletions

View File

@@ -1,9 +0,0 @@
build:
install:
mkdir -p $(PREFIX)/bin $(PREFIX)/share
cp direnv-cache $(PREFIX)/bin
cp direnvrc $(PREFIX)/share
check:
term-test -s tests

View File

@@ -1,19 +1,3 @@
{ stdenv { pkgs ? import <nixpkgs> {} }:
, direnv (pkgs.writers.writeBashBin "direnv-cache" ./direnv-cache).overrideAttrs
, term-test ? null (o: { passthru = { direnvrc = ./direnvrc; }; })
, doCheck ? true
}:
assert doCheck -> term-test != null;
stdenv.mkDerivation {
name = "direnv-cache";
src = ./.;
propagatedBuildInputs = [ direnv ];
installPhase = ''
make install PREFIX=$out
'';
checkInputs = [ term-test ];
inherit doCheck;
passthru = { direnvrc = ./direnvrc; };
preferLocalBuild = true;
}

View File

@@ -32,26 +32,22 @@ diffEnvs() {
while IFS= read -r -d '' line; do while IFS= read -r -d '' line; do
name=$(tr -d '\0' <<<"$line" | head -n1 | cut -f1 -d'=') name=$(tr -d '\0' <<<"$line" | head -n1 | cut -f1 -d'=')
if ! [[ "$name" = DIRENV_* ]]; then if ! getByName "$name" "$new" >/dev/null; then
if ! getByName "$name" "$new" >/dev/null; then echo "export -n '${name}'"
echo "export -n '${name}'"
fi
fi fi
done <"$old" done <"$old"
while IFS= read -rd $'\0' line; do while IFS= read -rd $'\0' line; do
name=$(tr -d '\0' <<<"$line" | head -n1 | cut -f1 -d'=') name=$(tr -d '\0' <<<"$line" | head -n1 | cut -f1 -d'=')
if ! [[ "$name" = DIRENV_* ]]; then if getByName "$name" "$old" >/dev/null; then
if getByName "$name" "$old" >/dev/null; then # found in old env, check if values match
# found in old env, check if values match if ! grep -z "^${line}\$" "$old" >/dev/null; then
if ! grep -z "^${line}\$" "$old" >/dev/null; then # variable has changed
# variable has changed
echo "export ${line@Q}"
fi
else
# variable is new
echo "export ${line@Q}" echo "export ${line@Q}"
fi fi
else
# variable is new
echo "export ${line@Q}"
fi fi
done <"$new" done <"$new"
} }
@@ -70,23 +66,9 @@ getCacheFilePath() {
echo "${cacheDir}/$(pwd | sha1sum | tr -d ' -')" echo "${cacheDir}/$(pwd | sha1sum | tr -d ' -')"
} }
switchToDirenvDir() {
# Find directory that contains the current .envrc. Allows direnv-cache to
# be called from subdirectories.
wd="$(pwd)"
if [[ -n $DIRENV_DIR ]]; then
wd="${DIRENV_DIR#-}"
fi
cd "$wd"
}
cmd="$1" cmd="$1"
switchToDirenvDir if ! [[ -e .envrc ]]; then
if ! [[ -e ".envrc" ]]; then
echo "Error, no .envrc found in current directory" echo "Error, no .envrc found in current directory"
exit 1 exit 1
fi fi
@@ -98,19 +80,19 @@ case $cmd in
if [[ -n $DIRENV_DIR ]]; then if [[ -n $DIRENV_DIR ]]; then
# run self in clean environment (i.e. outside of the current direnv # run self in clean environment (i.e. outside of the current direnv
# environment) # environment)
echo "Switching to clean environment"
exec direnv exec /proc "${BASH_SOURCE[0]}" "$@" exec direnv exec /proc "${BASH_SOURCE[0]}" "$@"
fi fi
echo "Re-creating cache" echo "Re-creating cache"
mkdir -p "$(dirname ${cacheFile})" mkdir -p "$(dirname ${cacheFile})"
dumpEnv > "${cacheFile}.pre" dumpEnv > "${cacheFile}.pre"
source <(direnv stdlib) source <(direnv stdlib)
echo "Re-creating recipe" if [[ .envrc -nt ${cacheFile}.recipe ]]; then
direnv exec . true > /dev/null direnv exec . true > /dev/null
fi
source "${cacheFile}.recipe" source "${cacheFile}.recipe"
dumpEnv > "${cacheFile}.post" dumpEnv > "${cacheFile}.post"
diffEnvs "${cacheFile}.pre" "${cacheFile}.post" > "$cacheFile" diffEnvs "${cacheFile}.pre" "${cacheFile}.post" > "$cacheFile"
rm "${cacheFile}.pre" "${cacheFile}.post" # rm "${cacheFile}.pre" "${cacheFile}.post"
echo "Environment cached in $cacheFile, telling direnv to reload" echo "Environment cached in $cacheFile, telling direnv to reload"
direnv reload direnv reload
;; ;;
@@ -122,7 +104,6 @@ case $cmd in
if [[ -e "$cacheFile" ]]; then if [[ -e "$cacheFile" ]]; then
rm "$cacheFile" rm "$cacheFile"
fi fi
direnv reload
;; ;;
*) *)
usage usage

View File

@@ -14,3 +14,4 @@ cache() {
echo "Environment not cached" echo "Environment not cached"
fi fi
} }

View File

@@ -1,2 +0,0 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.callPackage ./. {}

View File

@@ -1,33 +0,0 @@
# -*- mode: sh -*-
function initDirenv {
# direnv needs a writable home:
run "export HOME=$(pwd)"
run 'eval "$(direnv hook bash)"'
}
function test_simpleEnvrcWorks {
initDirenv
writeFile .envrc <<EOF
export FOO=bar
EOF
run "direnv allow"
assert "$(getEnv FOO)" == bar
}
function test_direnvExecRunsInCleanEnv {
initDirenv
run "export FOO=baz"
writeFile .envrc <<EOF
export FOO=bar
EOF
run "direnv allow"
assert "$(getEnv FOO)" == bar
#captureShell 'echo $FOO'
#captureShell 'direnv exec /proc env | grep FOO'
assert "$(captureShell 'direnv exec /proc env | grep FOO')" == "FOO=baz"
}