From eefa10623a2ebcdb791acbea876e4270284f2fa4 Mon Sep 17 00:00:00 2001 From: Josh Rabinowitz Date: Wed, 23 Mar 2022 14:13:53 -0400 Subject: [PATCH] changes to .gitignore usage and testing in response to #789 (#791) * test .gitignore has expected line count, for #792 * let 'add' append filenames to .gitignore in tests * add comments related to #789 * fix test to allow for more output from 'add' * improve error message output * allow for extra output from 'add' in test * tweaks as per shellcheck lint * improve comments, cleanup code * update changelog * describe test better --- CHANGELOG.md | 2 ++ src/commands/git_secret_add.sh | 3 ++- tests/_test_base.bash | 9 +++++---- tests/test_add.bats | 20 ++++++++------------ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed0885d7..9d1693fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Fix adding newlines to `.gitignore` entries (#643) - Fix `cat` and `reveal` on named files while in repo subdir (#710) - Fix for `removeperson` if same email is present multiple times (#638) +- Correct error message about files missing from .gitignore ### Misc @@ -22,6 +23,7 @@ - Upgrade bats-core to v1.5.0 (#755) - Update docs for use with CI/CD server (#675) - Test, and build RPMS, with Rocky and Alma Linux instead of CentOS (#765) +- Test .gitignore contents (#792) ## 0.4.0 diff --git a/src/commands/git_secret_add.sh b/src/commands/git_secret_add.sh index 389d1534..27c2e9b9 100644 --- a/src/commands/git_secret_add.sh +++ b/src/commands/git_secret_add.sh @@ -61,7 +61,8 @@ function add { if [[ ! "${#not_ignored[@]}" -eq 0 ]]; then # And show them all at once. local message - message="these files are not in .gitignore: $*" + message="these files are not in .gitignore: ${not_ignored[*]}" + # NOTE: message has spaces between filenames, which can be ambiguous if files also have spaces if [[ "$auto_ignore" -eq 0 ]]; then # This file is not ignored. user don't want it to be added automatically. diff --git a/tests/_test_base.bash b/tests/_test_base.bash index 140f2092..fe0dbcb5 100644 --- a/tests/_test_base.bash +++ b/tests/_test_base.bash @@ -87,9 +87,12 @@ function file_has_line { local filename="$2" # required local contains - contains=$(grep -Fqw "$key" "$filename"; echo $?) + # -F means 'Interpret PATTERN as a list of fixed strings' (not regexen) + # -q means 'do not write anything to standard output. Exit immediately with zero status if any match or error is found' + # -w means 'Select only those lines containing matches that form whole words' + contains=$(grep -Fqw "$key" "$filename"; echo $?) # this may not always be correct, especially because of -q - # 0 on contains, 1 or 2 for error. + # 0 on contains or error, 1 for not contains. We cannot get 2 because of grep -q (see above and 'man grep') echo "$contains" } @@ -258,7 +261,6 @@ function set_state_secret_add { local filename="$1" local content="$2" echo "$content" > "$filename" # we add a newline - echo "$filename" >> '.gitignore' git secret add "$filename" >> "$TEST_GPG_OUTPUT_FILE" 2>&1 } @@ -267,7 +269,6 @@ function set_state_secret_add_without_newline { local filename="$1" local content="$2" echo -n "$content" > "$filename" # we do not add a newline - echo "$filename" >> '.gitignore' git secret add "$filename" >> "$TEST_GPG_OUTPUT_FILE" 2>&1 } diff --git a/tests/test_add.bats b/tests/test_add.bats index c0f1b494..126320fe 100644 --- a/tests/test_add.bats +++ b/tests/test_add.bats @@ -23,7 +23,6 @@ function teardown { # Preparations: local filename="$TEST_DEFAULT_FILENAME" echo "content" > "$filename" - echo "$filename" > ".gitignore" run git secret add "$filename" [ "$status" -eq 0 ] @@ -143,7 +142,6 @@ function teardown { mkdir -p "$sibling" echo "content" > "$test_file" - echo "$test_file" > ".gitignore" cd "$sibling" @@ -179,7 +177,6 @@ function teardown { mkdir -p "$test_dir" touch "$test_dir/$test_file" echo "content" > "$test_dir/$test_file" - echo "$test_dir/$test_file" > ".gitignore" # Testing: run git secret add "$test_dir/$test_file" @@ -195,13 +192,12 @@ function teardown { # Preparations: local filename="$TEST_DEFAULT_FILENAME" echo "content" > "$filename" - echo "$filename" > ".gitignore" # Testing: run git secret add "$filename" run git secret add "$filename" [ "$status" -eq 0 ] - [ "$output" = "git-secret: 0 item(s) added." ] + [[ "$output" = *"git-secret: 0 item(s) added."* ]] # Ensuring that path mappings was set correctly: local path_mappings @@ -216,20 +212,23 @@ function teardown { } -@test "run 'add' for multiple files" { +@test "run 'add' for multiple files, and test .gitignore contents" { # Preparations: local filename1="$TEST_DEFAULT_FILENAME" echo "content1" > "$filename1" - echo "$filename1" > ".gitignore" local filename2="$TEST_SECOND_FILENAME" echo "content2" > "$filename2" - echo "$filename2" >> ".gitignore" # Testing: run git secret add "$filename1" "$filename2" [ "$status" -eq 0 ] - [ "$output" = "git-secret: 2 item(s) added." ] + [[ "$output" = *"git-secret: 2 item(s) added."* ]] # there may be additional lines too + + # test .gitignore has 4 lines as expected + local gitignore_linecount + gitignore_linecount=$(wc -l < .gitignore) + [ "$gitignore_linecount" -eq 4 ] # two added by `git secret init`, and one for each `added` file # Cleaning up: rm "$filename1" "$filename2" ".gitignore" @@ -239,11 +238,9 @@ function teardown { # Preparations: local filename1="$TEST_DEFAULT_FILENAME" echo "content1" > "$filename1" - echo "$filename1" > ".gitignore" local filename2="$TEST_SECOND_FILENAME" echo "content2" > "$filename2" - echo "$filename2" >> ".gitignore" # Testing: run git secret add -v "$filename1" "$filename2" @@ -261,7 +258,6 @@ function teardown { # Preparations: local filename="$TEST_FOURTH_FILENAME" echo "content" > "$filename" - echo "$filename" > ".gitignore" run git secret add "$filename" [ "$status" -eq 0 ]