use std::ffi::OsStr;
use camino::Utf8Path;
use rstest::rstest;
use super::{GitLsFiles, REDIRECTING, without_line_terminator};
#[rstest]
#[case("repo\n", "repo")]
#[case("repo\r\n", "repo")]
#[case("repo\n\n", "repo\n")]
#[case("repo ", "repo ")]
#[case("repo\r", "repo\r")]
#[case("", "")]
#[case("\n", "")]
fn one_line_terminator_is_stripped_and_the_rest_is_the_path(
#[case] reported: &str,
#[case] expected: &str,
) {
assert_eq!(without_line_terminator(reported), expected, "{reported:?}");
}
#[rstest]
#[case("GIT_DIR")]
#[case("GIT_WORK_TREE")]
#[case("GIT_INDEX_FILE")]
#[case("GIT_COMMON_DIR")]
fn a_redirecting_variable_is_removed_from_the_invocations_environment(#[case] variable: &str) {
let command = GitLsFiles::new(false).command(&["ls-files"], Utf8Path::new("."));
let removed: Vec<&OsStr> = command
.get_envs()
.filter(|(_, value)| value.is_none())
.map(|(key, _)| key)
.collect();
assert!(
removed.contains(&OsStr::new(variable)),
"every invocation must be told to ignore {variable}: {removed:?}"
);
}
#[test]
fn the_environment_is_pruned_rather_than_cleared() {
let command = GitLsFiles::new(false).command(&["ls-files"], Utf8Path::new("."));
let mut removed: Vec<&OsStr> = command
.get_envs()
.filter(|(_, value)| value.is_none())
.map(|(key, _)| key)
.collect();
removed.sort_unstable();
assert_eq!(
removed,
[
OsStr::new("GIT_COMMON_DIR"),
OsStr::new("GIT_DIR"),
OsStr::new("GIT_INDEX_FILE"),
OsStr::new("GIT_WORK_TREE"),
],
"an invocation must remove exactly the Git redirections"
);
}
#[test]
fn the_declared_redirections_are_the_ones_the_cases_pin() {
let mut declared = REDIRECTING.to_vec();
declared.sort_unstable();
assert_eq!(
declared,
[
"GIT_COMMON_DIR",
"GIT_DIR",
"GIT_INDEX_FILE",
"GIT_WORK_TREE"
],
"a variable added to the adapter needs a case naming it"
);
}