Skip to content

Commit 0a190e8

Browse files
committed
Migrate runmake tests away from custom commands and command_output
1 parent 18ae9af commit 0a190e8

File tree

30 files changed

+176
-141
lines changed

30 files changed

+176
-141
lines changed

src/tools/run-make-support/src/cc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22

3-
use crate::{bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, uname};
43
use crate::command::Command;
4+
use crate::{bin_name, cygpath_windows, env_var, is_msvc, is_windows, uname};
55

66
/// Construct a new platform-specific C compiler invocation.
77
///

src/tools/run-make-support/src/clang.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22

3-
use crate::{bin_name, env_var, handle_failed_output};
43
use crate::command::Command;
4+
use crate::{bin_name, env_var};
55

66
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
77
pub fn clang() -> Clang {

src/tools/run-make-support/src/command.rs

+68-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use crate::{assert_not_contains, handle_failed_output};
12
use std::ffi::OsStr;
23
use std::io::Write;
34
use std::ops::{Deref, DerefMut};
45
use std::process::{Command as StdCommand, ExitStatus, Output, Stdio};
56

7+
/// This is a custom command wrapper that simplifies working with commands
8+
/// and makes it easier to ensure that we check the exit status of executed
9+
/// processes.
610
#[derive(Debug)]
711
pub struct Command {
812
cmd: StdCommand,
@@ -11,16 +15,39 @@ pub struct Command {
1115

1216
impl Command {
1317
pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
14-
Self {
15-
cmd: StdCommand::new(program),
16-
stdin: None,
17-
}
18+
Self { cmd: StdCommand::new(program), stdin: None }
1819
}
1920

2021
pub fn set_stdin(&mut self, stdin: Box<[u8]>) {
2122
self.stdin = Some(stdin);
2223
}
2324

25+
/// Run the constructed command and assert that it is successfully run.
26+
#[track_caller]
27+
pub fn run(&mut self) -> CompletedProcess {
28+
let caller_location = std::panic::Location::caller();
29+
let caller_line_number = caller_location.line();
30+
31+
let output = self.command_output();
32+
if !output.status().success() {
33+
handle_failed_output(&self, output, caller_line_number);
34+
}
35+
output
36+
}
37+
38+
/// Run the constructed command and assert that it does not successfully run.
39+
#[track_caller]
40+
pub fn run_fail(&mut self) -> CompletedProcess {
41+
let caller_location = std::panic::Location::caller();
42+
let caller_line_number = caller_location.line();
43+
44+
let output = self.command_output();
45+
if output.status().success() {
46+
handle_failed_output(&self, output, caller_line_number);
47+
}
48+
output
49+
}
50+
2451
#[track_caller]
2552
pub(crate) fn command_output(&mut self) -> CompletedProcess {
2653
// let's make sure we piped all the input and outputs
@@ -59,6 +86,8 @@ impl DerefMut for Command {
5986
}
6087

6188
/// Represents the result of an executed process.
89+
/// The various `assert_` helper methods should preferably be used for
90+
/// checking the contents of stdout/stderr.
6291
pub struct CompletedProcess {
6392
output: Output,
6493
}
@@ -76,16 +105,47 @@ impl CompletedProcess {
76105
self.output.status
77106
}
78107

108+
/// Checks that trimmed `stdout` matches trimmed `content`.
109+
#[track_caller]
110+
pub fn assert_stdout_equals<S: AsRef<str>>(self, content: S) -> Self {
111+
assert_eq!(self.stdout_utf8().trim(), content.as_ref().trim());
112+
self
113+
}
114+
79115
#[track_caller]
80-
pub fn assert_exit_code(&self, code: i32) {
116+
pub fn assert_stdout_not_contains<S: AsRef<str>>(self, needle: S) -> Self {
117+
assert_not_contains(&self.stdout_utf8(), needle.as_ref());
118+
self
119+
}
120+
121+
/// Checks that trimmed `stderr` matches trimmed `content`.
122+
#[track_caller]
123+
pub fn assert_stderr_equals<S: AsRef<str>>(self, content: S) -> Self {
124+
assert_eq!(self.stderr_utf8().trim(), content.as_ref().trim());
125+
self
126+
}
127+
128+
#[track_caller]
129+
pub fn assert_stderr_contains<S: AsRef<str>>(self, needle: S) -> Self {
130+
assert!(self.stderr_utf8().contains(needle.as_ref()));
131+
self
132+
}
133+
134+
#[track_caller]
135+
pub fn assert_stderr_not_contains<S: AsRef<str>>(self, needle: S) -> Self {
136+
assert_not_contains(&self.stdout_utf8(), needle.as_ref());
137+
self
138+
}
139+
140+
#[track_caller]
141+
pub fn assert_exit_code(self, code: i32) -> Self {
81142
assert!(self.output.status.code() == Some(code));
143+
self
82144
}
83145
}
84146

85147
impl From<Output> for CompletedProcess {
86148
fn from(output: Output) -> Self {
87-
Self {
88-
output
89-
}
149+
Self { output }
90150
}
91151
}

src/tools/run-make-support/src/lib.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
66
pub mod cc;
77
pub mod clang;
8+
mod command;
89
pub mod diff;
910
pub mod llvm_readobj;
1011
pub mod run;
1112
pub mod rustc;
1213
pub mod rustdoc;
13-
mod command;
1414

1515
use std::env;
1616
use std::ffi::OsString;
@@ -27,7 +27,7 @@ pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
2727
pub use clang::{clang, Clang};
2828
pub use diff::{diff, Diff};
2929
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
30-
pub use run::{run, run_fail};
30+
pub use run::{cmd, run, run_fail};
3131
pub use rustc::{aux_build, rustc, Rustc};
3232
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
3333

@@ -285,6 +285,7 @@ pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) {
285285
}
286286

287287
/// Check that `haystack` does not contain `needle`. Panic otherwise.
288+
#[track_caller]
288289
pub fn assert_not_contains(haystack: &str, needle: &str) {
289290
if haystack.contains(needle) {
290291
eprintln!("=== HAYSTACK ===");
@@ -412,27 +413,13 @@ macro_rules! impl_common_helpers {
412413
/// Run the constructed command and assert that it is successfully run.
413414
#[track_caller]
414415
pub fn run(&mut self) -> crate::command::CompletedProcess {
415-
let caller_location = ::std::panic::Location::caller();
416-
let caller_line_number = caller_location.line();
417-
418-
let output = self.cmd.command_output();
419-
if !output.status().success() {
420-
handle_failed_output(&self.cmd, output, caller_line_number);
421-
}
422-
output
416+
self.cmd.run()
423417
}
424418

425419
/// Run the constructed command and assert that it does not successfully run.
426420
#[track_caller]
427421
pub fn run_fail(&mut self) -> crate::command::CompletedProcess {
428-
let caller_location = ::std::panic::Location::caller();
429-
let caller_line_number = caller_location.line();
430-
431-
let output = self.cmd.command_output();
432-
if output.status().success() {
433-
handle_failed_output(&self.cmd, output, caller_line_number);
434-
}
435-
output
422+
self.cmd.run_fail()
436423
}
437424

438425
/// Set the path where the command will be run.
@@ -444,5 +431,5 @@ macro_rules! impl_common_helpers {
444431
};
445432
}
446433

447-
pub(crate) use impl_common_helpers;
448434
use crate::command::{Command, CompletedProcess};
435+
pub(crate) use impl_common_helpers;

src/tools/run-make-support/src/llvm_readobj.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::{Path, PathBuf};
22

3-
use crate::{env_var, handle_failed_output};
43
use crate::command::Command;
4+
use crate::env_var;
55

66
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
77
/// at `$LLVM_BIN_DIR/llvm-readobj`.

src/tools/run-make-support/src/run.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::env;
2+
use std::ffi::OsStr;
23
use std::path::{Path, PathBuf};
34

4-
use crate::{cwd, env_var, is_windows};
55
use crate::command::{Command, CompletedProcess};
6+
use crate::{cwd, env_var, is_windows, set_host_rpath};
67

78
use super::handle_failed_output;
89

@@ -62,3 +63,11 @@ pub fn run_fail(name: &str) -> CompletedProcess {
6263
}
6364
output
6465
}
66+
67+
/// Create a new custom Command.
68+
/// This should be preferred to creating `std::process::Command` directly.
69+
pub fn cmd<S: AsRef<OsStr>>(program: S) -> Command {
70+
let mut command = Command::new(program);
71+
set_host_rpath(&mut command);
72+
command
73+
}

src/tools/run-make-support/src/rustc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use command::Command;
12
use std::ffi::{OsStr, OsString};
23
use std::path::Path;
3-
use command::Command;
44

5-
use crate::{command, cwd, env_var, handle_failed_output, set_host_rpath};
5+
use crate::{command, cwd, env_var, set_host_rpath};
66

77
/// Construct a new `rustc` invocation.
88
pub fn rustc() -> Rustc {

src/tools/run-make-support/src/rustdoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::ffi::OsStr;
22
use std::path::Path;
33

4-
use crate::{env_var, env_var_os, handle_failed_output, set_host_rpath};
54
use crate::command::Command;
5+
use crate::{env_var, env_var_os, set_host_rpath};
66

77
/// Construct a plain `rustdoc` invocation with no flags set.
88
pub fn bare_rustdoc() -> Rustdoc {

tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ use run_make_support::{aux_build, rustc, source_root};
1010
fn main() {
1111
aux_build().input("stable.rs").emit("metadata").run();
1212

13-
let output = rustc()
14-
.input("main.rs")
15-
.emit("metadata")
16-
.extern_("stable", "libstable.rmeta")
17-
.command_output();
13+
let output =
14+
rustc().input("main.rs").emit("metadata").extern_("stable", "libstable.rmeta").run();
1815

19-
let stderr = String::from_utf8_lossy(&output.stderr);
2016
let version = std::fs::read_to_string(source_root().join("src/version")).unwrap();
2117
let expected_string = format!("stable since {}", version.trim());
22-
assert!(stderr.contains(&expected_string));
18+
output.assert_stderr_contains(expected_string);
2319
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Test that `-Awarnings` suppresses warnings for unstable APIs.
22

3-
use run_make_support::{assert_not_contains, rustc};
3+
use run_make_support::rustc;
44

55
fn main() {
66
rustc().input("bar.rs").run();
7-
let output = rustc().input("foo.rs").arg("-Awarnings").run();
8-
9-
assert_not_contains(&String::from_utf8(output.stdout).unwrap(), "warning");
10-
assert_not_contains(&String::from_utf8(output.stderr).unwrap(), "warning");
7+
rustc()
8+
.input("foo.rs")
9+
.arg("-Awarnings")
10+
.run()
11+
.assert_stdout_not_contains("warning")
12+
.assert_stderr_not_contains("warning");
1113
}

tests/run-make/compiler-builtins/rmake.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use run_make_support::object::read::Object;
1919
use run_make_support::object::ObjectSection;
2020
use run_make_support::object::ObjectSymbol;
2121
use run_make_support::object::RelocationTarget;
22-
use run_make_support::set_host_rpath;
23-
use run_make_support::{env_var, object};
22+
use run_make_support::{cmd, env_var, object};
2423
use std::collections::HashSet;
2524
use std::path::PathBuf;
2625

@@ -35,7 +34,7 @@ fn main() {
3534
let path = env_var("PATH");
3635
let rustc = env_var("RUSTC");
3736
let bootstrap_cargo = env_var("BOOTSTRAP_CARGO");
38-
let mut cmd = std::process::Command::new(bootstrap_cargo);
37+
let mut cmd = cmd(bootstrap_cargo);
3938
cmd.args([
4039
"build",
4140
"--manifest-path",
@@ -52,10 +51,8 @@ fn main() {
5251
// Visual Studio 2022 requires that the LIB env var be set so it can
5352
// find the Windows SDK.
5453
.env("LIB", std::env::var("LIB").unwrap_or_default());
55-
set_host_rpath(&mut cmd);
5654

57-
let status = cmd.status().unwrap();
58-
assert!(status.success());
55+
cmd.run();
5956

6057
let rlibs_path = target_dir.join(target).join("debug").join("deps");
6158
let compiler_builtins_rlib = std::fs::read_dir(rlibs_path)

tests/run-make/const-prop-lint/rmake.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs;
55
use run_make_support::{cwd, rustc};
66

77
fn main() {
8-
rustc().input("input.rs").run_fail_assert_exit_code(1);
8+
rustc().input("input.rs").run_fail().assert_exit_code(1);
99

1010
for entry in fs::read_dir(cwd()).unwrap() {
1111
let entry = entry.unwrap();
+17-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
use std::process::Output;
2-
31
use run_make_support::{bin_name, rust_lib_name, rustc};
42

5-
fn compare_stdout<S: AsRef<str>>(output: Output, expected: S) {
6-
assert_eq!(String::from_utf8(output.stdout).unwrap().trim(), expected.as_ref());
7-
}
8-
93
fn main() {
10-
compare_stdout(rustc().print("crate-name").input("crate.rs").run(), "foo");
11-
compare_stdout(rustc().print("file-names").input("crate.rs").run(), bin_name("foo"));
12-
compare_stdout(
13-
rustc().print("file-names").crate_type("lib").arg("--test").input("crate.rs").run(),
14-
bin_name("foo"),
15-
);
16-
compare_stdout(
17-
rustc().print("file-names").arg("--test").input("lib.rs").run(),
18-
bin_name("mylib"),
19-
);
20-
compare_stdout(rustc().print("file-names").input("lib.rs").run(), rust_lib_name("mylib"));
21-
compare_stdout(rustc().print("file-names").input("rlib.rs").run(), rust_lib_name("mylib"));
4+
rustc().print("crate-name").input("crate.rs").run().assert_stdout_equals("foo");
5+
rustc().print("file-names").input("crate.rs").run().assert_stdout_equals(bin_name("foo"));
6+
rustc()
7+
.print("file-names")
8+
.crate_type("lib")
9+
.arg("--test")
10+
.input("crate.rs")
11+
.run()
12+
.assert_stdout_equals(bin_name("foo"));
13+
rustc()
14+
.print("file-names")
15+
.arg("--test")
16+
.input("lib.rs")
17+
.run()
18+
.assert_stdout_equals(bin_name("mylib"));
19+
rustc().print("file-names").input("lib.rs").run().assert_stdout_equals(rust_lib_name("mylib"));
20+
rustc().print("file-names").input("rlib.rs").run().assert_stdout_equals(rust_lib_name("mylib"));
2221
}

0 commit comments

Comments
 (0)