Skip to content

Commit

Permalink
Use trybuild instead of compiletest
Browse files Browse the repository at this point in the history
The compiletest crate requires lots of flaming hoops, doesn't work if
you change features between cargo runs, and currently doesn't work in
macOS at all (see Manishearth/compiletest-rs#179).

The trybuild crate, on the other hand, seems to work quite well
everywhere and gives nice diagnostics. Let's use that instead.
  • Loading branch information
antifuchs committed Dec 14, 2019
1 parent 979207e commit 60f40b5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 67 deletions.
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@ cache = "false"
[features]
default = ["std"]
std = []
stable = ["compiletest_rs/stable"]
beta = ["compiletest_rs/stable"]
nightly = ["compiletest_rs"]

[package.metadata.template_ci.clippy]
allow_failure = false

[dependencies]

[dev-dependencies]
trybuild = "1.0"

[dependencies.compiletest_rs]
version = "0.4.0"
optional = true
features = ["tmp"]
3 changes: 1 addition & 2 deletions tests/compile-fail/negative_i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ use std::num::NonZeroU32;

#[cfg_attr(rustfmt, rustfmt_skip)]
fn main() {
let _a: NonZeroU32 = nonzero!(-2i32); //~ ERROR no method named
// ^ should not be able to convert
let _a: NonZeroU32 = nonzero!(-2i32);
}
7 changes: 7 additions & 0 deletions tests/compile-fail/negative_i32.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error[E0599]: no method named `as_nonzero_unchecked` found for type `i32` in the current scope
--> $DIR/negative_i32.rs:8:26
|
8 | let _a: NonZeroU32 = nonzero!(-2i32);
| ^^^^^^^^^^^^^^^ method not found in `i32`
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
3 changes: 1 addition & 2 deletions tests/compile-fail/zero_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ use std::num::NonZeroU8;

#[cfg_attr(rustfmt, rustfmt_skip)]
fn main() {
let _a: NonZeroU8 = nonzero!(0u8); //~ ERROR evaluation of constant value failed [E0080]
// ^ should complain about the zero-ness of the argument
let _a: NonZeroU8 = nonzero!(0u8);
}
7 changes: 7 additions & 0 deletions tests/compile-fail/zero_u8.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error[E0080]: evaluation of constant value failed
--> $DIR/zero_u8.rs:8:25
|
8 | let _a: NonZeroU8 = nonzero!(0u8);
| ^^^^^^^^^^^^^ attempt to subtract with overflow
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
3 changes: 1 addition & 2 deletions tests/compile-fail/zero_usize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ use std::num::NonZeroUsize;

#[cfg_attr(rustfmt, rustfmt_skip)]
fn main() {
let _a: NonZeroUsize = nonzero!(0usize); //~ ERROR evaluation of constant value failed [E0080]
// ^ should complain about the zero-ness of the argument
let _a: NonZeroUsize = nonzero!(0usize);
}
7 changes: 7 additions & 0 deletions tests/compile-fail/zero_usize.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error[E0080]: evaluation of constant value failed
--> $DIR/zero_usize.rs:8:28
|
8 | let _a: NonZeroUsize = nonzero!(0usize);
| ^^^^^^^^^^^^^^^^ attempt to subtract with overflow
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
57 changes: 3 additions & 54 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
#![cfg(feature = "compiletest_rs")]

extern crate compiletest_rs as compiletest;
use std::fs::{read_dir, remove_file};
use std::path::PathBuf;

fn clean_rlibs(config: &compiletest_rs::Config) {
if config.target_rustcflags.is_some() {
for directory in config
.target_rustcflags
.as_ref()
.unwrap()
.split_whitespace()
{
if let Ok(mut entries) = read_dir(directory) {
while let Some(Ok(entry)) = entries.next() {
let f = entry.file_name().clone().into_string().unwrap();
if f.ends_with(".rmeta") {
let prefix = &f[..f.len() - 5];
let _ = remove_file(entry.path());
if let Ok(mut entries) = read_dir(directory) {
while let Some(Ok(entry)) = entries.next() {
let f = entry.file_name().clone().into_string().unwrap();
if f.starts_with(prefix) && !f.ends_with(".rmeta") {
let _ = remove_file(entry.path());
}
}
}
}
}
}
}
}
}

fn run_mode(mode: &'static str) {
let mut config = compiletest::Config::default().tempdir();
let cfg_mode = mode.parse().expect("Invalid mode");

config.mode = cfg_mode;
config.src_base = PathBuf::from(format!("tests/{}", mode));
// config.target_rustcflags = Some("-L target/debug -L target/debug/deps".to_string());
config.link_deps();
config.clean_rmeta();
clean_rlibs(&config);

compiletest::run_tests(&config);
}

#[test]
fn compile_test() {
// Don't run compile-fail tests under nightly: Error messages are unstable.
#[cfg(feature = "stable")]
run_mode("compile-fail");

run_mode("run-pass");
let t = trybuild::TestCases::new();
t.compile_fail("tests/compile-fail/*.rs");
t.pass("tests/run-pass/*.rs");
}

0 comments on commit 60f40b5

Please sign in to comment.