summaryrefslogtreecommitdiffstats
path: root/src/config/utils/get_diff_rename.rs
blob: 046a65f66429cfa7890b86fbf7fa55a4928387a1 (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
use crate::{
	config::{get_string, ConfigError, ConfigErrorCause},
	git::Config,
};

pub(crate) fn git_diff_renames(git_config: Option<&Config>, name: &str) -> Result<(bool, bool), ConfigError> {
	match get_string(git_config, name, "true")?.to_lowercase().as_str() {
		"true" => Ok((true, false)),
		"false" => Ok((false, false)),
		"copy" | "copies" => Ok((true, true)),
		input => Err(ConfigError::new(name, input, ConfigErrorCause::InvalidDiffRenames)),
	}
}

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

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

	#[rstest]
	#[case::true_str("true", (true, false))]
	#[case::false_str("false", (false, false))]
	#[case::off("copy", (true, true))]
	#[case::none("copies", (true, true))]
	#[case::mixed_case("CoPiEs", (true, true))]
	fn read_ok(#[case] value: &str, #[case] expected: (bool, bool)) {
		with_git_config(&["[test]", format!("value = \"{value}\"").as_str()], |git_config| {
			assert_ok_eq!(git_diff_renames(Some(&git_config), "test.value"), expected);
		});
	}

	#[test]
	fn read_default() {
		with_git_config(&[], |git_config| {
			assert_ok_eq!(git_diff_renames(Some(&git_config), "test.value"), (true, false));
		});
	}

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

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