blob: d1ebfd88c7a9a92a956b92347c4193c6b1d08701 [file] [log] [blame]
Johannes Schindelin678e4842010-06-12 11:36:51 -05001#!/bin/sh
2
3test_description='git grep --open-files-in-pager
4'
5
6. ./test-lib.sh
7. "$TEST_DIRECTORY"/lib-pager.sh
8unset PAGER GIT_PAGER
9
10test_expect_success 'setup' '
11 test_commit initial grep.h "
12enum 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};" &&
22
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}
29
30static 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" &&
34
35 mkdir subdir &&
36 test_commit subdir subdir/grep.c "enum grep_pat_token" &&
37
38 test_commit uninteresting unrelated "hello, world" &&
39
40 echo GREP_PATTERN >untracked
41'
42
43test_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 &&
Johannes Schindelin678e4842010-06-12 11:36:51 -050054
55 PATH=.:$PATH git grep -O GREP_PATTERN >out &&
56 {
57 test_cmp expect.less pager-args ||
58 test_cmp expect.notless pager-args
59 } &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +020060 test_must_be_empty out
Johannes Schindelin678e4842010-06-12 11:36:51 -050061'
62
Junio C Hamanoc9ea1182011-04-14 14:36:14 -070063test_expect_success 'git grep -O --cached' '
Johannes Schindelin678e4842010-06-12 11:36:51 -050064 test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&
Junio C Hamanoc9ea1182011-04-14 14:36:14 -070065 test_i18ngrep open-files-in-pager msg
Johannes Schindelin678e4842010-06-12 11:36:51 -050066'
67
68test_expect_success 'git grep -O --no-index' '
69 rm -f expect.less pager-args out &&
70 cat >expect <<-\EOF &&
71 grep.h
72 untracked
73 EOF
Johannes Schindelin678e4842010-06-12 11:36:51 -050074
75 (
76 GIT_PAGER='\''printf "%s\n" >pager-args'\'' &&
77 export GIT_PAGER &&
78 git grep --no-index -O GREP_PATTERN >out
79 ) &&
80 test_cmp expect pager-args &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +020081 test_must_be_empty out
Johannes Schindelin678e4842010-06-12 11:36:51 -050082'
83
84test_expect_success 'setup: fake "less"' '
Brandon Casey6a1b3fc2010-06-21 12:37:14 -050085 cat >less <<-\EOF &&
Johannes Schindelin678e4842010-06-12 11:36:51 -050086 #!/bin/sh
87 printf "%s\n" "$@" >actual
88 EOF
Brandon Casey6a1b3fc2010-06-21 12:37:14 -050089 chmod +x less
Johannes Schindelin678e4842010-06-12 11:36:51 -050090'
91
92test_expect_success 'git grep -O jumps to line in less' '
93 cat >expect <<-\EOF &&
94 +/*GREP_PATTERN
95 grep.h
96 EOF
Johannes Schindelin678e4842010-06-12 11:36:51 -050097
98 GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
99 test_cmp expect actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200100 test_must_be_empty out &&
Johannes Schindelin0af88c12010-06-12 11:39:46 -0500101
102 git grep -O./less GREP_PATTERN >out2 &&
103 test_cmp expect actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200104 test_must_be_empty out2
Johannes Schindelin678e4842010-06-12 11:36:51 -0500105'
106
107test_expect_success 'modified file' '
108 rm -f actual &&
Johannes Schindelin678e4842010-06-12 11:36:51 -0500109 cat >expect <<-\EOF &&
110 +/*enum grep_pat_token
111 grep.h
112 revision.c
113 subdir/grep.c
114 unrelated
115 EOF
116 >empty &&
117
118 echo "enum grep_pat_token" >unrelated &&
119 test_when_finished "git checkout HEAD unrelated" &&
120 GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
121 test_cmp expect actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200122 test_must_be_empty out
Johannes Schindelin678e4842010-06-12 11:36:51 -0500123'
124
Nazri Ramliye7b082a2010-07-02 21:55:06 -0500125test_expect_success 'copes with color settings' '
126 rm -f actual &&
127 echo grep.h >expect &&
128 test_config color.grep always &&
129 test_config color.grep.filename yellow &&
130 test_config color.grep.separator green &&
131 git grep -O'\''printf "%s\n" >actual'\'' GREP_AND &&
132 test_cmp expect actual
133'
134
Johannes Schindelin678e4842010-06-12 11:36:51 -0500135test_expect_success 'run from subdir' '
136 rm -f actual &&
137 echo grep.c >expect &&
Johannes Schindelin678e4842010-06-12 11:36:51 -0500138
139 (
140 cd subdir &&
141 export GIT_PAGER &&
142 GIT_PAGER='\''printf "%s\n" >../args'\'' &&
143 git grep -O "enum grep_pat_token" >../out &&
Johannes Schindelin0af88c12010-06-12 11:39:46 -0500144 git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2
Johannes Schindelin678e4842010-06-12 11:36:51 -0500145 ) &&
146 case $(cat dir) in
147 *subdir)
148 : good
149 ;;
150 *)
151 false
152 ;;
153 esac &&
154 test_cmp expect args &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200155 test_must_be_empty out &&
156 test_must_be_empty out2
Johannes Schindelin678e4842010-06-12 11:36:51 -0500157'
158
159test_done