Skip to content

Commit

Permalink
Create better error output for command errors (fixes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jan 11, 2021
1 parent afa1c6a commit 222958b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ impl error::Error for LlvmVersionParseError {
/// The error type for this crate.
#[derive(Debug)]
pub enum Error {
/// An error occurred when executing the `rustc` command.
/// An error occurred while trying to find the `rustc` to run.
CouldNotExecuteCommand(io::Error),
/// Error output from the command that was run.
CommandError {
/// stdout output from the command
stdout: String,
/// stderr output from the command
stderr: String,
},
/// The output of `rustc -vV` was not valid utf-8.
Utf8Error(str::Utf8Error),
/// The output of `rustc -vV` was not in the expected format.
Expand All @@ -82,6 +89,14 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CouldNotExecuteCommand(ref e) => write!(f, "could not execute command: {}", e),
CommandError {
ref stdout,
ref stderr,
} => write!(
f,
"error from command -- stderr:\n\n{}\n\nstderr:\n\n{}",
stderr, stdout,
),
Utf8Error(_) => write!(f, "invalid UTF-8 output from `rustc -vV`"),
UnexpectedVersionFormat => write!(f, "unexpected `rustc -vV` format"),
ReqParseError(ref e) => write!(f, "error parsing version requirement: {}", e),
Expand All @@ -96,6 +111,7 @@ impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
CouldNotExecuteCommand(ref e) => Some(e),
CommandError { .. } => None,
Utf8Error(ref e) => Some(e),
UnexpectedVersionFormat => None,
ReqParseError(ref e) => Some(e),
Expand Down
25 changes: 20 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,20 @@ pub struct VersionMeta {

impl VersionMeta {
/// Returns the version metadata for `cmd`, which should be a `rustc` command.
pub fn for_command(cmd: Command) -> Result<VersionMeta> {
let mut cmd = cmd;

pub fn for_command(mut cmd: Command) -> Result<VersionMeta> {
let out = cmd
.arg("-vV")
.output()
.map_err(Error::CouldNotExecuteCommand)?;
let out = str::from_utf8(&out.stdout)?;

version_meta_for(out)
if !out.status.success() {
return Err(Error::CommandError {
stdout: String::from_utf8_lossy(&out.stdout).into(),
stderr: String::from_utf8_lossy(&out.stderr).into(),
});
}

version_meta_for(str::from_utf8(&out.stdout)?)
}
}

Expand Down Expand Up @@ -275,6 +279,17 @@ pub fn version_meta_for(verbose_version_string: &str) -> Result<VersionMeta> {
})
}

#[test]
fn rustc_error() {
let mut cmd = Command::new("rustc");
cmd.arg("--FOO");
let stderr = match VersionMeta::for_command(cmd) {
Err(Error::CommandError { stdout: _, stderr }) => stderr,
_ => panic!("command error expected"),
};
assert_eq!(stderr, "error: Unrecognized option: \'FOO\'\n\n");
}

#[test]
fn smoketest() {
let v = version().unwrap();
Expand Down

0 comments on commit 222958b

Please sign in to comment.