Skip to content

Commit 685da9f

Browse files
authored
Merge pull request rust-lang#96 from whentze/less-awkward-tests
Less awkward tests
2 parents 97534c5 + f8f307a commit 685da9f

File tree

2 files changed

+67
-95
lines changed

2 files changed

+67
-95
lines changed

Diff for: src/bin/cargo_semver.rs

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ fn main() {
6262
return;
6363
}
6464

65+
if matches.opt_present("q") {
66+
config.shell().set_verbosity(cargo::core::shell::Verbosity::Quiet);
67+
}
68+
6569
if let Err(e) = cli::validate_args(&matches) {
6670
cli::exit_with_error(&config, e);
6771
}
@@ -214,6 +218,7 @@ mod cli {
214218
opts.optflag("h", "help", "print this message and exit");
215219
opts.optflag("V", "version", "print version information and exit");
216220
opts.optflag("e", "explain", "print detailed error explanations");
221+
opts.optflag("q", "quiet", "surpress regular cargo output, print only important messages");
217222
opts.optflag("d", "debug", "print command to debug and exit");
218223
opts.optflag(
219224
"a",
@@ -293,6 +298,7 @@ mod cli {
293298

294299
/// Exit with error `e`.
295300
pub fn exit_with_error(config: &cargo::Config, e: failure::Error) -> ! {
301+
config.shell().set_verbosity(cargo::core::shell::Verbosity::Normal);
296302
cargo::exit_with_error(CliError::new(e, 1), &mut config.shell());
297303
}
298304
}

Diff for: tests/full.rs

+61-95
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
mod full {
2+
use log::{log_enabled, Level};
23
use std::{
34
env,
45
fs::File,
5-
path::{Path, PathBuf},
6+
io::{BufRead, Write},
7+
path::Path,
68
process::{Command, Stdio},
79
};
810

911
fn test_full(crate_name: &str, old_version: &str, new_version: &str, expected_result: bool) {
10-
let prog = format!(
11-
r#"
12-
# wait for the actual output
13-
/^version bump/ {{
14-
doprint = 1;
15-
}}
12+
// Add target dir to PATH so cargo-semver will call the right rust-semverver
13+
if let Some(path) = env::var_os("PATH") {
14+
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
15+
let current_dir = env::current_dir().expect("could not determine current dir");
16+
paths.insert(0, current_dir.join("target/debug"));
17+
let new_path = env::join_paths(paths).unwrap();
18+
env::set_var("PATH", &new_path);
19+
} else {
20+
eprintln!("no path!");
21+
}
1622

17-
{{
18-
# check the environ for filtering
19-
if (ENVIRON["RUST_LOG"] == "debug")
20-
doprint = 1;
23+
let mut cmd = Command::new("./target/debug/cargo-semver");
24+
cmd.args(&[
25+
"-S", &format!("{}:{}", crate_name, old_version),
26+
"-C", &format!("{}:{}", crate_name, new_version),
27+
"-q"
28+
])
29+
.env("RUST_BACKTRACE", "full")
30+
.stdin(Stdio::null())
31+
.stdout(Stdio::piped())
32+
.stderr(Stdio::piped());
33+
34+
if let Ok(target) = std::env::var("TEST_TARGET") {
35+
cmd.args(&["--target", &target]);
36+
}
2137

22-
# skip compilation info
23-
if (!doprint)
24-
next;
38+
let output = cmd.output().expect("could not run cargo semver");
2539

26-
# sanitize paths
27-
gsub(/-->.*{crate_name}/, "--> {crate_name}", $0);
28-
print;
29-
}}{crate_name}{crate_name}"#,
30-
crate_name = crate_name
40+
assert_eq!(output.status.success(), expected_result,
41+
"cargo-semver returned unexpected exit status {}", output.status
3142
);
3243

33-
let out_file = Path::new("tests/full_cases")
34-
.join(format!("{}-{}-{}", crate_name, old_version, new_version));
35-
3644
// Choose solution depending on the platform
3745
let file_ext = if cfg!(target_os = "macos") {
3846
"osx"
@@ -45,82 +53,40 @@ mod full {
4553
return;
4654
};
4755

48-
let out_file: PathBuf = format!("{}.{}", out_file.display(), file_ext).into();
49-
assert!(
50-
out_file.exists(),
51-
"file `{}` does not exist",
52-
out_file.display()
53-
);
54-
55-
if let Some(path) = env::var_os("PATH") {
56-
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
57-
let current_dir = env::current_dir().expect("could not determine current dir");
58-
paths.insert(0, current_dir.join("target/debug"));
59-
let new_path = env::join_paths(paths).unwrap();
60-
env::set_var("PATH", &new_path);
61-
} else {
62-
eprintln!("no path!");
56+
let filename = Path::new("tests/full_cases").join(format!(
57+
"{}-{}-{}.{}",
58+
crate_name, old_version, new_version, file_ext
59+
));
60+
61+
assert!(filename.exists(), "file `{}` does not exist", filename.display());
62+
63+
let mut file = File::create(&filename).expect("could not create output file");
64+
65+
for line in output.stdout.lines()
66+
.chain(output.stderr.lines())
67+
.map(|r| r.expect("could not read line from cargo-semver output"))
68+
.skip_while(|line|
69+
// skip everything before the first important bit of info
70+
!line.starts_with("version bump") &&
71+
// ...unless debugging is enabled
72+
!log_enabled!(Level::Debug))
73+
{
74+
// sanitize paths for reproducibility
75+
let output = match line.find("-->") {
76+
Some(idx) => {
77+
let (start, end) = line.split_at(idx);
78+
match end.find(crate_name) {
79+
Some(idx) => format!("{}--> {}", start, end.split_at(idx).1),
80+
None => line,
81+
}
82+
}
83+
None => line,
84+
};
85+
writeln!(file, "{}", output).expect("error writing to output file");
6386
}
6487

65-
let stdout = File::create(&out_file).expect("could not create `stdout` file");
66-
let out_file = out_file.to_str().unwrap();
67-
68-
let mut awk_child = Command::new("awk")
69-
.arg(&prog)
70-
.stdin(Stdio::piped())
71-
.stdout(stdout)
72-
.spawn()
73-
.expect("could not run awk");
74-
75-
let (err_pipe, out_pipe) = if let Some(ref stdin) = awk_child.stdin {
76-
#[cfg(unix)]
77-
{
78-
use std::os::unix::io::{AsRawFd, FromRawFd};
79-
let fd = stdin.as_raw_fd();
80-
unsafe { (Stdio::from_raw_fd(fd), Stdio::from_raw_fd(fd)) }
81-
}
82-
#[cfg(windows)]
83-
{
84-
use std::os::windows::io::{AsRawHandle, FromRawHandle};
85-
let fd = stdin.as_raw_handle();
86-
unsafe { (Stdio::from_raw_handle(fd), Stdio::from_raw_handle(fd)) }
87-
}
88-
} else {
89-
panic!("could not pipe to awk");
90-
};
91-
92-
let old_version = format!("{}:{}", crate_name, old_version);
93-
let new_version = format!("{}:{}", crate_name, new_version);
94-
95-
let cargo_semver_result = {
96-
let mut cmd = Command::new("./target/debug/cargo-semver");
97-
cmd.args(&["-S", &old_version, "-C", &new_version])
98-
.env("RUST_BACKTRACE", "full")
99-
.stdin(Stdio::null())
100-
.stdout(out_pipe)
101-
.stderr(err_pipe);
102-
103-
if let Ok(target) = std::env::var("TEST_TARGET") {
104-
cmd.args(&["--target", &target]);
105-
}
106-
107-
cmd.status().expect("could not run cargo semver").success()
108-
};
109-
110-
assert_eq!(
111-
cargo_semver_result, expected_result,
112-
"cargo semver returned an unexpected exit status"
113-
);
114-
115-
let awk_result = awk_child
116-
.wait()
117-
.expect("could not wait for awk child")
118-
.success();
119-
120-
assert!(awk_result, "awk");
121-
12288
let git_result = Command::new("git")
123-
.args(&["diff", "--ignore-space-at-eol", "--exit-code", out_file])
89+
.args(&["diff", "--ignore-space-at-eol", "--exit-code", filename.to_str().unwrap()])
12490
.env("PAGER", "")
12591
.status()
12692
.expect("could not run git diff")

0 commit comments

Comments
 (0)