Skip to content

Commit

Permalink
move exit-code to rmake
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed Mar 2, 2024
1 parent 2dceda4 commit 651d799
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::env;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

mod rustdoc;
pub use rustdoc::rustdoc;

fn setup_common_build_cmd() -> Command {
let rustc = env::var("RUSTC").unwrap();
let mut cmd = Command::new(rustc);
Expand Down Expand Up @@ -45,6 +48,11 @@ impl RustcInvocationBuilder {
self
}

pub fn env(&mut self, key: &str, value: &str) -> &mut RustcInvocationBuilder {
self.cmd.env(key, value);
self
}

#[track_caller]
pub fn run(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
Expand All @@ -56,6 +64,18 @@ impl RustcInvocationBuilder {
}
output
}

#[track_caller]
pub fn run_fail(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -149,3 +169,7 @@ pub fn run_fail(bin_name: &str) -> Output {
}
output
}

pub fn tempdir() -> PathBuf {
PathBuf::from(env::var("TMPDIR").unwrap())
}
59 changes: 59 additions & 0 deletions src/tools/run-make-support/src/rustdoc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::path::Path;
use std::process::{Command, Output};

use crate::{handle_failed_output, setup_common_build_cmd};

pub fn rustdoc() -> RustdocInvocationBuilder {
RustdocInvocationBuilder::new()
}

#[derive(Debug)]
pub struct RustdocInvocationBuilder {
cmd: Command,
}

impl RustdocInvocationBuilder {
fn new() -> Self {
let cmd = setup_common_build_cmd();
Self { cmd }
}

pub fn arg(&mut self, arg: &str) -> &mut RustdocInvocationBuilder {
self.cmd.arg(arg);
self
}

pub fn arg_file(&mut self, arg: &Path) -> &mut RustdocInvocationBuilder {
self.cmd.arg(arg);
self
}

pub fn env(&mut self, key: &str, value: &str) -> &mut RustdocInvocationBuilder {
self.cmd.env(key, value);
self
}

#[track_caller]
pub fn run(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if !output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}

#[track_caller]
pub fn run_fail(&mut self) -> Output {
let caller_location = std::panic::Location::caller();
let caller_line_number = caller_location.line();

let output = self.cmd.output().unwrap();
if output.status.success() {
handle_failed_output(&format!("{:#?}", self.cmd), output, caller_line_number);
}
output
}
}
12 changes: 0 additions & 12 deletions tests/run-make/exit-code/Makefile

This file was deleted.

53 changes: 53 additions & 0 deletions tests/run-make/exit-code/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
extern crate run_make_support;

use run_make_support::{rustc, rustdoc, tempdir};

fn main() {
rustc()
.arg("success.rs")
.run();

assert_eq!(rustc()
.arg("--invalid-arg-foo")
.run_fail()
.status
.code().unwrap(), 1);

assert_eq!(rustc()
.arg("compile-error.rs")
.run_fail()
.status
.code().unwrap(), 1);

assert_eq!(rustc()
.env("RUSTC_ICE", "0")
.arg("-Ztreat-err-as-bug")
.arg("compile-error.rs")
.run_fail()
.status
.code().unwrap(), 101);

rustdoc()
.arg("-o")
.arg_file(&tempdir().join("exit-code"))
.arg("success.rs")
.run();

assert_eq!(rustdoc()
.arg("--invalid-arg-foo")
.run_fail()
.status
.code().unwrap(), 1);

assert_eq!(rustdoc()
.arg("compile-error.rs")
.run_fail()
.status
.code().unwrap(), 1);

assert_eq!(rustdoc()
.arg("lint-failure.rs")
.run_fail()
.status
.code().unwrap(), 1);
}

0 comments on commit 651d799

Please sign in to comment.