Debian Codegraph @

src/features/mod.rs

raw symbols SCIP
1 use std::collections::HashMap;
2
3 use crate::cli;
4 use crate::git_config::GitConfig;
5 use crate::options::option_value::ProvenancedOptionValue;
6 use ProvenancedOptionValue::*;
7
8 /// A custom feature is a named set of command line (option, value) pairs, supplied in a git config
9 /// file. I.e. it might look like
10 ///
11 /// [delta "decorations"]
12 /// commit-decoration-style = bold box ul
13 /// file-style = bold 19 ul
14 /// file-decoration-style = none
15 ///
16 /// A builtin feature is a named set of command line (option, value) pairs that is built-in to
17 /// delta. The valueof a builtin feature is a function. This function is passed the current set of
18 /// all command-line option-value pairs, and GitConfig, and returns either a GitConfigValue, or a
19 /// DefaultValue. (It may use the set of all option-value pairs when computing its default).
20 pub type BuiltinFeature = HashMap<String, OptionValueFunction>;
21
22 type OptionValueFunction = Box<dyn Fn(&cli::Opt, &Option<GitConfig>) -> ProvenancedOptionValue>;
23
24 // Construct a 2-level hash map: (feature name) -> (option name) -> (value function). A value
25 // function is a function that takes an Opt struct, and a git Config struct, and returns the value
26 // for the option.
27 pub fn make_builtin_features() -> HashMap<String, BuiltinFeature> {
28 vec![
29 (
30 "color-only".to_string(),
31 color_only::make_feature().into_iter().collect(),
32 ),
33 (
34 "diff-highlight".to_string(),
35 diff_highlight::make_feature().into_iter().collect(),
36 ),
37 (
38 "diff-so-fancy".to_string(),
39 diff_so_fancy::make_feature().into_iter().collect(),
40 ),
41 (
42 "hyperlinks".to_string(),
43 hyperlinks::make_feature().into_iter().collect(),
44 ),
45 (
46 "line-numbers".to_string(),
47 line_numbers::make_feature().into_iter().collect(),
48 ),
49 (
50 "navigate".to_string(),
51 navigate::make_feature().into_iter().collect(),
52 ),
53 ("raw".to_string(), raw::make_feature().into_iter().collect()),
54 (
55 "side-by-side".to_string(),
56 side_by_side::make_feature().into_iter().collect(),
57 ),
58 ]
59 .into_iter()
60 .collect()
61 }
62
63 /// The macro permits the values of a builtin feature to be specified as either (a) a git config
64 /// entry or (b) a value, which may be computed from the other command line options (cli::Opt).
65 macro_rules! builtin_feature {
66 ([$( ($option_name:expr, $type:ty, $git_config_key:expr, $opt:ident => $value:expr) ),*]) => {
67 vec![$(
68 (
69 $option_name.to_string(),
70 Box::new(move |$opt: &$crate::cli::Opt, git_config: &Option<$crate::git_config::GitConfig>| {
71 match (git_config, $git_config_key) {
72 (Some(git_config), Some(git_config_key)) => git_config.get::<$type>(git_config_key).map(|value| $crate::features::GitConfigValue(value.into())),
73 _ => None,
74 }
75 .unwrap_or_else(|| $crate::features::DefaultValue($value.into()))
76 }) as OptionValueFunction
77 )
78 ),*]
79 }
80 }
81
82 pub mod color_only;
83 pub mod diff_highlight;
84 pub mod diff_so_fancy;
85 pub mod hyperlinks;
86 pub mod line_numbers;
87 pub mod navigate;
88 pub mod raw;
89 pub mod side_by_side;
90
91 #[cfg(test)]
92 pub mod tests {
93 use std::collections::HashSet;
94 use std::fs::remove_file;
95
96 use crate::cli;
97 use crate::env::DeltaEnv;
98 use crate::features::make_builtin_features;
99 use crate::tests::integration_test_utils::make_options_from_args_and_git_config;
100
101 #[test]
102 fn test_builtin_features_have_flags_and_these_set_features() {
103 let builtin_features = make_builtin_features();
104 let mut args = vec!["delta".to_string()];
105 args.extend(builtin_features.keys().map(|s| format!("--{s}")));
106 let opt = cli::Opt::from_iter_and_git_config(&DeltaEnv::default(), args, None);
107 let features: HashSet<&str> = opt
108 .features
109 .as_deref()
110 .unwrap_or("")
111 .split_whitespace()
112 .collect();
113 for feature in builtin_features.keys() {
114 assert!(features.contains(feature.as_str()))
115 }
116 }
117
118 #[test]
119 fn test_builtin_feature_from_gitconfig() {
120 let git_config_contents = b"
121 [delta]
122 navigate = true
123 ";
124 let git_config_path = "delta__test_builtin_feature_from_gitconfig.gitconfig";
125
126 assert_eq!(
127 make_options_from_args_and_git_config(
128 &[],
129 Some(git_config_contents),
130 Some(git_config_path)
131 )
132 .features
133 .unwrap(),
134 "navigate"
135 );
136
137 remove_file(git_config_path).unwrap();
138 }
139
140 #[test]
141 fn test_features_on_command_line_replace_features_in_gitconfig() {
142 let git_config_contents = b"
143 [delta]
144 features = my-feature
145 ";
146 let git_config_path =
147 "delta__test_features_on_command_line_replace_features_in_gitconfig.gitconfig";
148
149 assert_eq!(
150 make_options_from_args_and_git_config(
151 &["--features", "navigate raw"],
152 Some(git_config_contents),
153 Some(git_config_path),
154 )
155 .features
156 .unwrap(),
157 "navigate raw"
158 );
159 assert_eq!(
160 make_options_from_args_and_git_config(
161 &["--navigate", "--features", "raw"],
162 Some(git_config_contents),
163 Some(git_config_path),
164 )
165 .features
166 .unwrap(),
167 "navigate raw"
168 );
169
170 remove_file(git_config_path).unwrap();
171 }
172
173 #[test]
174 fn test_feature_flag_on_command_line_does_not_replace_features_in_gitconfig() {
175 let git_config_contents = b"
176 [delta]
177 features = my-feature
178 ";
179 let git_config_path =
180 "delta__test_feature_flag_on_command_line_does_not_replace_features_in_gitconfig.gitconfig";
181 assert_eq!(
182 make_options_from_args_and_git_config(
183 &["--navigate", "--raw"],
184 Some(git_config_contents),
185 Some(git_config_path),
186 )
187 .features
188 .unwrap(),
189 "my-feature navigate raw"
190 );
191
192 remove_file(git_config_path).unwrap();
193 }
194
195 #[test]
196 fn test_recursive_feature_gathering_1() {
197 let git_config_contents = b"
198 [delta]
199 features = h g
200
201 [delta \"a\"]
202 features = c b
203 diff-highlight = true
204
205 [delta \"d\"]
206 features = f e
207 diff-so-fancy = true
208 ";
209 let git_config_path = "delta__test_feature_collection.gitconfig";
210
211 assert_eq!(
212 make_options_from_args_and_git_config(
213 &["--raw", "--features", "d a"],
214 Some(git_config_contents),
215 Some(git_config_path),
216 )
217 .features
218 .unwrap(),
219 "raw diff-so-fancy f e d diff-highlight c b a"
220 );
221
222 remove_file(git_config_path).unwrap();
223 }
224
225 #[test]
226 fn test_recursive_feature_gathering_2() {
227 let git_config_contents = b"
228 [delta]
229 features = feature-1
230
231 [delta \"feature-1\"]
232 features = feature-2 feature-3
233
234 [delta \"feature-2\"]
235 features = feature-4
236
237 [delta \"feature-4\"]
238 minus-style = blue
239 ";
240 let git_config_path = "delta__test_recursive_features.gitconfig";
241 let opt = make_options_from_args_and_git_config(
242 &["delta"],
243 Some(git_config_contents),
244 Some(git_config_path),
245 );
246 assert_eq!(
247 opt.features.unwrap(),
248 "feature-4 feature-2 feature-3 feature-1"
249 );
250
251 remove_file(git_config_path).unwrap();
252 }
253
254 #[test]
255 fn test_main_section() {
256 let git_config_contents = b"
257 [delta]
258 minus-style = blue
259 ";
260 let git_config_path = "delta__test_main_section.gitconfig";
261
262 // First check that it doesn't default to blue, because that's going to be used to signal
263 // that gitconfig has set the style.
264 assert_ne!(
265 make_options_from_args_and_git_config(&[], None, None).minus_style,
266 "blue"
267 );
268
269 // Check that --minus-style is honored as we expect.
270 assert_eq!(
271 make_options_from_args_and_git_config(&["--minus-style", "red"], None, None)
272 .minus_style,
273 "red"
274 );
275
276 // Check that gitconfig does not override a command line argument
277 assert_eq!(
278 make_options_from_args_and_git_config(
279 &["--minus-style", "red"],
280 Some(git_config_contents),
281 Some(git_config_path),
282 )
283 .minus_style,
284 "red"
285 );
286
287 // Finally, check that gitconfig is honored when not overridden by a command line argument.
288 assert_eq!(
289 make_options_from_args_and_git_config(
290 &[],
291 Some(git_config_contents),
292 Some(git_config_path)
293 )
294 .minus_style,
295 "blue"
296 );
297
298 remove_file(git_config_path).unwrap();
299 }
300
301 #[test]
302 fn test_feature() {
303 let git_config_contents = b"
304 [delta]
305
306
307 [delta \"my-feature\"]
308 minus-style = green
309 ";
310 let git_config_path = "delta__test_feature.gitconfig";
311
312 assert_eq!(
313 make_options_from_args_and_git_config(
314 &["--features", "my-feature"],
315 Some(git_config_contents),
316 Some(git_config_path),
317 )
318 .minus_style,
319 "green"
320 );
321 remove_file(git_config_path).unwrap();
322 }
323
324 #[test]
325 fn test_main_section_overrides_feature() {
326 let git_config_contents = b"
327 [delta]
328 minus-style = blue
329
330 [delta \"my-feature-1\"]
331 minus-style = green
332 ";
333 let git_config_path = "delta__test_main_section_overrides_feature.gitconfig";
334
335 // Without --features the main section takes effect
336 assert_eq!(
337 make_options_from_args_and_git_config(
338 &[],
339 Some(git_config_contents),
340 Some(git_config_path)
341 )
342 .minus_style,
343 "blue"
344 );
345
346 // Event with --features the main section overrides the feature.
347 assert_eq!(
348 make_options_from_args_and_git_config(
349 &["--features", "my-feature-1"],
350 Some(git_config_contents),
351 Some(git_config_path),
352 )
353 .minus_style,
354 "blue"
355 );
356 remove_file(git_config_path).unwrap();
357 }
358
359 #[test]
360 fn test_multiple_features() {
361 let git_config_contents = b"
362 [delta]
363
364
365 [delta \"my-feature-1\"]
366 minus-style = green
367
368 [delta \"my-feature-2\"]
369 minus-style = yellow
370 ";
371 let git_config_path = "delta__test_multiple_features.gitconfig";
372
373 assert_eq!(
374 make_options_from_args_and_git_config(
375 &["--features", "my-feature-1 my-feature-2"],
376 Some(git_config_contents),
377 Some(git_config_path),
378 )
379 .minus_style,
380 "yellow"
381 );
382
383 assert_eq!(
384 make_options_from_args_and_git_config(
385 &["--features", "my-feature-2 my-feature-1"],
386 Some(git_config_contents),
387 Some(git_config_path),
388 )
389 .minus_style,
390 "green"
391 );
392
393 remove_file(git_config_path).unwrap();
394 }
395
396 #[test]
397 fn test_invalid_features() {
398 let git_config_contents = b"
399 [delta \"my-feature-1\"]
400 minus-style = green
401
402 [delta \"my-feature-2\"]
403 minus-style = yellow
404 ";
405 let git_config_path = "delta__test_invalid_features.gitconfig";
406
407 let default = make_options_from_args_and_git_config(&[], None, None).minus_style;
408 assert_ne!(default, "green");
409 assert_ne!(default, "yellow");
410
411 assert_eq!(
412 make_options_from_args_and_git_config(
413 &["--features", "my-feature-1"],
414 Some(git_config_contents),
415 Some(git_config_path),
416 )
417 .minus_style,
418 "green"
419 );
420
421 assert_eq!(
422 make_options_from_args_and_git_config(
423 &["--features", "my-feature-x"],
424 Some(git_config_contents),
425 Some(git_config_path),
426 )
427 .minus_style,
428 default
429 );
430
431 assert_eq!(
432 make_options_from_args_and_git_config(
433 &["--features", "my-feature-1 my-feature-x"],
434 Some(git_config_contents),
435 Some(git_config_path),
436 )
437 .minus_style,
438 "green"
439 );
440
441 assert_eq!(
442 make_options_from_args_and_git_config(
443 &["--features", "my-feature-x my-feature-2 my-feature-x"],
444 Some(git_config_contents),
445 Some(git_config_path),
446 )
447 .minus_style,
448 "yellow"
449 );
450
451 remove_file(git_config_path).unwrap();
452 }
453
454 #[test]
455 fn test_whitespace_error_style() {
456 let git_config_contents = b"
457 [color \"diff\"]
458 whitespace = yellow dim ul magenta
459 ";
460 let git_config_path = "delta__test_whitespace_error_style.gitconfig";
461
462 // Git config disabled: hard-coded delta default wins
463 assert_eq!(
464 make_options_from_args_and_git_config(&[], None, None).whitespace_error_style,
465 "magenta reverse"
466 );
467
468 // Unspecified by user: color.diff.whitespace wins
469 assert_eq!(
470 make_options_from_args_and_git_config(
471 &[],
472 Some(git_config_contents),
473 Some(git_config_path)
474 )
475 .whitespace_error_style,
476 "yellow dim ul magenta"
477 );
478
479 // Command line argument wins
480 assert_eq!(
481 make_options_from_args_and_git_config(
482 &["--whitespace-error-style", "red reverse"],
483 Some(git_config_contents),
484 Some(git_config_path)
485 )
486 .whitespace_error_style,
487 "red reverse"
488 );
489
490 let git_config_contents = b"
491 [color \"diff\"]
492 whitespace = yellow dim ul magenta
493
494 [delta]
495 whitespace-error-style = blue reverse
496
497 [delta \"my-whitespace-error-style-feature\"]
498 whitespace-error-style = green reverse
499 ";
500
501 // Command line argument wins
502 assert_eq!(
503 make_options_from_args_and_git_config(
504 &["--whitespace-error-style", "red reverse"],
505 Some(git_config_contents),
506 Some(git_config_path)
507 )
508 .whitespace_error_style,
509 "red reverse"
510 );
511
512 // No command line argument or features; main [delta] section wins
513 assert_eq!(
514 make_options_from_args_and_git_config(
515 &[],
516 Some(git_config_contents),
517 Some(git_config_path)
518 )
519 .whitespace_error_style,
520 "blue reverse"
521 );
522
523 // Feature contains key, but main [delta] section still wins.
524 // This is equivalent to
525 //
526 // [delta]
527 // features = my-whitespace-error-style-feature
528 // whitespace-error-style = blue reverse
529 //
530 // In this situation, the value from the feature is overridden.
531 assert_eq!(
532 make_options_from_args_and_git_config(
533 &["--features", "my-whitespace-error-style-feature"],
534 Some(git_config_contents),
535 Some(git_config_path)
536 )
537 .whitespace_error_style,
538 "blue reverse"
539 );
540
541 remove_file(git_config_path).unwrap();
542 }
543 }