1
0
mirror of https://github.com/Tarrasch/zsh-autoenv.git synced 2025-12-15 02:35:12 +02:00

Automatically upgrade v1 hashes (SHA-1) to v2 (cksum) (#55)

A new parameter is added to `_autoenv_hash_pair` to specify the version, defaulting to the latest (2). It outputs a `cksum`-based hash for version 2 and `shasum`-based for version 1.
Moves logic to check for an entry in `$AUTOENV_AUTH_FILE` into its own function (`_autoenv_authorized_pair`), as it may need to be called twice.
Modifies `_autoenv_authorized_env_file` to check for v1 entries when v2 fails.

Fixes #53. Alternative implementation to #54.
This commit is contained in:
Rob Speed
2017-01-10 06:07:39 -05:00
committed by Daniel Hahler
parent 398b6f4f54
commit dfb5648505
4 changed files with 97 additions and 26 deletions

View File

@@ -35,41 +35,41 @@ Now adding some auth pair.
$ echo first > first
$ _autoenv_authorize first
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:2 (glob)
And a second one.
$ echo second > second
$ _autoenv_authorize second
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:2 (glob)
And a third.
$ echo third > third
$ _autoenv_authorize third
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:451243482.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:451243482.6:2 (glob)
Re-add the second one, with the same hash.
$ _autoenv_authorize second
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:451243482.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:2715464726.6:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:451243482.6:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:2 (glob)
Re-add the first one, with a new hash.
$ echo one more line >> first
$ _autoenv_authorize first
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/third:451243482.6:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:3620404822.20:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:451243482.6:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:594940475.7:2 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:3620404822.20:2 (glob)
}}}

View File

@@ -28,9 +28,9 @@ if [[ -f $AUTOENV_AUTH_FILE ]]; then
echo -n >| $AUTOENV_AUTH_FILE
fi
# Add file $1 (with optional hash $2) to authentication file.
# Add file ($1), version ($2), and optional hash ($3) to authentication file.
test_autoenv_add_to_env() {
_autoenv_hash_pair $1 ${2:-} >>| $AUTOENV_AUTH_FILE
_autoenv_hash_pair $1 1 ${2:-} >>| $AUTOENV_AUTH_FILE
}
# Add enter and leave env files to authentication file.

43
tests/upgrade_hash.t Normal file
View File

@@ -0,0 +1,43 @@
Tests for upgrading hashes.
$ source $TESTDIR/setup.zsh || return 1
$ mkdir -p sub
$ mkdir -p ${AUTOENV_AUTH_FILE:h}
Create a single v1 hash entry.
$ echo 'echo ENTERED' > sub/$AUTOENV_FILE_ENTER
$ echo 'echo LEAVE' > sub/$AUTOENV_FILE_LEAVE
$ echo :$PWD/sub/$AUTOENV_FILE_ENTER:4c403f1870af2ab5472370a42b6b2b488cefe83c:1 > $AUTOENV_AUTH_FILE
$ cd sub
ENTERED
Hashes should get upgraded automatically.
This also tests that there are no empty lines being added to the auth file when
de-authenticating the old hash.
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/upgrade_hash.t/sub/.autoenv.zsh:3679467995.13:2 (glob)
Re-create auth file with v1 hashes for both auth files.
$ echo :$PWD/$AUTOENV_FILE_LEAVE:882cf0333ea3c35537c9459c08d8987f25087ea9:1 > $AUTOENV_AUTH_FILE
$ echo :$PWD/$AUTOENV_FILE_ENTER:4c403f1870af2ab5472370a42b6b2b488cefe83c:1 >> $AUTOENV_AUTH_FILE
Only the leave file's hash should get updated.
$ cd ..
LEAVE
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/upgrade_hash.t/sub/.autoenv.zsh:4c403f1870af2ab5472370a42b6b2b488cefe83c:1 (glob)
:/tmp/cramtests-*/upgrade_hash.t/sub/.autoenv_leave.zsh:803077150.11:2 (glob)
The enter file's hash should get updated.
$ cd sub
ENTERED
$ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/upgrade_hash.t/sub/.autoenv_leave.zsh:803077150.11:2 (glob)
:/tmp/cramtests-*/upgrade_hash.t/sub/.autoenv.zsh:3679467995.13:2 (glob)