Skip to content

Commit

Permalink
Merge pull request rust-lang#100 from whentze/clean-tests
Browse files Browse the repository at this point in the history
Make `tests/full.rs` no longer use `git diff`
  • Loading branch information
ibabushkin authored Mar 28, 2019
2 parents 282227e + 62af748 commit 74f08e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This does nothing.
// It is only there because cargo will only set the $OUT_DIR env variable
// for tests if there is a build script.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
}
82 changes: 40 additions & 42 deletions tests/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod full {
use log::{log_enabled, Level};
use std::{
env,
fs::File,
fs::{read_to_string, File},
io::{BufRead, Write},
path::Path,
process::{Command, Stdio},
Expand Down Expand Up @@ -39,13 +39,6 @@ mod full {

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

assert_eq!(
output.status.success(),
expected_result,
"cargo-semver returned unexpected exit status {}",
output.status
);

// Choose solution depending on the platform
let file_ext = if cfg!(target_os = "macos") {
"osx"
Expand All @@ -58,20 +51,17 @@ mod full {
return;
};

let filename = Path::new("tests/full_cases").join(format!(
let filename = format!(
"{}-{}-{}.{}",
crate_name, old_version, new_version, file_ext
));

assert!(
filename.exists(),
"file `{}` does not exist",
filename.display()
);

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

for line in output
let expected_output =
read_to_string(&expected_path).expect("could not read expected output from file");

let new_output = output
.stdout
.lines()
.chain(output.stderr.lines())
Expand All @@ -81,34 +71,42 @@ mod full {
!line.starts_with("version bump") &&
// ...unless debugging is enabled
!log_enabled!(Level::Debug))
{
// sanitize paths for reproducibility
let output = match line.find("-->") {
Some(idx) => {
let (start, end) = line.split_at(idx);
match end.find(crate_name) {
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
None => line,
.map(|line| {
// sanitize paths for reproducibility
(match line.find("-->") {
Some(idx) => {
let (start, end) = line.split_at(idx);
match end.find(crate_name) {
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
None => line,
}
}
}
None => line,
};
writeln!(file, "{}", output).expect("error writing to output file");
}
None => line,
}) + "\n"
})
.collect::<String>();

let git_result = Command::new("git")
.args(&[
"diff",
"--ignore-space-at-eol",
"--exit-code",
filename.to_str().unwrap(),
])
.env("PAGER", "")
.status()
.expect("could not run git diff")
.success();
if expected_output != new_output {
eprintln!("cargo-semver failed to produce the expected output");

assert!(git_result, "git reports unexpected diff");
let new_path = Path::new(&env::var("OUT_DIR").unwrap()).join(filename);
let mut new_file = File::create(&new_path).unwrap();
new_file.write_all(new_output.as_bytes()).unwrap();

eprintln!(
"For details, try this command: \n\n diff {} {}\n\n",
expected_path.display(),
new_path.display()
);
panic!("unexpected output diff");
}

assert_eq!(
output.status.success(),
expected_result,
"cargo-semver returned unexpected exit status {}",
output.status
);
}

macro_rules! full_test {
Expand Down

0 comments on commit 74f08e0

Please sign in to comment.