Debian Codegraph @

cli/tests/test_templater.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 crate::common::CommandOutput;
16 use crate::common::TestEnvironment;
17 use crate::common::TestWorkDir;
18
19 #[test]
20 fn test_templater_parse_error() {
21 let test_env = TestEnvironment::default();
22 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
23 let work_dir = test_env.work_dir("repo");
24 let render = |template| get_template_output(&work_dir, "@-", template);
25
26 // Typo
27 test_env.add_config(
28 r###"
29 [template-aliases]
30 'conflicting' = ''
31 'shorted()' = ''
32 'socat(x)' = 'x'
33 'format_id(id)' = 'id.sort()'
34 "###,
35 );
36 insta::assert_snapshot!(render(r#"conflicts"#), @"
37 ------- stderr -------
38 Error: Failed to parse template: Keyword `conflicts` doesn't exist
39 Caused by: --> 1:1
40 |
41 1 | conflicts
42 | ^-------^
43 |
44 = Keyword `conflicts` doesn't exist
45 Hint: Did you mean `conflict`, `conflicted_files`, `conflicting`?
46 [EOF]
47 [exit status: 1]
48 ");
49 insta::assert_snapshot!(render(r#"commit_id.shorter()"#), @"
50 ------- stderr -------
51 Error: Failed to parse template: Method `shorter` doesn't exist for type `CommitId`
52 Caused by: --> 1:11
53 |
54 1 | commit_id.shorter()
55 | ^-----^
56 |
57 = Method `shorter` doesn't exist for type `CommitId`
58 Hint: Did you mean `short`, `shortest`?
59 [EOF]
60 [exit status: 1]
61 ");
62 insta::assert_snapshot!(render(r#"oncat()"#), @"
63 ------- stderr -------
64 Error: Failed to parse template: Function `oncat` doesn't exist
65 Caused by: --> 1:1
66 |
67 1 | oncat()
68 | ^---^
69 |
70 = Function `oncat` doesn't exist
71 Hint: Did you mean `concat`, `socat`?
72 [EOF]
73 [exit status: 1]
74 ");
75 insta::assert_snapshot!(render(r#""".lines().map(|s| se)"#), @r#"
76 ------- stderr -------
77 Error: Failed to parse template: Keyword `se` doesn't exist
78 Caused by: --> 1:20
79 |
80 1 | "".lines().map(|s| se)
81 | ^^
82 |
83 = Keyword `se` doesn't exist
84 Hint: Did you mean `s`, `self`?
85 [EOF]
86 [exit status: 1]
87 "#);
88 insta::assert_snapshot!(render(r#"format_id(commit_id)"#), @"
89 ------- stderr -------
90 Error: Failed to parse template: In alias `format_id(id)`
91 Caused by:
92 1: --> 1:1
93 |
94 1 | format_id(commit_id)
95 | ^------------------^
96 |
97 = In alias `format_id(id)`
98 2: --> 1:4
99 |
100 1 | id.sort()
101 | ^--^
102 |
103 = Method `sort` doesn't exist for type `CommitId`
104 Hint: Did you mean `short`, `shortest`?
105 [EOF]
106 [exit status: 1]
107 ");
108
109 // Unexpanded identifier in string pattern
110 insta::assert_snapshot!(render(r#"''.match(exact:foo)"#), @"
111 ------- stderr -------
112 Error: Failed to parse template: Expected string literal
113 Caused by: --> 1:16
114 |
115 1 | ''.match(exact:foo)
116 | ^-^
117 |
118 = Expected string literal
119 [EOF]
120 [exit status: 1]
121 ");
122
123 // "at least N arguments"
124 insta::assert_snapshot!(render("separate()"), @"
125 ------- stderr -------
126 Error: Failed to parse template: Function `separate`: Expected at least 1 arguments
127 Caused by: --> 1:10
128 |
129 1 | separate()
130 | ^
131 |
132 = Function `separate`: Expected at least 1 arguments
133 [EOF]
134 [exit status: 1]
135 ");
136
137 // -Tbuiltin shows the predefined builtin_* aliases. This isn't 100%
138 // guaranteed, but is nice.
139 insta::assert_snapshot!(render(r#"builtin"#), @"
140 ------- stderr -------
141 Error: Failed to parse template: Keyword `builtin` doesn't exist
142 Caused by: --> 1:1
143 |
144 1 | builtin
145 | ^-----^
146 |
147 = Keyword `builtin` doesn't exist
148 Hint: Did you mean `builtin_config_list`, `builtin_config_list_detailed`, `builtin_draft_commit_description`, `builtin_draft_commit_description_with_diff`, `builtin_evolog_compact`, `builtin_log_comfortable`, `builtin_log_compact`, `builtin_log_compact_full_description`, `builtin_log_detailed`, `builtin_log_node`, `builtin_log_node_ascii`, `builtin_log_oneline`, `builtin_log_redacted`, `builtin_op_log_comfortable`, `builtin_op_log_compact`, `builtin_op_log_node`, `builtin_op_log_node_ascii`, `builtin_op_log_oneline`, `builtin_op_log_redacted`, `builtin_workspace_list`?
149 [EOF]
150 [exit status: 1]
151 ");
152 }
153
154 #[test]
155 fn test_templater_parse_warning() {
156 let test_env = TestEnvironment::default();
157 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
158 let work_dir = test_env.work_dir("repo");
159
160 let output = work_dir.run_jj(["op", "log", "-T", "tags"]);
161 insta::assert_snapshot!(output, @"
162 @
163 ○
164 [EOF]
165 ------- stderr -------
166 Warning: In template expression
167 --> 1:1
168 |
169 1 | tags
170 | ^--^
171 |
172 = operation.tags() is deprecated; use .attributes() instead
173 [EOF]
174 ");
175 }
176
177 #[test]
178 fn test_templater_upper_lower() {
179 let test_env = TestEnvironment::default();
180 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
181 let work_dir = test_env.work_dir("repo");
182 let render = |template| get_colored_template_output(&work_dir, "@-", template);
183
184 insta::assert_snapshot!(
185 render(r#"change_id.shortest(4).upper() ++ change_id.shortest(4).upper().lower()"#),
186 @"ZZZZzzzz[EOF]");
187 insta::assert_snapshot!(
188 render(r#""Hello".upper() ++ "Hello".lower()"#), @"HELLOhello[EOF]");
189 }
190
191 #[test]
192 fn test_templater_alias() {
193 let test_env = TestEnvironment::default();
194 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
195 let work_dir = test_env.work_dir("repo");
196 let render = |template| get_template_output(&work_dir, "@-", template);
197
198 test_env.add_config(
199 r###"
200 [template-aliases]
201 'my_commit_id' = 'commit_id.short()'
202 'syntax_error' = 'foo.'
203 'name_error' = 'unknown_id'
204 'recurse' = 'recurse1'
205 'recurse1' = 'recurse2()'
206 'recurse2()' = 'recurse'
207 'identity(x)' = 'x'
208 'coalesce2(x, y)' = 'if(x, x, y)'
209 'deprecated()' = 'tags'
210 'format_commit_summary_with_refs(x, y)' = 'x.commit_id()'
211 'builtin_log_node' = '"#"'
212 'builtin_op_log_node' = '"#"'
213 'builtin_log_node_ascii' = '"#"'
214 'builtin_op_log_node_ascii' = '"#"'
215 "###,
216 );
217
218 insta::assert_snapshot!(render("my_commit_id"), @"000000000000[EOF]");
219 insta::assert_snapshot!(render("identity(my_commit_id)"), @"000000000000[EOF]");
220
221 insta::assert_snapshot!(render("commit_id ++ syntax_error"), @"
222 ------- stderr -------
223 Error: Failed to parse template: In alias `syntax_error`
224 Caused by:
225 1: --> 1:14
226 |
227 1 | commit_id ++ syntax_error
228 | ^----------^
229 |
230 = In alias `syntax_error`
231 2: --> 1:5
232 |
233 1 | foo.
234 | ^---
235 |
236 = expected <identifier>
237 [EOF]
238 [exit status: 1]
239 ");
240
241 insta::assert_snapshot!(render("commit_id ++ name_error"), @"
242 ------- stderr -------
243 Error: Failed to parse template: In alias `name_error`
244 Caused by:
245 1: --> 1:14
246 |
247 1 | commit_id ++ name_error
248 | ^--------^
249 |
250 = In alias `name_error`
251 2: --> 1:1
252 |
253 1 | unknown_id
254 | ^--------^
255 |
256 = Keyword `unknown_id` doesn't exist
257 [EOF]
258 [exit status: 1]
259 ");
260
261 insta::assert_snapshot!(render(r#"identity(identity(commit_id.short("")))"#), @r#"
262 ------- stderr -------
263 Error: Failed to parse template: In alias `identity(x)`
264 Caused by:
265 1: --> 1:1
266 |
267 1 | identity(identity(commit_id.short("")))
268 | ^-------------------------------------^
269 |
270 = In alias `identity(x)`
271 2: --> 1:1
272 |
273 1 | x
274 | ^
275 |
276 = In function parameter `x`
277 3: --> 1:10
278 |
279 1 | identity(identity(commit_id.short("")))
280 | ^---------------------------^
281 |
282 = In alias `identity(x)`
283 4: --> 1:1
284 |
285 1 | x
286 | ^
287 |
288 = In function parameter `x`
289 5: --> 1:35
290 |
291 1 | identity(identity(commit_id.short("")))
292 | ^^
293 |
294 = Expected expression of type `Integer`, but actual type is `String`
295 [EOF]
296 [exit status: 1]
297 "#);
298
299 insta::assert_snapshot!(render("commit_id ++ recurse"), @"
300 ------- stderr -------
301 Error: Failed to parse template: In alias `recurse`
302 Caused by:
303 1: --> 1:14
304 |
305 1 | commit_id ++ recurse
306 | ^-----^
307 |
308 = In alias `recurse`
309 2: --> 1:1
310 |
311 1 | recurse1
312 | ^------^
313 |
314 = In alias `recurse1`
315 3: --> 1:1
316 |
317 1 | recurse2()
318 | ^--------^
319 |
320 = In alias `recurse2()`
321 4: --> 1:1
322 |
323 1 | recurse
324 | ^-----^
325 |
326 = Alias `recurse` expanded recursively
327 [EOF]
328 [exit status: 1]
329 ");
330
331 insta::assert_snapshot!(render("identity()"), @"
332 ------- stderr -------
333 Error: Failed to parse template: Function `identity`: Expected 1 arguments
334 Caused by: --> 1:10
335 |
336 1 | identity()
337 | ^
338 |
339 = Function `identity`: Expected 1 arguments
340 [EOF]
341 [exit status: 1]
342 ");
343 insta::assert_snapshot!(render("identity(commit_id, commit_id)"), @"
344 ------- stderr -------
345 Error: Failed to parse template: Function `identity`: Expected 1 arguments
346 Caused by: --> 1:10
347 |
348 1 | identity(commit_id, commit_id)
349 | ^------------------^
350 |
351 = Function `identity`: Expected 1 arguments
352 [EOF]
353 [exit status: 1]
354 ");
355
356 insta::assert_snapshot!(render(r#"coalesce2(label("x", "not boolean"), "")"#), @r#"
357 ------- stderr -------
358 Error: Failed to parse template: In alias `coalesce2(x, y)`
359 Caused by:
360 1: --> 1:1
361 |
362 1 | coalesce2(label("x", "not boolean"), "")
363 | ^--------------------------------------^
364 |
365 = In alias `coalesce2(x, y)`
366 2: --> 1:4
367 |
368 1 | if(x, x, y)
369 | ^
370 |
371 = In function parameter `x`
372 3: --> 1:11
373 |
374 1 | coalesce2(label("x", "not boolean"), "")
375 | ^-----------------------^
376 |
377 = Expected expression of type `Boolean`, but actual type is `Template`
378 [EOF]
379 [exit status: 1]
380 "#);
381
382 insta::assert_snapshot!(render("(-my_commit_id)"), @"
383 ------- stderr -------
384 Error: Failed to parse template: In alias `my_commit_id`
385 Caused by:
386 1: --> 1:3
387 |
388 1 | (-my_commit_id)
389 | ^----------^
390 |
391 = In alias `my_commit_id`
392 2: --> 1:1
393 |
394 1 | commit_id.short()
395 | ^---------------^
396 |
397 = Expected expression of type `Integer`, but actual type is `String`
398 [EOF]
399 [exit status: 1]
400 ");
401
402 let output = work_dir.run_jj(["op", "log", "-n1", "-Tdeprecated()"]);
403 insta::assert_snapshot!(output, @"
404 #
405 [EOF]
406 ------- stderr -------
407 Warning: In template expression
408 --> 1:1
409 |
410 1 | deprecated()
411 | ^----------^
412 |
413 = In alias `deprecated()`
414 --> 1:1
415 |
416 1 | tags
417 | ^--^
418 |
419 = operation.tags() is deprecated; use .attributes() instead
420 [EOF]
421 ");
422 }
423
424 #[test]
425 fn test_templater_alias_override() {
426 let test_env = TestEnvironment::default();
427 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
428 let work_dir = test_env.work_dir("repo");
429
430 test_env.add_config(
431 r#"
432 [template-aliases]
433 'f(x)' = '"user"'
434 "#,
435 );
436
437 // 'f(x)' should be overridden by --config 'f(a)'. If aliases were sorted
438 // purely by name, 'f(a)' would come first.
439 let output = work_dir.run_jj([
440 "log",
441 "--no-graph",
442 "-r@",
443 "-T",
444 r#"f(_)"#,
445 r#"--config=template-aliases.'f(a)'='"arg"'"#,
446 ]);
447 insta::assert_snapshot!(output, @"arg[EOF]");
448 }
449
450 #[test]
451 fn test_templater_bad_alias_decl() {
452 let test_env = TestEnvironment::default();
453 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
454 let work_dir = test_env.work_dir("repo");
455
456 test_env.add_config(
457 r###"
458 [template-aliases]
459 'badfn(a, a)' = 'a'
460 'my_commit_id' = 'commit_id.short()'
461 "###,
462 );
463
464 // Invalid declaration should be warned and ignored.
465 let output = work_dir.run_jj(["log", "--no-graph", "-r@-", "-Tmy_commit_id"]);
466 insta::assert_snapshot!(output, @"
467 000000000000[EOF]
468 ------- stderr -------
469 Warning: Failed to load `template-aliases.badfn(a, a)`: --> 1:7
470 |
471 1 | badfn(a, a)
472 | ^--^
473 |
474 = Redefinition of function parameter
475 [EOF]
476 ");
477 }
478
479 #[test]
480 fn test_templater_config_function() {
481 let test_env = TestEnvironment::default();
482 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
483 let work_dir = test_env.work_dir("repo");
484 let render = |template| get_template_output(&work_dir, "@-", template);
485
486 insta::assert_snapshot!(
487 render("config('user.name')"),
488 @r#""Test User"[EOF]"#);
489 insta::assert_snapshot!(
490 render("config('user')"),
491 @r#"{ email = "[email protected]", name = "Test User" }[EOF]"#);
492 insta::assert_snapshot!(render("config('invalid name')"), @"
493 ------- stderr -------
494 Error: Failed to parse template: Failed to parse config name
495 Caused by:
496 1: --> 1:8
497 |
498 1 | config('invalid name')
499 | ^------------^
500 |
501 = Failed to parse config name
502 2: TOML parse error at line 1, column 9
503 |
504 1 | invalid name
505 | ^
506 unexpected content, expected nothing
507
508 [EOF]
509 [exit status: 1]
510 ");
511 insta::assert_snapshot!(render("config('unknown')"), @"");
512 }
513
514 #[must_use]
515 fn get_template_output(work_dir: &TestWorkDir, rev: &str, template: &str) -> CommandOutput {
516 work_dir.run_jj(["log", "--no-graph", "-r", rev, "-T", template])
517 }
518
519 #[must_use]
520 fn get_colored_template_output(work_dir: &TestWorkDir, rev: &str, template: &str) -> CommandOutput {
521 work_dir.run_jj([
522 "log",
523 "--color=always",
524 "--no-graph",
525 "-r",
526 rev,
527 "-T",
528 template,
529 ])
530 }