Skip to content

Commit 748fa15

Browse files
committed
refactor
1 parent 501da55 commit 748fa15

4 files changed

Lines changed: 162 additions & 232 deletions

File tree

tests/tools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fixtures/

tests/tools/src/lib.rs

Lines changed: 17 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,7 @@ pub fn scripted_fixture_read_only_with_args_standalone_single_archive(
492492
/// Ok(())
493493
/// }
494494
/// ```
495-
pub fn rust_fixture_read_only(
496-
name: &str,
497-
version: u32,
498-
make_fixture: impl FnOnce(&Path) -> Result,
499-
) -> Result<PathBuf> {
495+
pub fn rust_fixture_read_only(name: &str, version: u32, make_fixture: impl FnOnce(&Path) -> Result) -> Result<PathBuf> {
500496
rust_fixture_read_only_inner(name, version, make_fixture, None, DirectoryRoot::IntegrationTest)
501497
}
502498

@@ -593,18 +589,17 @@ fn rust_fixture_read_only_inner(
593589
let archive_name = format!("rust-{name}");
594590

595591
let archive_file_path = fixture_path_inner(
596-
Path::new("generated-archives").join(format!(
597-
"{archive_name}.tar{}",
598-
if cfg!(feature = "xz") { ".xz" } else { "" }
599-
)),
592+
Path::new("generated-archives").join(format!("{archive_name}.{}", tar_extension())),
600593
root,
601594
);
602595
let (force_run, script_result_directory) = destination_dir.map_or_else(
603596
|| {
604597
let dir = fixture_path_inner(
605-
Path::new("generated-do-not-edit")
606-
.join(&archive_name)
607-
.join(format!("{}-{}", script_identity, family_name())),
598+
Path::new("generated-do-not-edit").join(&archive_name).join(format!(
599+
"{}-{}",
600+
script_identity,
601+
family_name()
602+
)),
608603
root,
609604
);
610605
(false, dir)
@@ -727,9 +722,9 @@ fn scripted_fixture_read_only_with_args_inner(
727722
ArgsInHash::No => "".into(),
728723
};
729724
Path::new("generated-archives").join(format!(
730-
"{}{suffix}.tar{}",
725+
"{}{suffix}.{}",
731726
script_basename.to_str().expect("valid UTF-8"),
732-
if cfg!(feature = "xz") { ".xz" } else { "" }
727+
tar_extension()
733728
))
734729
},
735730
root,
@@ -1220,133 +1215,13 @@ pub fn umask() -> u32 {
12201215
u32::from_str_radix(text, 8).expect("parses as octal number")
12211216
}
12221217

1223-
#[cfg(test)]
1224-
mod tests {
1225-
use super::*;
1226-
1227-
#[test]
1228-
fn parse_version() {
1229-
assert_eq!(git_version_from_bytes(b"git version 2.37.2").unwrap(), (2, 37, 2));
1230-
assert_eq!(
1231-
git_version_from_bytes(b"git version 2.32.1 (Apple Git-133)").unwrap(),
1232-
(2, 32, 1)
1233-
);
1234-
}
1235-
1236-
#[test]
1237-
fn parse_version_with_trailing_newline() {
1238-
assert_eq!(git_version_from_bytes(b"git version 2.37.2\n").unwrap(), (2, 37, 2));
1239-
}
1240-
1241-
const SCOPE_ENV_VALUE: &str = "gitconfig";
1242-
1243-
fn populate_ad_hoc_config_files(dir: &Path) {
1244-
const CONFIG_DATA: &[u8] = b"[foo]\n\tbar = baz\n";
1245-
1246-
let paths: &[PathBuf] = if cfg!(windows) {
1247-
let unc_literal_nul = dir.canonicalize().expect("directory exists").join("nul");
1248-
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), unc_literal_nul]
1249-
} else {
1250-
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), dir.join(":")]
1251-
};
1252-
// Create the files.
1253-
for path in paths {
1254-
std::fs::write(path, CONFIG_DATA).expect("can write contents");
1255-
}
1256-
// Verify the files. This is mostly to show we really made a `\\?\...\nul` on Windows.
1257-
for path in paths {
1258-
let buf = std::fs::read(path).expect("the file really exists");
1259-
assert_eq!(buf, CONFIG_DATA, "{path:?} should be a config file");
1260-
}
1261-
}
1262-
1263-
#[test]
1264-
fn configure_command_clears_external_config() {
1265-
let temp = tempfile::TempDir::new().expect("can create temp dir");
1266-
populate_ad_hoc_config_files(temp.path());
1267-
1268-
let mut cmd = std::process::Command::new(GIT_PROGRAM);
1269-
cmd.env("GIT_CONFIG_SYSTEM", SCOPE_ENV_VALUE);
1270-
cmd.env("GIT_CONFIG_GLOBAL", SCOPE_ENV_VALUE);
1271-
configure_command(&mut cmd, ["config", "-l", "--show-origin"], temp.path());
1272-
1273-
let output = cmd.output().expect("can run git");
1274-
let lines: Vec<_> = output
1275-
.stdout
1276-
.to_str()
1277-
.expect("valid UTF-8")
1278-
.lines()
1279-
.filter(|line| !line.starts_with("command line:\t"))
1280-
.collect();
1281-
let status = output.status.code().expect("terminated normally");
1282-
assert_eq!(lines, Vec::<&str>::new(), "should be no config variables from files");
1283-
assert_eq!(status, 0, "reading the config should succeed");
1284-
}
1285-
1286-
#[test]
1287-
#[cfg(windows)]
1288-
fn bash_program_ok_for_platform() {
1289-
let path = bash_program();
1290-
assert!(path.is_absolute());
1291-
1292-
let for_version = std::process::Command::new(path)
1293-
.arg("--version")
1294-
.output()
1295-
.expect("can pass it `--version`");
1296-
assert!(for_version.status.success(), "passing `--version` succeeds");
1297-
let version_line = for_version
1298-
.stdout
1299-
.lines()
1300-
.nth(0)
1301-
.expect("`--version` output has first line");
1302-
assert!(
1303-
version_line.ends_with(b"-pc-msys)"), // On Windows, "-pc-linux-gnu)" would be WSL.
1304-
"it is an MSYS bash (such as Git Bash)"
1305-
);
1306-
1307-
let for_uname_os = std::process::Command::new(path)
1308-
.args(["-c", "uname -o"])
1309-
.output()
1310-
.expect("can tell it to run `uname -o`");
1311-
assert!(for_uname_os.status.success(), "telling it to run `uname -o` succeeds");
1312-
assert_eq!(
1313-
for_uname_os.stdout.trim_end(),
1314-
b"Msys",
1315-
"it runs commands in an MSYS environment"
1316-
);
1317-
}
1318-
1319-
#[test]
1320-
#[cfg(not(windows))]
1321-
fn bash_program_ok_for_platform() {
1322-
assert_eq!(bash_program(), Path::new("bash"));
1323-
}
1324-
1325-
#[test]
1326-
fn bash_program_unix_path() {
1327-
let path = bash_program()
1328-
.to_str()
1329-
.expect("This test depends on the bash path being valid Unicode");
1330-
assert!(
1331-
!path.contains('\\'),
1332-
"The path to bash should have no backslashes, barring very unusual environments"
1333-
);
1334-
}
1335-
1336-
fn is_rooted_relative(path: impl AsRef<Path>) -> bool {
1337-
let p = path.as_ref();
1338-
p.is_relative() && p.has_root()
1339-
}
1340-
1341-
#[test]
1342-
#[cfg(windows)]
1343-
fn unix_style_absolute_is_rooted_relative() {
1344-
assert!(is_rooted_relative("/bin/bash"), "can detect paths like /bin/bash");
1345-
}
1346-
1347-
#[test]
1348-
fn bash_program_absolute_or_unrooted() {
1349-
let bash = bash_program();
1350-
assert!(!is_rooted_relative(bash), "{bash:?}");
1218+
fn tar_extension() -> &'static str {
1219+
if cfg!(feature = "xz") {
1220+
".tar.xz"
1221+
} else {
1222+
".tar"
13511223
}
13521224
}
1225+
1226+
#[cfg(test)]
1227+
mod tests;

tests/tools/src/tests.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
use super::*;
2+
3+
#[test]
4+
fn parse_version() {
5+
assert_eq!(git_version_from_bytes(b"git version 2.37.2").unwrap(), (2, 37, 2));
6+
assert_eq!(
7+
git_version_from_bytes(b"git version 2.32.1 (Apple Git-133)").unwrap(),
8+
(2, 32, 1)
9+
);
10+
}
11+
12+
#[test]
13+
fn parse_version_with_trailing_newline() {
14+
assert_eq!(git_version_from_bytes(b"git version 2.37.2\n").unwrap(), (2, 37, 2));
15+
}
16+
17+
const SCOPE_ENV_VALUE: &str = "gitconfig";
18+
19+
fn populate_ad_hoc_config_files(dir: &Path) {
20+
const CONFIG_DATA: &[u8] = b"[foo]\n\tbar = baz\n";
21+
22+
let paths: &[PathBuf] = if cfg!(windows) {
23+
let unc_literal_nul = dir.canonicalize().expect("directory exists").join("nul");
24+
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), unc_literal_nul]
25+
} else {
26+
&[dir.join(SCOPE_ENV_VALUE), dir.join("-"), dir.join(":")]
27+
};
28+
// Create the files.
29+
for path in paths {
30+
std::fs::write(path, CONFIG_DATA).expect("can write contents");
31+
}
32+
// Verify the files. This is mostly to show we really made a `\\?\...\nul` on Windows.
33+
for path in paths {
34+
let buf = std::fs::read(path).expect("the file really exists");
35+
assert_eq!(buf, CONFIG_DATA, "{path:?} should be a config file");
36+
}
37+
}
38+
39+
#[test]
40+
fn configure_command_clears_external_config() {
41+
let temp = tempfile::TempDir::new().expect("can create temp dir");
42+
populate_ad_hoc_config_files(temp.path());
43+
44+
let mut cmd = std::process::Command::new(GIT_PROGRAM);
45+
cmd.env("GIT_CONFIG_SYSTEM", SCOPE_ENV_VALUE);
46+
cmd.env("GIT_CONFIG_GLOBAL", SCOPE_ENV_VALUE);
47+
configure_command(&mut cmd, ["config", "-l", "--show-origin"], temp.path());
48+
49+
let output = cmd.output().expect("can run git");
50+
let lines: Vec<_> = output
51+
.stdout
52+
.to_str()
53+
.expect("valid UTF-8")
54+
.lines()
55+
.filter(|line| !line.starts_with("command line:\t"))
56+
.collect();
57+
let status = output.status.code().expect("terminated normally");
58+
assert_eq!(lines, Vec::<&str>::new(), "should be no config variables from files");
59+
assert_eq!(status, 0, "reading the config should succeed");
60+
}
61+
62+
#[test]
63+
#[cfg(windows)]
64+
fn bash_program_ok_for_platform() {
65+
let path = bash_program();
66+
assert!(path.is_absolute());
67+
68+
let for_version = std::process::Command::new(path)
69+
.arg("--version")
70+
.output()
71+
.expect("can pass it `--version`");
72+
assert!(for_version.status.success(), "passing `--version` succeeds");
73+
let version_line = for_version
74+
.stdout
75+
.lines()
76+
.nth(0)
77+
.expect("`--version` output has first line");
78+
assert!(
79+
version_line.ends_with(b"-pc-msys)"), // On Windows, "-pc-linux-gnu)" would be WSL.
80+
"it is an MSYS bash (such as Git Bash)"
81+
);
82+
83+
let for_uname_os = std::process::Command::new(path)
84+
.args(["-c", "uname -o"])
85+
.output()
86+
.expect("can tell it to run `uname -o`");
87+
assert!(for_uname_os.status.success(), "telling it to run `uname -o` succeeds");
88+
assert_eq!(
89+
for_uname_os.stdout.trim_end(),
90+
b"Msys",
91+
"it runs commands in an MSYS environment"
92+
);
93+
}
94+
95+
#[test]
96+
#[cfg(not(windows))]
97+
fn bash_program_ok_for_platform() {
98+
assert_eq!(bash_program(), Path::new("bash"));
99+
}
100+
101+
#[test]
102+
fn bash_program_unix_path() {
103+
let path = bash_program()
104+
.to_str()
105+
.expect("This test depends on the bash path being valid Unicode");
106+
assert!(
107+
!path.contains('\\'),
108+
"The path to bash should have no backslashes, barring very unusual environments"
109+
);
110+
}
111+
112+
fn is_rooted_relative(path: impl AsRef<Path>) -> bool {
113+
let p = path.as_ref();
114+
p.is_relative() && p.has_root()
115+
}
116+
117+
#[test]
118+
#[cfg(windows)]
119+
fn unix_style_absolute_is_rooted_relative() {
120+
assert!(is_rooted_relative("/bin/bash"), "can detect paths like /bin/bash");
121+
}
122+
123+
#[test]
124+
fn bash_program_absolute_or_unrooted() {
125+
let bash = bash_program();
126+
assert!(!is_rooted_relative(bash), "{bash:?}");
127+
}

0 commit comments

Comments
 (0)