Skip to content

Commit 74f08e0

Browse files
authored
Merge pull request rust-lang#100 from whentze/clean-tests
Make `tests/full.rs` no longer use `git diff`
2 parents 282227e + 62af748 commit 74f08e0

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This does nothing.
2+
// It is only there because cargo will only set the $OUT_DIR env variable
3+
// for tests if there is a build script.
4+
fn main() {
5+
println!("cargo:rerun-if-changed=build.rs");
6+
}

tests/full.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod full {
22
use log::{log_enabled, Level};
33
use std::{
44
env,
5-
fs::File,
5+
fs::{read_to_string, File},
66
io::{BufRead, Write},
77
path::Path,
88
process::{Command, Stdio},
@@ -39,13 +39,6 @@ mod full {
3939

4040
let output = cmd.output().expect("could not run cargo semver");
4141

42-
assert_eq!(
43-
output.status.success(),
44-
expected_result,
45-
"cargo-semver returned unexpected exit status {}",
46-
output.status
47-
);
48-
4942
// Choose solution depending on the platform
5043
let file_ext = if cfg!(target_os = "macos") {
5144
"osx"
@@ -58,20 +51,17 @@ mod full {
5851
return;
5952
};
6053

61-
let filename = Path::new("tests/full_cases").join(format!(
54+
let filename = format!(
6255
"{}-{}-{}.{}",
6356
crate_name, old_version, new_version, file_ext
64-
));
65-
66-
assert!(
67-
filename.exists(),
68-
"file `{}` does not exist",
69-
filename.display()
7057
);
7158

72-
let mut file = File::create(&filename).expect("could not create output file");
59+
let expected_path = Path::new("tests/full_cases").join(&filename);
7360

74-
for line in output
61+
let expected_output =
62+
read_to_string(&expected_path).expect("could not read expected output from file");
63+
64+
let new_output = output
7565
.stdout
7666
.lines()
7767
.chain(output.stderr.lines())
@@ -81,34 +71,42 @@ mod full {
8171
!line.starts_with("version bump") &&
8272
// ...unless debugging is enabled
8373
!log_enabled!(Level::Debug))
84-
{
85-
// sanitize paths for reproducibility
86-
let output = match line.find("-->") {
87-
Some(idx) => {
88-
let (start, end) = line.split_at(idx);
89-
match end.find(crate_name) {
90-
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
91-
None => line,
74+
.map(|line| {
75+
// sanitize paths for reproducibility
76+
(match line.find("-->") {
77+
Some(idx) => {
78+
let (start, end) = line.split_at(idx);
79+
match end.find(crate_name) {
80+
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
81+
None => line,
82+
}
9283
}
93-
}
94-
None => line,
95-
};
96-
writeln!(file, "{}", output).expect("error writing to output file");
97-
}
84+
None => line,
85+
}) + "\n"
86+
})
87+
.collect::<String>();
9888

99-
let git_result = Command::new("git")
100-
.args(&[
101-
"diff",
102-
"--ignore-space-at-eol",
103-
"--exit-code",
104-
filename.to_str().unwrap(),
105-
])
106-
.env("PAGER", "")
107-
.status()
108-
.expect("could not run git diff")
109-
.success();
89+
if expected_output != new_output {
90+
eprintln!("cargo-semver failed to produce the expected output");
11091

111-
assert!(git_result, "git reports unexpected diff");
92+
let new_path = Path::new(&env::var("OUT_DIR").unwrap()).join(filename);
93+
let mut new_file = File::create(&new_path).unwrap();
94+
new_file.write_all(new_output.as_bytes()).unwrap();
95+
96+
eprintln!(
97+
"For details, try this command: \n\n diff {} {}\n\n",
98+
expected_path.display(),
99+
new_path.display()
100+
);
101+
panic!("unexpected output diff");
102+
}
103+
104+
assert_eq!(
105+
output.status.success(),
106+
expected_result,
107+
"cargo-semver returned unexpected exit status {}",
108+
output.status
109+
);
112110
}
113111

114112
macro_rules! full_test {

0 commit comments

Comments
 (0)