Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Oct 13, 2024
1 parent bad405d commit a8d4fbe
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 29 deletions.
14 changes: 11 additions & 3 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ pub enum CommandError {
Timeout(u64),

/// The command failed to execute.
#[error("command failed: {0}")]
ExecutionFailed(ExitStatus),
#[error("command failed: {status}\n\n{stderr}")]
ExecutionFailed {
/// the exit status we got from the command
status: ExitStatus,
/// the stderr output, if it was captured
stderr: String,
},

/// Killing the underlying process after the timeout failed.
#[error("{0}")]
Expand Down Expand Up @@ -496,7 +501,10 @@ impl<'w, 'pl> Command<'w, 'pl> {
if out.status.success() {
Ok(out.into())
} else {
Err(CommandError::ExecutionFailed(out.status))
Err(CommandError::ExecutionFailed {
status: out.status,
stderr: out.stderr.join("\n"),
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl Container<'_> {
// Return a different error if the container was killed due to an OOM
if details.state.oom_killed {
Err(match res {
Ok(_) | Err(CommandError::ExecutionFailed(_)) => CommandError::SandboxOOM,
Ok(_) | Err(CommandError::ExecutionFailed { .. }) => CommandError::SandboxOOM,
Err(err) => err,
})
} else {
Expand Down
36 changes: 21 additions & 15 deletions src/prepare.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmd::Command;
use crate::cmd::{Command, CommandError};
use crate::{build::CratePatch, Crate, Toolchain, Workspace};
use anyhow::Context as _;
use log::info;
Expand Down Expand Up @@ -124,17 +124,19 @@ impl<'a> Prepare<'a> {
missing_deps = true;
}
})
.run();
.run_capture();

match res {
Err(_) if yanked_deps => {
return Err(PrepareError::YankedDependencies.into());
Err(CommandError::ExecutionFailed { status: _, stderr }) if yanked_deps => {
return Err(PrepareError::YankedDependencies(stderr).into())
}
Err(_) if missing_deps => {
return Err(PrepareError::MissingDependencies.into());
Err(CommandError::ExecutionFailed { status: _, stderr }) if missing_deps => {
return Err(PrepareError::MissingDependencies(stderr).into())
}
other => other?,
other => other,
}
Ok(())
.map(|_| ())
.map_err(Into::into)
}

fn fetch_deps(&mut self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -167,11 +169,15 @@ pub(crate) fn fetch_deps(
missing_deps = true;
}
})
.run();
.run_capture()
.map(|_| ());

match res {
Ok(_) => Ok(()),
Err(_) if missing_deps => Err(PrepareError::MissingDependencies.into()),
err => err.map_err(|e| e.into()),
Err(CommandError::ExecutionFailed { status: _, stderr }) if missing_deps => {
Err(PrepareError::MissingDependencies(stderr).into())
}
err => err.map_err(Into::into),
}
}

Expand Down Expand Up @@ -381,11 +387,11 @@ pub enum PrepareError {
#[error("invalid Cargo.toml syntax")]
InvalidCargoTomlSyntax,
/// Some of this crate's dependencies were yanked, preventing Crater from fetching them.
#[error("the crate depends on yanked dependencies")]
YankedDependencies,
#[error("the crate depends on yanked dependencies: \n\n{0}")]
YankedDependencies(String),
/// Some of the dependencies do not exist anymore.
#[error("the crate depends on missing dependencies")]
MissingDependencies,
#[error("the crate depends on missing dependencies: \n\n{0}")]
MissingDependencies(String),
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions tests/buildtest/crates/unstable-feature/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[unstable]
bindeps = true
7 changes: 7 additions & 0 deletions tests/buildtest/crates/unstable-feature/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/buildtest/crates/unstable-feature/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "unstable-feature"
version = "0.0.1"
edition = "2021"
description = ""

[lib]
doctest = false

[dependencies]

[package.metadata.docs.rs]
cargo-args = ["-Z", "build-std", "-Z", "bindeps"]
4 changes: 4 additions & 0 deletions tests/buildtest/crates/unstable-feature/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("Hello, world!");
println!("Hello, world again!");
}
24 changes: 16 additions & 8 deletions tests/buildtest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::LevelFilter;
use rustwide::cmd::{ProcessLinesActions, SandboxBuilder};
use rustwide::cmd::{Command, ProcessLinesActions, SandboxBuilder};

#[macro_use]
mod runner;
Expand Down Expand Up @@ -188,22 +188,30 @@ test_prepare_error!(
InvalidCargoTomlSyntax
);

test_prepare_error!(test_yanked_deps, "yanked-deps", YankedDependencies);
test_prepare_error_stderr!(
test_yanked_deps,
"yanked-deps",
YankedDependencies,
r#"failed to select a version for the requirement `ring = "^0.2"`"#
);

test_prepare_error!(
test_prepare_error_stderr!(
test_missing_deps_git,
"missing-deps-git",
MissingDependencies
MissingDependencies,
"failed to get `not-a-git-repo` as a dependency of package `missing-deps v0.1.0"
);

test_prepare_error!(
test_prepare_error_stderr!(
test_missing_deps_git_locked,
"missing-deps-git-locked",
MissingDependencies
MissingDependencies,
"failed to get `not-a-git-repo` as a dependency of package `missing-deps-git-locked v0.1.0"
);

test_prepare_error!(
test_prepare_error_stderr!(
test_missing_deps_registry,
"missing-deps-registry",
MissingDependencies
MissingDependencies,
"error: no matching package named `macro` found"
);
26 changes: 24 additions & 2 deletions tests/buildtest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub(crate) fn run(crate_name: &str, f: impl FnOnce(&mut Runner) -> anyhow::Resul

pub(crate) struct Runner {
crate_name: String,
workspace: Workspace,
toolchain: Toolchain,
pub(crate) workspace: Workspace,
pub(crate) toolchain: Toolchain,
krate: Crate,
}

Expand Down Expand Up @@ -84,3 +84,25 @@ macro_rules! test_prepare_error {
}
};
}

macro_rules! test_prepare_error_stderr {
($name:ident, $krate:expr, $expected:ident, $expected_output:expr) => {
#[test]
fn $name() {
runner::run($krate, |run| {
let res = run.run(
rustwide::cmd::SandboxBuilder::new().enable_networking(false),
|_| Ok(()),
);
if let Some(rustwide::PrepareError::$expected(output)) =
res.err().and_then(|err| err.downcast().ok())
{
assert!(output.contains($expected_output), "output: {:?}", output);
} else {
panic!("didn't get the error {}", stringify!($expected));
}
Ok(())
});
}
};
}

0 comments on commit a8d4fbe

Please sign in to comment.