Skip to content

Commit d7dca27

Browse files
committed
Verify that we really write the strangely named test files
Ordinarily such a check would not be justified, but the behavior of `canonicalize()` in giving UNC paths is non-obvious.
1 parent f71d596 commit d7dca27

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

tests/tools/src/lib.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ impl<'a> Drop for Env<'a> {
869869
mod tests {
870870
use super::*;
871871
use std::fs::File;
872-
use std::io::Write;
872+
use std::io::{Read, Write};
873873

874874
#[test]
875875
fn parse_version() {
@@ -885,27 +885,44 @@ mod tests {
885885
assert_eq!(git_version_from_bytes(b"git version 2.37.2\n").unwrap(), (2, 37, 2));
886886
}
887887

888-
fn check_configure_clears_scope(scope_env_key: &str, scope_option: &str) {
889-
let scope_env_value = "gitconfig";
890-
let temp = tempfile::TempDir::new().expect("can create temp dir");
891-
let dir = temp.path();
888+
const SCOPE_ENV_VALUE: &str = "gitconfig";
889+
890+
fn populate_ad_hoc_config_files(dir: &Path) {
891+
const CONFIG_DATA: &[u8] = b"[foo]\n\tbar = baz\n";
892892

893893
let paths: &[PathBuf] = if cfg!(windows) {
894894
let unc_literal_nul = dir.canonicalize().expect("directory exists").join("NUL");
895-
&[dir.join(scope_env_value), dir.join("-"), unc_literal_nul]
895+
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), unc_literal_nul]
896896
} else {
897-
&[dir.join(scope_env_value), dir.join("-"), dir.join(":")]
897+
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), dir.join(":")]
898898
};
899899

900+
// Create the files.
900901
for path in paths {
901902
File::create_new(path)
902903
.expect("can create file")
903-
.write_all(b"[foo]\n\tbar = baz\n")
904+
.write_all(CONFIG_DATA)
904905
.expect("can write contents");
905906
}
906907

908+
// Verify the files. This is mostly to show we really made a `\\?\...\NUL` on Windows.
909+
for path in paths {
910+
let mut buf = Vec::with_capacity(CONFIG_DATA.len());
911+
File::open(path)
912+
.expect("the file really exists")
913+
.read_to_end(&mut buf)
914+
.expect("can read contents");
915+
assert_eq!(buf, CONFIG_DATA, "File {:?} should be created", path);
916+
}
917+
}
918+
919+
fn check_configure_clears_scope(scope_env_key: &str, scope_option: &str) {
920+
let temp = tempfile::TempDir::new().expect("can create temp dir");
921+
let dir = temp.path();
922+
populate_ad_hoc_config_files(dir);
923+
907924
let mut cmd = std::process::Command::new("git");
908-
cmd.env(scope_env_key, scope_env_value); // configure_command() should override it.
925+
cmd.env(scope_env_key, SCOPE_ENV_VALUE); // configure_command() should override it.
909926
let args = ["config", "-l", "--show-origin", scope_option].map(String::from);
910927
configure_command(&mut cmd, &args, dir);
911928

0 commit comments

Comments
 (0)