From 5e18125f04ed1f59f1c03a7a6a573d64674aed22 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 27 Nov 2014 13:33:04 +0100 Subject: [PATCH] Improve interactive prompt - Revert usage of "read -q" for authentication (#10) - Change format of "not authenticated" message (#9) --- autoenv.zsh | 19 +++++++++++-------- tests/autoenv.t | 39 +++++++++++++++++---------------------- tests/leave.t | 21 ++++++++++++--------- tests/recurse-upwards.t | 14 ++++++++------ tests/setup.sh | 3 --- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/autoenv.zsh b/autoenv.zsh index 034b7fe..f770d8a 100644 --- a/autoenv.zsh +++ b/autoenv.zsh @@ -154,10 +154,14 @@ _autoenv_deauthorize() { } # This function can be mocked in tests -_autoenv_read_answer() { +_autoenv_ask_for_yes() { local answer - read $=_AUTOENV_TEST_READ_ARGS -q answer - echo $answer + read answer + if [[ $answer == "yes" ]]; then + return 0 + else + return 1 + fi } # Args: 1: absolute path to env file (resolved symlinks). @@ -166,7 +170,8 @@ _autoenv_check_authorized_env_file() { return 1 fi if ! _autoenv_authorized_env_file $1; then - echo "Attempting to load unauthorized env file: $1" + echo "Attempting to load unauthorized env file!" + command ls -l $1 echo "" echo "**********************************************" echo "" @@ -174,11 +179,9 @@ _autoenv_check_authorized_env_file() { echo "" echo "**********************************************" echo "" - echo -n "Would you like to authorize it? [y/N] " + echo -n "Would you like to authorize it? (type 'yes') " - local answer=$(_autoenv_read_answer) - echo - if [[ $answer != 'y' ]]; then + if ! _autoenv_ask_for_yes; then return 1 fi diff --git a/tests/autoenv.t b/tests/autoenv.t index f78560a..19d0592 100644 --- a/tests/autoenv.t +++ b/tests/autoenv.t @@ -14,9 +14,10 @@ Now try to make it accept it $ unset _autoenv_stack_entered $ rm $AUTOENV_ENV_FILENAME - $ _autoenv_read_answer() { echo 'y' } + $ _autoenv_ask_for_yes() { echo "yes" } $ cd . - Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/autoenv.t/.env (glob) ********************************************** @@ -24,16 +25,13 @@ Now try to make it accept it ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') yes ENTERED +The last "ENTERED" is because it executed the command. - - -The last "ENTERED" is because it executed the command - -Now lets see that it actually checks the shasum value +Now lets see that it actually checks the shasum value. $ unset _autoenv_stack_entered $ cd . @@ -43,7 +41,8 @@ Now lets see that it actually checks the shasum value $ rm $AUTOENV_ENV_FILENAME $ test_autoenv_add_to_env $PWD/.env mischief $ cd . - Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/autoenv.t/.env (glob) ********************************************** @@ -51,20 +50,18 @@ Now lets see that it actually checks the shasum value ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') yes ENTERED - - - Now, will it take no for an answer? $ unset _autoenv_stack_entered $ rm $AUTOENV_ENV_FILENAME - $ _autoenv_read_answer() { echo 'n' } + $ _autoenv_ask_for_yes() { echo "no"; return 1 } $ cd . - Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/autoenv.t/.env (glob) ********************************************** @@ -72,16 +69,14 @@ Now, will it take no for an answer? ********************************************** - Would you like to authorize it? [y/N] - - - + Would you like to authorize it? (type 'yes') no -Lets also try one more time to ensure it didnt add it +Lets also try one more time to ensure it didn't add it. $ cd . - Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/autoenv.t/.env (glob) ********************************************** @@ -89,4 +84,4 @@ Lets also try one more time to ensure it didnt add it ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') no diff --git a/tests/leave.t b/tests/leave.t index d5531b1..2e4e360 100644 --- a/tests/leave.t +++ b/tests/leave.t @@ -9,9 +9,10 @@ Lets set a simple .env action Change to the directory. - $ _autoenv_read_answer() { echo 'y' } + $ _autoenv_ask_for_yes() { echo "yes"; return 0 } $ cd . - Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/leave.t/sub/.env (glob) ********************************************** @@ -19,15 +20,16 @@ Change to the directory. ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') yes ENTERED Leave the directory and answer "no". - $ _autoenv_read_answer() { echo 'n' } + $ _autoenv_ask_for_yes() { echo "no"; return 1 } $ cd .. - Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env.leave (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/leave.t/sub/.env.leave (glob) ********************************************** @@ -35,14 +37,15 @@ Leave the directory and answer "no". ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') no $ cd sub ENTERED - $ _autoenv_read_answer() { echo 'y' } + $ _autoenv_ask_for_yes() { echo "yes"; return 0 } $ cd .. - Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env.leave (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/leave.t/sub/.env.leave (glob) ********************************************** @@ -50,7 +53,7 @@ Leave the directory and answer "no". ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') yes LEFT diff --git a/tests/recurse-upwards.t b/tests/recurse-upwards.t index 7a6f28f..f4c2780 100644 --- a/tests/recurse-upwards.t +++ b/tests/recurse-upwards.t @@ -110,10 +110,11 @@ Changing the root .env should trigger re-authentication via autoenv_source_paren First, let's answer "no". $ echo "echo NEW" > .env - $ _autoenv_read_answer() { echo 'n' } + $ _autoenv_ask_for_yes() { echo "no"; return 1 } $ cd sub autoenv_source_parent_from_sub: - Attempting to load unauthorized env file: /tmp/cramtests-*/recurse-upwards.t/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/recurse-upwards.t/.env (glob) ********************************************** @@ -121,7 +122,7 @@ First, let's answer "no". ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') no ENTERED_sub: PWD:sub from:recurse-upwards.t to:sub ENTER2 done_sub @@ -129,7 +130,7 @@ First, let's answer "no". Now with "yes". This currently does not trigger re-execution of the .env file. - $ _autoenv_read_answer() { echo 'y' } + $ _autoenv_ask_for_yes() { echo "yes"; return 0 } $ cd . Touching the .env file will now source the parent env file. @@ -137,7 +138,8 @@ Touching the .env file will now source the parent env file. $ touch -t 201401010104 .env $ cd . autoenv_source_parent_from_sub: - Attempting to load unauthorized env file: /tmp/cramtests-*/recurse-upwards.t/.env (glob) + Attempting to load unauthorized env file! + -* /tmp/cramtests-*/recurse-upwards.t/.env (glob) ********************************************** @@ -145,7 +147,7 @@ Touching the .env file will now source the parent env file. ********************************************** - Would you like to authorize it? [y/N] + Would you like to authorize it? (type 'yes') yes NEW ENTERED_sub: PWD:sub from:sub to:sub ENTER2 diff --git a/tests/setup.sh b/tests/setup.sh index d5a4f6e..90bfc51 100644 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -6,9 +6,6 @@ # Reset any authentication. echo -n > $AUTOENV_ENV_FILENAME -# Inject timeout for `read` while running tests. -_AUTOENV_TEST_READ_ARGS='-t 1' - # Add file $1 (with optional hash $2) to authentication file. test_autoenv_add_to_env() { _autoenv_hash_pair $1 $2 >> $AUTOENV_ENV_FILENAME -- 2.44.0