Skip to content

Commit 651d799

Browse files
committed
move exit-code to rmake
1 parent 2dceda4 commit 651d799

File tree

4 files changed

+136
-12
lines changed

4 files changed

+136
-12
lines changed

Diff for: src/tools/run-make-support/src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use std::env;
22
use std::path::{Path, PathBuf};
33
use std::process::{Command, Output};
44

5+
mod rustdoc;
6+
pub use rustdoc::rustdoc;
7+
58
fn setup_common_build_cmd() -> Command {
69
let rustc = env::var("RUSTC").unwrap();
710
let mut cmd = Command::new(rustc);
@@ -45,6 +48,11 @@ impl RustcInvocationBuilder {
4548
self
4649
}
4750

51+
pub fn env(&mut self, key: &str, value: &str) -> &mut RustcInvocationBuilder {
52+
self.cmd.env(key, value);
53+
self
54+
}
55+
4856
#[track_caller]
4957
pub fn run(&mut self) -> Output {
5058
let caller_location = std::panic::Location::caller();
@@ -56,6 +64,18 @@ impl RustcInvocationBuilder {
5664
}
5765
output
5866
}
67+
68+
#[track_caller]
69+
pub fn run_fail(&mut self) -> Output {
70+
let caller_location = std::panic::Location::caller();
71+
let caller_line_number = caller_location.line();
72+
73+
let output = self.cmd.output().unwrap();
74+
if output.status.success() {
75+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
76+
}
77+
output
78+
}
5979
}
6080

6181
#[derive(Debug)]
@@ -149,3 +169,7 @@ pub fn run_fail(bin_name: &str) -> Output {
149169
}
150170
output
151171
}
172+
173+
pub fn tempdir() -> PathBuf {
174+
PathBuf::from(env::var("TMPDIR").unwrap())
175+
}

Diff for: src/tools/run-make-support/src/rustdoc.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::path::Path;
2+
use std::process::{Command, Output};
3+
4+
use crate::{handle_failed_output, setup_common_build_cmd};
5+
6+
pub fn rustdoc() -> RustdocInvocationBuilder {
7+
RustdocInvocationBuilder::new()
8+
}
9+
10+
#[derive(Debug)]
11+
pub struct RustdocInvocationBuilder {
12+
cmd: Command,
13+
}
14+
15+
impl RustdocInvocationBuilder {
16+
fn new() -> Self {
17+
let cmd = setup_common_build_cmd();
18+
Self { cmd }
19+
}
20+
21+
pub fn arg(&mut self, arg: &str) -> &mut RustdocInvocationBuilder {
22+
self.cmd.arg(arg);
23+
self
24+
}
25+
26+
pub fn arg_file(&mut self, arg: &Path) -> &mut RustdocInvocationBuilder {
27+
self.cmd.arg(arg);
28+
self
29+
}
30+
31+
pub fn env(&mut self, key: &str, value: &str) -> &mut RustdocInvocationBuilder {
32+
self.cmd.env(key, value);
33+
self
34+
}
35+
36+
#[track_caller]
37+
pub fn run(&mut self) -> Output {
38+
let caller_location = std::panic::Location::caller();
39+
let caller_line_number = caller_location.line();
40+
41+
let output = self.cmd.output().unwrap();
42+
if !output.status.success() {
43+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
44+
}
45+
output
46+
}
47+
48+
#[track_caller]
49+
pub fn run_fail(&mut self) -> Output {
50+
let caller_location = std::panic::Location::caller();
51+
let caller_line_number = caller_location.line();
52+
53+
let output = self.cmd.output().unwrap();
54+
if output.status.success() {
55+
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
56+
}
57+
output
58+
}
59+
}

Diff for: tests/run-make/exit-code/Makefile

-12
This file was deleted.

Diff for: tests/run-make/exit-code/rmake.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{rustc, rustdoc, tempdir};
4+
5+
fn main() {
6+
rustc()
7+
.arg("success.rs")
8+
.run();
9+
10+
assert_eq!(rustc()
11+
.arg("--invalid-arg-foo")
12+
.run_fail()
13+
.status
14+
.code().unwrap(), 1);
15+
16+
assert_eq!(rustc()
17+
.arg("compile-error.rs")
18+
.run_fail()
19+
.status
20+
.code().unwrap(), 1);
21+
22+
assert_eq!(rustc()
23+
.env("RUSTC_ICE", "0")
24+
.arg("-Ztreat-err-as-bug")
25+
.arg("compile-error.rs")
26+
.run_fail()
27+
.status
28+
.code().unwrap(), 101);
29+
30+
rustdoc()
31+
.arg("-o")
32+
.arg_file(&tempdir().join("exit-code"))
33+
.arg("success.rs")
34+
.run();
35+
36+
assert_eq!(rustdoc()
37+
.arg("--invalid-arg-foo")
38+
.run_fail()
39+
.status
40+
.code().unwrap(), 1);
41+
42+
assert_eq!(rustdoc()
43+
.arg("compile-error.rs")
44+
.run_fail()
45+
.status
46+
.code().unwrap(), 1);
47+
48+
assert_eq!(rustdoc()
49+
.arg("lint-failure.rs")
50+
.run_fail()
51+
.status
52+
.code().unwrap(), 1);
53+
}

0 commit comments

Comments
 (0)