Skip to content

Commit

Permalink
Add TestCases::full_build() to force full builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Oct 23, 2024
1 parent 32408f9 commit 2179985
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub(crate) fn build_dependencies(project: &mut Project) -> Result<()> {

let mut command = cargo(project);
command
.arg(if project.has_pass { "build" } else { "check" })
.arg(if project.full_build { "build" } else { "check" })
.args(target())
.arg("--bin")
.arg(&project.name)
Expand Down Expand Up @@ -114,7 +114,7 @@ pub(crate) fn build_test(project: &Project, name: &Name) -> Result<Output> {
.status();

cargo(project)
.arg(if project.has_pass { "build" } else { "check" })
.arg(if project.full_build { "build" } else { "check" })
.args(target())
.arg("--bin")
.arg(name)
Expand All @@ -137,7 +137,7 @@ pub(crate) fn build_all_tests(project: &Project) -> Result<Output> {
.status();

cargo(project)
.arg(if project.has_pass { "build" } else { "check" })
.arg(if project.full_build { "build" } else { "check" })
.args(target())
.arg("--bins")
.args(features(project))
Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub struct TestCases {
#[derive(Debug)]
struct Runner {
tests: Vec<Test>,
full_build: bool,
}

#[derive(Clone, Debug)]
Expand All @@ -312,23 +313,44 @@ impl TestCases {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
TestCases {
runner: RefCell::new(Runner { tests: Vec::new() }),
runner: RefCell::new(Runner {
tests: Vec::new(),
full_build: false,
}),
}
}

/// Add one or more source files containing pass tests.
///
/// You can use a [`glob`][glob::glob] patterns add multiple source files at once.
///
/// Each source file must have a `main` function, and the test is considered to suceed if the `main` function does not panic.
pub fn pass<P: AsRef<Path>>(&self, path: P) {
self.runner.borrow_mut().tests.push(Test {
path: path.as_ref().to_owned(),
expected: Expected::Pass,
});
}

/// Add one or more source files containing compile-fail tests.
///
/// You can use a [`glob`][glob::glob] patterns add multiple source files at once.
pub fn compile_fail<P: AsRef<Path>>(&self, path: P) {
self.runner.borrow_mut().tests.push(Test {
path: path.as_ref().to_owned(),
expected: Expected::CompileFail,
});
}

/// Enable or disable full builds of the test cases.
///
/// To save time, `trybuild` runs `cargo check` instead of `cargo build` if there are no [`pass()`][Self::pass] tests.
/// However, some build failures (like assertions in const context) do not currently show up with `cargo check`.
///
/// You can force `trybuild` to run a full build as a work-around.
pub fn full_build(&self, full: bool) {
self.runner.borrow_mut().full_build = full;
}
}

impl RefUnwindSafe for TestCases {}
Expand Down
4 changes: 3 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ pub(crate) struct Project {
pub target_dir: Directory,
pub name: String,
update: Update,
pub has_pass: bool,
has_pass: bool,
has_compile_fail: bool,
pub full_build: bool,
pub features: Option<Vec<String>>,
pub workspace: Directory,
pub path_dependencies: Vec<PathDependency>,
Expand Down Expand Up @@ -172,6 +173,7 @@ impl Runner {
update: Update::env()?,
has_pass,
has_compile_fail,
full_build: self.full_build || has_pass,
features,
workspace,
path_dependencies,
Expand Down

0 comments on commit 2179985

Please sign in to comment.