summaryrefslogtreecommitdiffstats
path: root/src/config/utils/get_diff_ignore_whitespace.rs
blob: 4f5796f8d35d168e495d35aec937e687b24a6d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{
	config::{utils::get_string, ConfigError, ConfigErrorCause, DiffIgnoreWhitespaceSetting},
	git::Config,
};

pub(crate) fn get_diff_ignore_whitespace(
	git_config: Option<&Config>,
	name: &str,
) -> Result<DiffIgnoreWhitespaceSetting, ConfigError> {
	match get_string(git_config, name, "none")?.to_lowercase().as_str() {
		"true" | "on" | "all" => Ok(DiffIgnoreWhitespaceSetting::All),
		"change" => Ok(DiffIgnoreWhitespaceSetting::Change),
		"false" | "off" | "none" => Ok(DiffIgnoreWhitespaceSetting::None),
		input => {
			Err(ConfigError::new(
				name,
				input,
				ConfigErrorCause::InvalidDiffIgnoreWhitespace,
			))
		},
	}
}

#[cfg(test)]
mod tests {
	use claims::{assert_err_eq, assert_ok_eq};
	use rstest::rstest;

	use super::*;
	use crate::test_helpers::{invalid_utf, with_git_config};

	#[rstest]
	#[case::true_str("true", DiffIgnoreWhitespaceSetting::All)]
	#[case::on("on", DiffIgnoreWhitespaceSetting::All)]
	#[case::all("all", DiffIgnoreWhitespaceSetting::All)]
	#[case::change("change", DiffIgnoreWhitespaceSetting::Change)]
	#[case::false_str("false", DiffIgnoreWhitespaceSetting::None)]
	#[case::off("off", DiffIgnoreWhitespaceSetting::None)]
	#[case::none("none", DiffIgnoreWhitespaceSetting::None)]
	#[case::mixed_case("ChAnGe", DiffIgnoreWhitespaceSetting::Change)]
	fn read_ok(#[case] value: &str, #[case] expected: DiffIgnoreWhitespaceSetting) {
		with_git_config(&["[test]", format!("value = \"{value}\"").as_str()], |git_config| {
			assert_ok_eq!(get_diff_ignore_whitespace(Some(&git_config), "test.value"), expected);
		});
	}

	#[test]
	fn read_default() {
		with_git_config(&[], |git_config| {
			assert_ok_eq!(
				get_diff_ignore_whitespace(Some(&git_config), "test.value"),
				DiffIgnoreWhitespaceSetting::None
			);
		});
	}

	#[test]
	fn read_invalid_value() {
		with_git_config(&["[test]", "value = invalid"], |git_config| {
			assert_err_eq!(
				get_diff_ignore_whitespace(Some(&git_config), "test.value"),
				ConfigError::new("test.value", "invalid", ConfigErrorCause::InvalidDiffIgnoreWhitespace)
			);
		});
	}

	#[test]
	fn read_invalid_non_utf() {
		with_git_config(
			&["[test]", format!("value = {}", invalid_utf()).as_str()],
			|git_config| {
				assert_err_eq!(
					get_diff_ignore_whitespace(Some(&git_config), "test.value"),
					ConfigError::new_read_error("test.value", ConfigErrorCause::InvalidUtf)
				);
			},
		);
	}
}