#!/bin/sh test_description=check-ignore . ./test-lib.sh init_vars () { global_excludes="$(pwd)/global-excludes" } enable_global_excludes () { init_vars && git config core.excludesfile "$global_excludes" } expect_in () { dest="$HOME/expected-$1" text="$2" if test -z "$text" then >"$dest" # avoid newline else echo "$text" >"$dest" fi } expect () { expect_in stdout "$1" } expect_from_stdin () { cat >"$HOME/expected-stdout" } test_stderr () { expected="$1" expect_in stderr "$1" && test_cmp "$HOME/expected-stderr" "$HOME/stderr" } stderr_contains () { regexp="$1" if grep "$regexp" "$HOME/stderr" then return 0 else echo "didn't find /$regexp/ in $HOME/stderr" cat "$HOME/stderr" return 1 fi } stderr_empty_on_success () { expect_code="$1" if test $expect_code = 0 then test_stderr "" else # If we expect failure then stderr might or might not be empty # due to --quiet - the caller can check its contents return 0 fi } test_check_ignore () { args="$1" expect_code="${2:-0}" global_args="$3" init_vars && rm -f "$HOME/stdout" "$HOME/stderr" "$HOME/cmd" && echo git $global_args check-ignore $quiet_opt $verbose_opt $args \ >"$HOME/cmd" && test_expect_code "$expect_code" \ git $global_args check-ignore $quiet_opt $verbose_opt $args \ >"$HOME/stdout" 2>"$HOME/stderr" && test_cmp "$HOME/expected-stdout" "$HOME/stdout" && stderr_empty_on_success "$expect_code" } # Runs the same code with 3 different levels of output verbosity, # expecting success each time. Takes advantage of the fact that # check-ignore --verbose output is the same as normal output except # for the extra first column. # # Arguments: # - (optional) prereqs for this test, e.g. 'SYMLINKS' # - test name # - output to expect from -v / --verbose mode # - code to run (should invoke test_check_ignore) test_expect_success_multi () { prereq= if test $# -eq 4 then prereq=$1 shift fi testname="$1" expect_verbose="$2" code="$3" expect=$( echo "$expect_verbose" | sed -e 's/.* //' ) test_expect_success $prereq "$testname" ' expect "$expect" && eval "$code" ' for quiet_opt in '-q' '--quiet' do test_expect_success $prereq "$testname${quiet_opt:+ with $quiet_opt}" " expect '' && $code " done quiet_opt= for verbose_opt in '-v' '--verbose' do test_expect_success $prereq "$testname${verbose_opt:+ with $verbose_opt}" " expect '$expect_verbose' && $code " done verbose_opt= } test_expect_success 'setup' ' init_vars && mkdir -p a/b/ignored-dir a/submodule b && if test_have_prereq SYMLINKS then ln -s b a/symlink fi && ( cd a/submodule && git init && echo a >a && git add a && git commit -m"commit in submodule" ) && git add a/submodule && cat <<-\EOF >.gitignore && one ignored-* top-level-dir/ EOF for dir in . a do : >$dir/not-ignored && : >$dir/ignored-and-untracked && : >$dir/ignored-but-in-index done && git add -f ignored-but-in-index a/ignored-but-in-index && cat <<-\EOF >a/.gitignore && two* *three EOF cat <<-\EOF >a/b/.gitignore && four five # this comment should affect the line numbers six ignored-dir/ # and so should this blank line: !on* !two EOF echo "seven" >a/b/ignored-dir/.gitignore && test -n "$HOME" && cat <<-\EOF >"$global_excludes" && globalone !globaltwo globalthree EOF cat <<-\EOF >>.git/info/exclude per-repo EOF ' ############################################################################ # # test invalid inputs test_expect_success_multi '. corner-case' '' ' test_check_ignore . 1 ' test_expect_success_multi 'empty command line' '' ' test_check_ignore "" 128 && stderr_contains "fatal: no path specified" ' test_expect_success_multi '--stdin with empty STDIN' '' ' test_check_ignore "--stdin" 1 stdin one not-ignored a/one a/not-ignored a/b/on a/b/one a/b/one one "a/b/one two" "a/b/one\"three" a/b/not-ignored a/b/two a/b/twooo globaltwo a/globaltwo a/b/globaltwo b/globaltwo EOF cat <<-\EOF >expected-default one a/one a/b/on a/b/one a/b/one one a/b/one two "a/b/one\"three" a/b/two a/b/twooo globaltwo a/globaltwo a/b/globaltwo b/globaltwo EOF cat <<-EOF >expected-verbose .gitignore:1:one one .gitignore:1:one a/one a/b/.gitignore:8:!on* a/b/on a/b/.gitignore:8:!on* a/b/one a/b/.gitignore:8:!on* a/b/one one a/b/.gitignore:8:!on* a/b/one two a/b/.gitignore:8:!on* "a/b/one\"three" a/b/.gitignore:9:!two a/b/two a/.gitignore:1:two* a/b/twooo $global_excludes:2:!globaltwo globaltwo $global_excludes:2:!globaltwo a/globaltwo $global_excludes:2:!globaltwo a/b/globaltwo $global_excludes:2:!globaltwo b/globaltwo EOF sed -e 's/^"//' -e 's/\\//' -e 's/"$//' stdin | \ tr "\n" "\0" >stdin0 sed -e 's/^"//' -e 's/\\//' -e 's/"$//' expected-default | \ tr "\n" "\0" >expected-default0 sed -e 's/ "/ /' -e 's/\\//' -e 's/"$//' expected-verbose | \ tr ":\t\n" "\0" >expected-verbose0 test_expect_success '--stdin' ' expect_from_stdin stdin ../one ../not-ignored one not-ignored b/on b/one b/one one "b/one two" "b/one\"three" b/two b/not-ignored b/twooo ../globaltwo globaltwo b/globaltwo ../b/globaltwo EOF cat <<-\EOF >expected-default ../one one b/on b/one b/one one b/one two "b/one\"three" b/two b/twooo ../globaltwo globaltwo b/globaltwo ../b/globaltwo EOF cat <<-EOF >expected-verbose .gitignore:1:one ../one .gitignore:1:one one a/b/.gitignore:8:!on* b/on a/b/.gitignore:8:!on* b/one a/b/.gitignore:8:!on* b/one one a/b/.gitignore:8:!on* b/one two a/b/.gitignore:8:!on* "b/one\"three" a/b/.gitignore:9:!two b/two a/.gitignore:1:two* b/twooo $global_excludes:2:!globaltwo ../globaltwo $global_excludes:2:!globaltwo globaltwo $global_excludes:2:!globaltwo b/globaltwo $global_excludes:2:!globaltwo ../b/globaltwo EOF sed -e 's/^"//' -e 's/\\//' -e 's/"$//' stdin | \ tr "\n" "\0" >stdin0 sed -e 's/^"//' -e 's/\\//' -e 's/"$//' expected-default | \ tr "\n" "\0" >expected-default0 sed -e 's/ "/ /' -e 's/\\//' -e 's/"$//' expected-verbose | \ tr ":\t\n" "\0" >expected-verbose0 test_expect_success '--stdin from subdirectory' ' expect_from_stdin