Debian Codegraph @

cli/tests/test_file_search_command.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::common::TestEnvironment;
16
17 #[test]
18 fn test_file_search() {
19 let test_env = TestEnvironment::default();
20 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
21 let work_dir = test_env.work_dir("repo");
22
23 work_dir.write_file("file1", "-foo-");
24 work_dir.write_file("file2", "-bar-");
25 work_dir.run_jj(["new"]).success();
26 work_dir.create_dir("dir");
27 work_dir.write_file("dir/file3", "-foobar-");
28
29 // Searches all files in the current revision by default and prints each
30 // matched line prefixed by the file path
31 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*foo*"]);
32 insta::assert_snapshot!(output.normalize_backslash(), @"
33 dir/file3:-foobar-
34 file1:-foo-
35 [EOF]
36 ");
37
38 // --name-only restores path-only output
39 let output = work_dir.run_jj(["file", "search", "--name-only", "--pattern=glob:*foo*"]);
40 insta::assert_snapshot!(output.normalize_backslash(), @"
41 dir/file3
42 file1
43 [EOF]
44 ");
45
46 // Matches only the whole line for glob pattern
47 let output = work_dir.run_jj(["file", "search", "--pattern=glob:foo"]);
48 insta::assert_snapshot!(output.normalize_backslash(), @"");
49
50 // Can search files in another revision
51 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*foo*", "-r=@-"]);
52 insta::assert_snapshot!(output.normalize_backslash(), @"
53 file1:-foo-
54 [EOF]
55 ");
56
57 // Can filter by path
58 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*foo*", "dir"]);
59 insta::assert_snapshot!(output.normalize_backslash(), @"
60 dir/file3:-foobar-
61 [EOF]
62 ");
63
64 // Warning if path doesn't exist
65 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*foo*", "file9"]);
66 insta::assert_snapshot!(output.normalize_backslash(), @"
67 ------- stderr -------
68 Warning: No matching entries for paths: file9
69 [EOF]
70 ");
71
72 // The default is regex
73 let output = work_dir.run_jj(["file", "search", "--pattern=f.o"]);
74 insta::assert_snapshot!(output.normalize_backslash(), @"
75 dir/file3:-foobar-
76 file1:-foo-
77 [EOF]
78 ");
79
80 // Can specify the kind
81 let output = work_dir.run_jj(["file", "search", "--pattern=glob-i:*foo*"]);
82 insta::assert_snapshot!(output.normalize_backslash(), @"
83 dir/file3:-foobar-
84 file1:-foo-
85 [EOF]
86 ");
87
88 // The part before the first colon is treated as the kind.
89 let output = work_dir.run_jj(["file", "search", "--pattern=foo:bar"]);
90 insta::assert_snapshot!(output.normalize_stderr_exit_status(), @"
91 ------- stderr -------
92 Error: Invalid string pattern kind `foo:`
93 [EOF]
94 [exit status: 2]
95 ");
96
97 // Colons can be in the pattern part.
98 let output = work_dir.run_jj(["file", "search", "--pattern=regex-i:foo:bar"]);
99 insta::assert_snapshot!(output.normalize_backslash(), @"");
100
101 // Prints every matched line, not just the first
102 work_dir.write_file("multi", "hit-one\nmiss\nhit-two\nhit-three\n");
103 let output = work_dir.run_jj(["file", "search", "--pattern=hit", "multi"]);
104 insta::assert_snapshot!(output.normalize_backslash(), @"
105 multi:hit-one
106 multi:hit-two
107 multi:hit-three
108 [EOF]
109 ");
110 // --name-only collapses the same file to a single line
111 let output = work_dir.run_jj(["file", "search", "--name-only", "--pattern=hit", "multi"]);
112 insta::assert_snapshot!(output.normalize_backslash(), @"
113 multi
114 [EOF]
115 ");
116
117 // -n prefixes each match with its 1-based line number
118 let output = work_dir.run_jj(["file", "search", "-n", "--pattern=hit", "multi"]);
119 insta::assert_snapshot!(output.normalize_backslash(), @"
120 multi:1:hit-one
121 multi:3:hit-two
122 multi:4:hit-three
123 [EOF]
124 ");
125 }
126
127 #[test]
128 fn test_file_search_conflicts() {
129 let test_env = TestEnvironment::default();
130 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
131 let work_dir = test_env.work_dir("repo");
132
133 work_dir.write_file("file1", "-foo-\n");
134 work_dir.run_jj(["new"]).success();
135 work_dir.write_file("file1", "-bar-\n");
136 work_dir.run_jj(["new"]).success();
137 work_dir.write_file("file1", "-baz-\n");
138 work_dir.run_jj(["rebase", "-r=@", "-B=@-"]).success();
139
140 // Test the setup
141 insta::assert_snapshot!(work_dir.read_file("file1"), @r"
142 <<<<<<< conflict 1 of 1
143 %%%%%%% diff from: rlvkpnrz 60901f47 (parents of rebased revision)
144 \\\\\\\ to: qpvuntsm fae24a95 (rebase destination)
145 --bar-
146 +-foo-
147 +++++++ kkmpptxz 51957a05 (rebased revision)
148 -baz-
149 >>>>>>> conflict 1 of 1 ends
150 ");
151
152 // Matches positive terms (one match per matching add side)
153 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*foo*"]);
154 insta::assert_snapshot!(output.normalize_backslash(), @"
155 file1:-foo-
156 [EOF]
157 ");
158 // --name-only collapses per-file even with a conflict
159 let output = work_dir.run_jj(["file", "search", "--name-only", "--pattern=glob:*foo*"]);
160 insta::assert_snapshot!(output.normalize_backslash(), @"
161 file1
162 [EOF]
163 ");
164 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*bar*"]);
165 insta::assert_snapshot!(output.normalize_backslash(), @"");
166 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*baz*"]);
167 insta::assert_snapshot!(output.normalize_backslash(), @"
168 file1:-baz-
169 [EOF]
170 ");
171
172 // A pattern that matches on multiple add sides: each matching side emits
173 // one line; with --name-only the path is deduped to a single output.
174 let output = work_dir.run_jj(["file", "search", "--pattern=regex:-(foo|baz)-"]);
175 insta::assert_snapshot!(output.normalize_backslash(), @"
176 file1:-foo-
177 file1:-baz-
178 [EOF]
179 ");
180 let output = work_dir.run_jj([
181 "file",
182 "search",
183 "--name-only",
184 "--pattern=regex:-(foo|baz)-",
185 ]);
186 insta::assert_snapshot!(output.normalize_backslash(), @"
187 file1
188 [EOF]
189 ");
190
191 // -n numbers lines within each conflict side independently, so matches on
192 // different sides can share a line number.
193 let output = work_dir.run_jj(["file", "search", "-n", "--pattern=regex:-(foo|baz)-"]);
194 insta::assert_snapshot!(output.normalize_backslash(), @"
195 file1:1:-foo-
196 file1:1:-baz-
197 [EOF]
198 ");
199
200 // Doesn't match the conflict markers
201 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*%%%*"]);
202 insta::assert_snapshot!(output.normalize_backslash(), @"");
203
204 // Doesn't list file if the pattern doesn't match
205 let output = work_dir.run_jj(["file", "search", "--pattern=glob:*qux*"]);
206 insta::assert_snapshot!(output.normalize_backslash(), @"");
207 }