From 8a1fe788f88163b86322c859bbeb0226059c1572 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 6 Mar 2017 02:19:50 +0300 Subject: [PATCH] Adds fix to the `changes` command Changes: 1. Fixes #80, there was a pipe to null, which broke the output 2. Also changed the `diff` formating 3. Also added test case to test deletions --- .travis.yml | 2 +- src/_utils/_git_secret_tools.sh | 6 +++--- src/commands/git_secret_changes.sh | 7 +++---- tests/test_changes.bats | 24 ++++++++++++++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38cb4b9c..8efa14c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ matrix: language: ruby - os: linux env: GITSECRET_DIST="none"; GITSECRET_GPG_DEP="gnupg"; SECRETS_GPG_COMMAND="gpg" - sudo: required + sudo: false language: ruby - os: linux env: GITSECRET_DIST="none"; GITSECRET_GPG_DEP="gnupg2"; SECRETS_GPG_COMMAND="gpg2" diff --git a/src/_utils/_git_secret_tools.sh b/src/_utils/_git_secret_tools.sh index 2d6ba5f4..11a704f6 100644 --- a/src/_utils/_git_secret_tools.sh +++ b/src/_utils/_git_secret_tools.sh @@ -432,9 +432,9 @@ function _decrypt { fi if [[ ! -z "$passphrase" ]]; then - echo "$passphrase" | $base --batch --yes --no-tty --passphrase-fd 0 \ - "$encrypted_filename" > /dev/null 2>&1 + echo "$passphrase" | $base --quiet --batch --yes --no-tty --passphrase-fd 0 \ + "$encrypted_filename" else - $base "$encrypted_filename" > /dev/null 2>&1 + $base --quiet "$encrypted_filename" fi } diff --git a/src/commands/git_secret_changes.sh b/src/commands/git_secret_changes.sh index 96bb46a7..b3a84f06 100644 --- a/src/commands/git_secret_changes.sh +++ b/src/commands/git_secret_changes.sh @@ -31,17 +31,16 @@ function changes { for filename in $filenames; do local decrypted - local content local diff_result # Now we have all the data required: decrypted=$(_decrypt "$filename" "0" "0" "$homedir" "$passphrase") - content=$(cat "$filename") # Let's diff the result: - diff_result=$(diff <(echo "$decrypted") <(echo "$content")) || true + diff_result=$(diff -u <(echo "$decrypted") "$filename") || true # There was a bug in the previous version, since `diff` returns # exit code `1` when the files are different. - echo "changes in ${filename}: ${diff_result}" + echo "changes in ${filename}:" + echo "${diff_result}" done } diff --git a/tests/test_changes.bats b/tests/test_changes.bats index 21c45a36..8a76edca 100644 --- a/tests/test_changes.bats +++ b/tests/test_changes.bats @@ -40,7 +40,22 @@ function teardown { # Testing that output has both filename and changes: [[ "$output" == *"changes in $FILE_TO_HIDE"* ]] - [[ "$output" == *"$new_content"* ]] + [[ "$output" == *"+$new_content"* ]] +} + + +@test "run 'changes' with one file changed (with deletions)" { + local password=$(test_user_password "$TEST_DEFAULT_USER") + local new_content="replace" + echo "$new_content" > "$FILE_TO_HIDE" + + run git secret changes -d "$TEST_GPG_HOMEDIR" -p "$password" "$FILE_TO_HIDE" + [ "$status" -eq 0 ] + + # Testing that output has both filename and changes: + [[ "$output" == *"changes in $FILE_TO_HIDE"* ]] + [[ "$output" == *"-$FILE_CONTENTS"* ]] + [[ "$output" == *"+$new_content"* ]] } @@ -64,12 +79,13 @@ function teardown { # Testing that output has both filename and changes: [[ "$output" == *"changes in $FILE_TO_HIDE"* ]] - [[ "$output" == *"$new_content"* ]] + [[ "$output" == *"+$new_content"* ]] [[ "$output" == *"changes in $SECOND_FILE_TO_HIDE"* ]] [[ "$output" == *"$second_file_to_hide"* ]] } + @test "run 'changes' with multiple selected files changed" { local password=$(test_user_password "$TEST_DEFAULT_USER") local new_content="new content" @@ -84,8 +100,8 @@ function teardown { # Testing that output has both filename and changes: [[ "$output" == *"changes in $FILE_TO_HIDE"* ]] - [[ "$output" == *"$new_content"* ]] + [[ "$output" == *"+$new_content"* ]] [[ "$output" == *"changes in $SECOND_FILE_TO_HIDE"* ]] - [[ "$output" == *"$second_file_to_hide"* ]] + [[ "$output" == *"+$second_file_to_hide"* ]] }