Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TestCases::full_build() to force full builds. #294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ fn test() {
t.compile_fail("tests/ui/compile-fail-2.rs");
t.compile_fail("tests/ui/compile-fail-3.rs");
}

#[test]
fn test_full_build() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/compile-fail-const-assert.rs");
t.full_build(true);
}
3 changes: 3 additions & 0 deletions tests/ui/compile-fail-const-assert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
const { assert!(false) };
}
13 changes: 13 additions & 0 deletions tests/ui/compile-fail-const-assert.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0080]: evaluation of `main::{constant#0}` failed
--> tests/ui/compile-fail-const-assert.rs:2:13
|
2 | const { assert!(false) };
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: false', $DIR/tests/ui/compile-fail-const-assert.rs:2:13
|
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
--> tests/ui/compile-fail-const-assert.rs:2:5
|
2 | const { assert!(false) };
| ^^^^^^^^^^^^^^^^^^^^^^^^