Skip to content

Commit d576b32

Browse files
committed
Test that env for fixture scripts has only command-scope config
It doesn't have to have any configuration variables set in the command scope, though in practice it always should (with versions of `git` with which `gix-testtools` is compatible) because we are setting them with `GIT_CONFIG_{COUNT,KEY,VALUE}`, which, like `-c`, sets configuration variables in the command scope. But it must not have any configuration variables set in other scopes. Of course, actual fixture scripts, when run, most often create Git repositories, in which case there will in practice almost always be local scope configuration for the script. The main point here is to make sure we are omitting variables from the global and system scopes, as we have already been doing (and testing for), and also omitting them from the other high scopes, such as the "unknown" scope associated with an Apple Git installation that is higher than the system scope and unaffected by `GIT_CONFIG_SYSTEM` but affected by `GIT_CONFIG_NOSYSTEM`. Like the tests that preceded it, this test creates a new empty temporary directory to set as the CWD of the test `git` command configured by `configure_command`. As such, there should be no local scope configuration, because this directory should be a subdirectory of a system `/tmp`-like directory. A `/tmp`-like directory can technically be a Git repository, and can even contribute configuration if the repository is owned by the current user or something is keeping `safe.directory` protections from excluding it. When the goal is to *get* configuration from scopes higher than the local scope, it is worth taking steps to prevent this (which for `gix-path` is achieved by the combination of GitoxideLabs#1523 and GitoxideLabs#1567). But in the test suite, temporary directories should preferably be in locations that are only `git` repositories (owned by the curent user) in the unusual situation that this is desired, and supporting tooling such as `git` should be at recent enough versions to really support the usage that the test suite makes of it. Furthermore, while it is possible to clear the local scope in this new test -- such as by using, as the command's CWD, a subdirectory of a directory that is, in the command's environment, prepended to `GIT_CEILING_DIRECTORIES` -- this would tend to hide problems in the actual `gix-testtools` code that this is testing. If any such measure needs to be taken, it would be better done in code that uses `configure_command` (or in `configure_command` itself it it is widely needed and generally acceptable), rather than in tests of `configure_command`. For these reasons, the test is, at least for now, deliberately written in such a way that it will fail if the directory we get from `tempfile::TempDir::new()` is somehow a Git repository. This commit consolidates the two preceding test cases into this one. So now there is a single test that `configure-command` both: - Excludes global and system scope config, as it already did. - Also excludes other high-scoped config, which it doesn't yet do. Thus, this new test is expected to fail on most macOS systems (where `git` is Apple Git and the installation-associated "unknown" scope configuration file is nonempty), until that is fixed.
1 parent 6f128dd commit d576b32

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

tests/tools/src/lib.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -898,43 +898,37 @@ mod tests {
898898
} else {
899899
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), dir.join(":")]
900900
};
901-
902901
// Create the files.
903902
for path in paths {
904903
std::fs::write(path, CONFIG_DATA).expect("can write contents");
905904
}
906-
907905
// Verify the files. This is mostly to show we really made a `\\?\...\NUL` on Windows.
908906
for path in paths {
909907
let buf = std::fs::read(path).expect("the file really exists");
910-
assert_eq!(buf, CONFIG_DATA, "File {path:?} should be created");
908+
assert_eq!(buf, CONFIG_DATA, "{path:?} should be a config file");
911909
}
912910
}
913911

914-
fn check_configure_clears_scope(scope_env_key: &str, scope_option: &str) {
912+
#[test]
913+
fn configure_command_clears_external_config() {
915914
let temp = tempfile::TempDir::new().expect("can create temp dir");
916-
let dir = temp.path();
917-
populate_ad_hoc_config_files(dir);
915+
populate_ad_hoc_config_files(temp.path());
918916

919917
let mut cmd = std::process::Command::new("git");
920-
cmd.env(scope_env_key, SCOPE_ENV_VALUE); // configure_command() should override it.
921-
let args = ["config", "-l", "--show-origin", scope_option].map(String::from);
922-
configure_command(&mut cmd, &args, dir);
918+
let args = ["config", "-l", "--show-origin"].map(String::from);
919+
cmd.env("GIT_CONFIG_SYSTEM", SCOPE_ENV_VALUE);
920+
cmd.env("GIT_CONFIG_GLOBAL", SCOPE_ENV_VALUE);
921+
configure_command(&mut cmd, &args, temp.path());
923922

924923
let output = cmd.output().expect("can run git");
925-
let stdout = output.stdout.to_str().expect("valid UTF-8");
924+
let lines: Vec<_> = output.stdout
925+
.to_str()
926+
.expect("valid UTF-8")
927+
.lines()
928+
.filter(|line| !line.starts_with("command line:\t"))
929+
.collect();
926930
let status = output.status.code().expect("terminated normally");
927-
assert_eq!(stdout, "", "should be no config variables to display");
928-
assert_eq!(status, 0, "reading the config should nonetheless succeed");
929-
}
930-
931-
#[test]
932-
fn configure_command_clears_system_scope() {
933-
check_configure_clears_scope("GIT_CONFIG_SYSTEM", "--system");
934-
}
935-
936-
#[test]
937-
fn configure_command_clears_global_scope() {
938-
check_configure_clears_scope("GIT_CONFIG_GLOBAL", "--global");
931+
assert_eq!(lines, Vec::<&str>::new(), "should be no config variables from files");
932+
assert_eq!(status, 0, "reading the config should succeed");
939933
}
940934
}

0 commit comments

Comments
 (0)