Skip to content

Add support for compiler tests #90

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

Merged
merged 3 commits into from
Nov 12, 2017
Merged
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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ script:
travis-cargo --only nightly build -- --features spin_no_std &&
travis-cargo --only nightly test -- --features spin_no_std &&
travis-cargo --only nightly bench -- --features spin_no_std &&
travis-cargo --only nightly clean &&
travis-cargo --only nightly build -- --features compiletest &&
travis-cargo --only nightly test -- --features compiletest &&
travis-cargo --only nightly clean &&
travis-cargo --only stable doc
after_success:
- travis-cargo --only stable doc-upload
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ categories = [ "no-std", "rust-patterns" ]
version = "0.4"
optional = true

[dependencies.compiletest_rs]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a dev-dependency instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid it can't at the moment - rust-lang/cargo#1596 and it needs to be optional, because, due to language features, it simply doesn't build on stable/beta

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really

version = "0.3"
optional = true

[features]
nightly = []
spin_no_std = ["nightly", "spin"]
compiletest = ["compiletest_rs"]

[badges]
appveyor = { repository = "rust-lang-nursery/lazy-static.rs" }
Expand Down
11 changes: 8 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ environment:
# (Based on from https://github.com/rust-lang/libc/blob/master/appveyor.yml)
install:
- appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
- rustup-init.exe -y --default-host %TARGET%
- rustup-init.exe -y --default-toolchain %CHANNEL% --default-host %TARGET%
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
- if "%TARGET%" == "i686-pc-windows-gnu" set PATH=%PATH%;C:\msys64\mingw32\bin
- if "%TARGET%" == "x86_64-pc-windows-gnu" set PATH=%PATH%;C:\msys64\mingw64\bin
Expand All @@ -50,5 +50,10 @@ install:
build: false

test_script:
- cargo build --verbose --target %TARGET%
- cargo test --target %TARGET%
- cargo build --verbose
- cargo test
- if [%CHANNEL%]==[nightly] (
cargo clean &&
cargo build --verbose --features "compiletest" &&
cargo test --features "compiletest"
)
22 changes: 22 additions & 0 deletions tests/compile-fail/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
This directory contains snippets of code that should yield a
warning/note/help/error at compilation. Syntax of annotations is described in
[rust documentation](https://github.com/rust-lang/rust/blob/master/src/test/COMPILER_TESTS.md).
For more information check out [`compiletest` crate](https://github.com/laumann/compiletest-rs).

To run compile tests issue `cargo +nightly --test --features compiletest`.

## Notes on working with `compiletest` crate

* Currently code that is inside macro should not be annotated, as `compiletest`
crate cannot deal with the fact that macro invocations effectively changes
line numbering. To prevent this add a `// error-pattern:<your error message here>`
on the top of the file and make sure that you set `deny` lint level
if you want to test compiler message different than error.
* `compiletest` crate by default sets `allow(dead_code)` lint level so make sure
that you change it to something suiting your needs even if the warning is
issued prior to any macro invocation.
* If you get a message `error: 0 unexpected errors found, 1 expected errors not found`
despite the fact that some error was bound to occur don't worry - it's a known
issue in the `compiletest` crate and your error was probably not registered -
make sure that your annotations are correct and that you are setting correct
lint levels.
10 changes: 10 additions & 0 deletions tests/compile-fail/static_never_used.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// error-pattern:static item is never used: `UNUSED`
#![deny(dead_code)]
#[macro_use]
extern crate lazy_static;

lazy_static! {
static ref UNUSED: () = ();
}

fn main() { }
18 changes: 18 additions & 0 deletions tests/compile_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg(feature="compiletest")]
extern crate compiletest_rs as compiletest;

fn run_mode(mode: &'static str) {
let mut config = compiletest::Config::default();
config.mode = mode.parse().expect("Invalid mode");
config.src_base = ["tests", mode].iter().collect();
config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_owned());

config.verbose = true;

compiletest::run_tests(&config);
}

#[test]
fn compile_test() {
run_mode("compile-fail");
}
4 changes: 0 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ lazy_static! {
static ref UNSAFE: u32 = unsafe {
std::mem::transmute::<i32, u32>(-1)
};

// This *should* triggger warn(dead_code) by design.
static ref UNUSED: () = ();

}

lazy_static! {
Expand Down