Debian Codegraph @

cli/tests/test_debug_command.rs

raw
1 // Copyright 2022 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 insta::assert_snapshot;
16 use regex::Regex;
17
18 use crate::common::CommandOutput;
19 use crate::common::TestEnvironment;
20
21 #[test]
22 fn test_debug_fileset() {
23 let test_env = TestEnvironment::default();
24 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
25 let work_dir = test_env.work_dir("repo");
26
27 let output = work_dir.run_jj(["debug", "fileset", "all()"]);
28 assert_snapshot!(output, @"
29 -- Parsed:
30 All
31
32 -- Matcher:
33 EverythingMatcher
34 [EOF]
35 ");
36
37 let output = work_dir.run_jj(["debug", "fileset", "cwd:.."]);
38 assert_snapshot!(output.normalize_backslash(), @r#"
39 ------- stderr -------
40 Error: Failed to parse fileset: Invalid file pattern
41 Caused by:
42 1: --> 1:1
43 |
44 1 | cwd:..
45 | ^----^
46 |
47 = Invalid file pattern
48 2: Path ".." is not in the repo "."
49 3: Invalid component ".." in repo-relative path ".."
50 [EOF]
51 [exit status: 1]
52 "#);
53 }
54
55 #[test]
56 fn test_debug_revset() {
57 let test_env = TestEnvironment::default();
58 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
59 let work_dir = test_env.work_dir("repo");
60
61 let mut insta_settings = insta::Settings::clone_current();
62 insta_settings.add_filter(r"(?m)(^ .*\n)+", " ..\n");
63 let _guard = insta_settings.bind_to_scope();
64
65 let output = work_dir.run_jj(["debug", "revset", "root()"]);
66 assert_snapshot!(output, @"
67 -- Parsed:
68 Root
69
70 -- Resolved:
71 Root
72
73 -- Optimized:
74 Root
75
76 -- Backend:
77 Commits(
78 ..
79 )
80
81 -- Evaluated:
82 RevsetImpl {
83 ..
84 }
85
86 -- Commit IDs:
87 0000000000000000000000000000000000000000
88 [EOF]
89 ");
90
91 let output = work_dir.run_jj(["debug", "revset", "--no-optimize", "root() & ~@"]);
92 assert_snapshot!(output, @"
93 -- Parsed:
94 Intersection(
95 ..
96 )
97
98 -- Resolved:
99 Intersection(
100 ..
101 )
102
103 -- Backend:
104 Intersection(
105 ..
106 )
107
108 -- Evaluated:
109 RevsetImpl {
110 ..
111 }
112
113 -- Commit IDs:
114 0000000000000000000000000000000000000000
115 [EOF]
116 ");
117
118 let output = work_dir.run_jj(["debug", "revset", "--no-resolve", "foo & ~bar"]);
119 assert_snapshot!(output, @"
120 -- Parsed:
121 Intersection(
122 ..
123 )
124
125 -- Optimized:
126 Difference(
127 ..
128 )
129
130 [EOF]
131 ");
132
133 let output = work_dir.run_jj([
134 "debug",
135 "revset",
136 "--no-resolve",
137 "--no-optimize",
138 "foo & ~bar",
139 ]);
140 assert_snapshot!(output, @"
141 -- Parsed:
142 Intersection(
143 ..
144 )
145
146 [EOF]
147 ");
148 }
149
150 #[test]
151 fn test_debug_index() {
152 let test_env = TestEnvironment::default();
153 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
154 let work_dir = test_env.work_dir("repo");
155 let output = work_dir.run_jj(["debug", "index"]);
156 assert_snapshot!(filter_index_stats(output), @"
157 === Commits ===
158 Number of commits: 2
159 Number of merges: 0
160 Max generation number: 1
161 Number of heads: 1
162 Number of changes: 2
163 Stats per level:
164 Level 0:
165 Number of commits: 2
166 Name: [hash]
167 === Changed paths ===
168 Indexed commits: none
169 Stats per level:
170 [EOF]
171 ");
172
173 // Enable changed-path index, index one commit
174 let output = work_dir.run_jj(["debug", "index-changed-paths", "-n1"]);
175 assert_snapshot!(output, @r"
176 ------- stderr -------
177 Finished indexing 1..2 commits.
178 [EOF]
179 ");
180 let output = work_dir.run_jj(["debug", "index"]);
181 assert_snapshot!(filter_index_stats(output), @"
182 === Commits ===
183 Number of commits: 2
184 Number of merges: 0
185 Max generation number: 1
186 Number of heads: 1
187 Number of changes: 2
188 Stats per level:
189 Level 0:
190 Number of commits: 2
191 Name: [hash]
192 === Changed paths ===
193 Indexed commits: 1..2
194 Stats per level:
195 Level 0:
196 Number of commits: 1
197 Number of changed paths: 0
198 Number of paths: 0
199 Name: [hash]
200 [EOF]
201 ");
202 }
203
204 #[test]
205 fn test_debug_reindex() {
206 let test_env = TestEnvironment::default();
207 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
208 let work_dir = test_env.work_dir("repo");
209 work_dir.run_jj(["new"]).success();
210 work_dir.run_jj(["new"]).success();
211 let output = work_dir.run_jj(["debug", "index"]);
212 assert_snapshot!(filter_index_stats(output), @"
213 === Commits ===
214 Number of commits: 4
215 Number of merges: 0
216 Max generation number: 3
217 Number of heads: 1
218 Number of changes: 4
219 Stats per level:
220 Level 0:
221 Number of commits: 3
222 Name: [hash]
223 Level 1:
224 Number of commits: 1
225 Name: [hash]
226 === Changed paths ===
227 Indexed commits: none
228 Stats per level:
229 [EOF]
230 ");
231 let output = work_dir.run_jj(["debug", "reindex"]);
232 insta::assert_snapshot!(output, @r"
233 ------- stderr -------
234 Finished indexing 4 commits.
235 [EOF]
236 ");
237 let output = work_dir.run_jj(["debug", "index"]);
238 assert_snapshot!(filter_index_stats(output), @"
239 === Commits ===
240 Number of commits: 4
241 Number of merges: 0
242 Max generation number: 3
243 Number of heads: 1
244 Number of changes: 4
245 Stats per level:
246 Level 0:
247 Number of commits: 4
248 Name: [hash]
249 === Changed paths ===
250 Indexed commits: none
251 Stats per level:
252 [EOF]
253 ");
254 }
255
256 #[test]
257 fn test_debug_stacked_table() {
258 let test_env = TestEnvironment::default();
259 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
260 let work_dir = test_env.work_dir("repo");
261
262 work_dir.run_jj(["new"]).success();
263 work_dir.run_jj(["new"]).success();
264 work_dir.run_jj(["new"]).success();
265
266 let output = work_dir.run_jj([
267 "debug",
268 "stacked-table",
269 ".jj/repo/store/extra",
270 "--key-size=20", // HASH_LENGTH
271 ]);
272 assert_snapshot!(filter_index_stats(output), @"
273 Number of entries: 4
274 Stats per level:
275 Level 0:
276 Number of entries: 3
277 Name: [hash]
278 Level 1:
279 Number of entries: 1
280 Name: [hash]
281 [EOF]
282 ");
283 }
284
285 #[test]
286 fn test_debug_tree() {
287 let test_env = TestEnvironment::default();
288 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
289 let work_dir = test_env.work_dir("repo");
290 let sub_dir = work_dir.create_dir_all("dir/subdir");
291 sub_dir.write_file("file1", "contents 1");
292 work_dir.run_jj(["new"]).success();
293 sub_dir.write_file("file2", "contents 2");
294
295 // Defaults to showing the tree at the current commit
296 let output = work_dir.run_jj(["debug", "tree"]);
297 assert_snapshot!(output.normalize_backslash(), @r#"
298 dir/subdir/file1: Ok(Resolved(Some(File { id: FileId("498e9b01d79cb8d31cdf0df1a663cc1fcefd9de3"), executable: false, copy_id: CopyId("") })))
299 dir/subdir/file2: Ok(Resolved(Some(File { id: FileId("b2496eaffe394cd50a9db4de5787f45f09fd9722"), executable: false, copy_id: CopyId("") })))
300 [EOF]
301 "#
302 );
303
304 // Can show the tree at another commit
305 let output = work_dir.run_jj(["debug", "tree", "-r@-"]);
306 assert_snapshot!(output.normalize_backslash(), @r#"
307 dir/subdir/file1: Ok(Resolved(Some(File { id: FileId("498e9b01d79cb8d31cdf0df1a663cc1fcefd9de3"), executable: false, copy_id: CopyId("") })))
308 [EOF]
309 "#
310 );
311
312 // Can filter by paths
313 let output = work_dir.run_jj(["debug", "tree", "dir/subdir/file2"]);
314 assert_snapshot!(output.normalize_backslash(), @r#"
315 dir/subdir/file2: Ok(Resolved(Some(File { id: FileId("b2496eaffe394cd50a9db4de5787f45f09fd9722"), executable: false, copy_id: CopyId("") })))
316 [EOF]
317 "#
318 );
319
320 // Can a show the root tree by id
321 let output = work_dir.run_jj([
322 "debug",
323 "tree",
324 "--id=0958358e3f80e794f032b25ed2be96cf5825da6c",
325 ]);
326 assert_snapshot!(output.normalize_backslash(), @r#"
327 dir/subdir/file1: Ok(Resolved(Some(File { id: FileId("498e9b01d79cb8d31cdf0df1a663cc1fcefd9de3"), executable: false, copy_id: CopyId("") })))
328 dir/subdir/file2: Ok(Resolved(Some(File { id: FileId("b2496eaffe394cd50a9db4de5787f45f09fd9722"), executable: false, copy_id: CopyId("") })))
329 [EOF]
330 "#
331 );
332
333 // Can a show non-root tree by id
334 let output = work_dir.run_jj([
335 "debug",
336 "tree",
337 "--dir=dir",
338 "--id=6ac232efa713535ae518a1a898b77e76c0478184",
339 ]);
340 assert_snapshot!(output.normalize_backslash(), @r#"
341 dir/subdir/file1: Ok(Resolved(Some(File { id: FileId("498e9b01d79cb8d31cdf0df1a663cc1fcefd9de3"), executable: false, copy_id: CopyId("") })))
342 dir/subdir/file2: Ok(Resolved(Some(File { id: FileId("b2496eaffe394cd50a9db4de5787f45f09fd9722"), executable: false, copy_id: CopyId("") })))
343 [EOF]
344 "#
345 );
346
347 // Can filter by paths when showing non-root tree (matcher applies from root)
348 let output = work_dir.run_jj([
349 "debug",
350 "tree",
351 "--dir=dir",
352 "--id=6ac232efa713535ae518a1a898b77e76c0478184",
353 "dir/subdir/file2",
354 ]);
355 assert_snapshot!(output.normalize_backslash(), @r#"
356 dir/subdir/file2: Ok(Resolved(Some(File { id: FileId("b2496eaffe394cd50a9db4de5787f45f09fd9722"), executable: false, copy_id: CopyId("") })))
357 [EOF]
358 "#
359 );
360 }
361
362 fn filter_index_stats(output: CommandOutput) -> CommandOutput {
363 let regex = Regex::new(r" Name: [0-9a-z]+").unwrap();
364 output.normalize_stdout_with(|text| regex.replace_all(&text, " Name: [hash]").into_owned())
365 }