Code

t/t7811-grep-open.sh: ensure fake "less" is made executable
[git.git] / t / t7811-grep-open.sh
1 #!/bin/sh
3 test_description='git grep --open-files-in-pager
4 '
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-pager.sh
8 unset PAGER GIT_PAGER
10 test_expect_success 'setup' '
11         test_commit initial grep.h "
12 enum grep_pat_token {
13         GREP_PATTERN,
14         GREP_PATTERN_HEAD,
15         GREP_PATTERN_BODY,
16         GREP_AND,
17         GREP_OPEN_PAREN,
18         GREP_CLOSE_PAREN,
19         GREP_NOT,
20         GREP_OR,
21 };" &&
23         test_commit add-user revision.c "
24         }
25         if (seen_dashdash)
26                 read_pathspec_from_stdin(revs, &sb, prune);
27         strbuf_release(&sb);
28 }
30 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
31 {
32         append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what);
33 " &&
35         mkdir subdir &&
36         test_commit subdir subdir/grep.c "enum grep_pat_token" &&
38         test_commit uninteresting unrelated "hello, world" &&
40         echo GREP_PATTERN >untracked
41 '
43 test_expect_success SIMPLEPAGER 'git grep -O' '
44         cat >$less <<-\EOF &&
45         #!/bin/sh
46         printf "%s\n" "$@" >pager-args
47         EOF
48         chmod +x $less &&
49         cat >expect.less <<-\EOF &&
50         +/*GREP_PATTERN
51         grep.h
52         EOF
53         echo grep.h >expect.notless &&
54         >empty &&
56         PATH=.:$PATH git grep -O GREP_PATTERN >out &&
57         {
58                 test_cmp expect.less pager-args ||
59                 test_cmp expect.notless pager-args
60         } &&
61         test_cmp empty out
62 '
64 test_expect_success 'git grep -O --cached' '
65         test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&
66         grep open-files-in-pager msg
67 '
69 test_expect_success 'git grep -O --no-index' '
70         rm -f expect.less pager-args out &&
71         cat >expect <<-\EOF &&
72         grep.h
73         untracked
74         EOF
75         >empty &&
77         (
78                 GIT_PAGER='\''printf "%s\n" >pager-args'\'' &&
79                 export GIT_PAGER &&
80                 git grep --no-index -O GREP_PATTERN >out
81         ) &&
82         test_cmp expect pager-args &&
83         test_cmp empty out
84 '
86 test_expect_success 'setup: fake "less"' '
87         cat >less <<-\EOF &&
88         #!/bin/sh
89         printf "%s\n" "$@" >actual
90         EOF
91         chmod +x less
92 '
94 test_expect_success 'git grep -O jumps to line in less' '
95         cat >expect <<-\EOF &&
96         +/*GREP_PATTERN
97         grep.h
98         EOF
99         >empty &&
101         GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
102         test_cmp expect actual &&
103         test_cmp empty out &&
105         git grep -O./less GREP_PATTERN >out2 &&
106         test_cmp expect actual &&
107         test_cmp empty out2
110 test_expect_success 'modified file' '
111         rm -f actual &&
112         cat >less <<-\EOF &&
113         #!/bin/sh
114         printf "%s\n" "$@" >actual
115         EOF
116         chmod +x $less &&
117         cat >expect <<-\EOF &&
118         +/*enum grep_pat_token
119         grep.h
120         revision.c
121         subdir/grep.c
122         unrelated
123         EOF
124         >empty &&
126         echo "enum grep_pat_token" >unrelated &&
127         test_when_finished "git checkout HEAD unrelated" &&
128         GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
129         test_cmp expect actual &&
130         test_cmp empty out
133 test_expect_success 'run from subdir' '
134         rm -f actual &&
135         echo grep.c >expect &&
136         >empty &&
138         (
139                 cd subdir &&
140                 export GIT_PAGER &&
141                 GIT_PAGER='\''printf "%s\n" >../args'\'' &&
142                 git grep -O "enum grep_pat_token" >../out &&
143                 git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2
144         ) &&
145         case $(cat dir) in
146         *subdir)
147                 : good
148                 ;;
149         *)
150                 false
151                 ;;
152         esac &&
153         test_cmp expect args &&
154         test_cmp empty out &&
155         test_cmp empty out2
158 test_done