Skip to content

Commit

Permalink
Check that the tests do not emit warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Feb 18, 2019
1 parent da35cf6 commit af6d9a3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "ctest"
version = "0.2.8"
version = "0.2.9"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand All @@ -14,6 +14,7 @@ Automated tests of FFI bindings.
[dependencies]
syntex_syntax = "0.59.1"
cc = "1.0.1"
rustc_version = "0.2"

[workspace]
members = ["testcrate"]
58 changes: 54 additions & 4 deletions src/lib.rs
Expand Up @@ -14,6 +14,8 @@
extern crate cc;
extern crate syntex_syntax as syntax;

extern crate rustc_version;

use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::env;
Expand Down Expand Up @@ -86,6 +88,7 @@ pub struct TestGenerator {
type_name: Box<Fn(&str, bool, bool) -> String>,
fn_cname: Box<Fn(&str, Option<&str>) -> String>,
const_cname: Box<Fn(&str) -> String>,
rust_version: rustc_version::Version,
}

struct TyFinder {
Expand Down Expand Up @@ -145,6 +148,7 @@ impl TestGenerator {
}
}),
const_cname: Box::new(|a| a.to_string()),
rust_version: rustc_version::version().unwrap(),
}
}

Expand All @@ -171,6 +175,21 @@ impl TestGenerator {
self
}

/// Target Rust version: `major`.`minor`.`patch`
///
/// # Examples
///
/// ```no_run
/// use ctest::TestGenerator;
///
/// let mut cfg = TestGenerator::new();
/// cfg.rust_version(1, 0, 1);
/// ```
pub fn rust_version(&mut self, major: u64, minor: u64, patch: u64) -> &mut Self {
self.rust_version = rustc_version::Version::new(major, minor, patch);
self
}

/// Add a path to the C compiler header lookup path.
///
/// This is useful for if the C library is installed to a nonstandard
Expand Down Expand Up @@ -820,11 +839,24 @@ impl TestGenerator {
t!(writeln!(gen.c, "#include <{}>", header));
}

eprintln!("rust version: {}", self.rust_version);
t!(gen.rust.write_all(
if self.rust_version < rustc_version::Version::new(1, 30, 0) {
br#"
static FAILED: AtomicBool = std::sync::atomic::ATOMIC_BOOL_INIT;
static NTESTS: AtomicUsize = std::sync::atomic::ATOMIC_USIZE_INIT;
"#
} else {
br#"
static FAILED: AtomicBool = AtomicBool::new(false);
static NTESTS: AtomicUsize = AtomicUsize::new(0);
"#
}));

t!(gen.rust.write_all(
br#"
use std::any::{Any, TypeId};
use std::mem;
use std::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
fn main() {
Expand Down Expand Up @@ -861,9 +893,6 @@ impl TestGenerator {
}
p! { i8 i16 i32 i64 u8 u16 u32 u64 usize isize }
static FAILED: AtomicBool = ATOMIC_BOOL_INIT;
static NTESTS: AtomicUsize = ATOMIC_USIZE_INIT;
fn same<T: Eq + Pretty>(rust: T, c: T, attr: &str) {
if rust != c {
println!("bad {}: rust: {} != c {}", attr, rust.pretty(),
Expand Down Expand Up @@ -1070,6 +1099,7 @@ impl<'a> Generator<'a> {
t!(writeln!(
self.rust,
r#"
#[allow(non_snake_case)]
#[inline(never)]
fn field_offset_size_{ty}() {{
"#,
Expand Down Expand Up @@ -1114,7 +1144,9 @@ impl<'a> Generator<'a> {
self.rust,
r#"
extern {{
#[allow(non_snake_case)]
fn __test_offset_{ty}_{field}() -> u64;
#[allow(non_snake_case)]
fn __test_fsize_{ty}_{field}() -> u64;
}}
unsafe {{
Expand Down Expand Up @@ -1152,6 +1184,7 @@ impl<'a> Generator<'a> {
self.rust,
r#"
extern {{
#[allow(non_snake_case)]
fn __test_field_type_{ty}_{field}(a: *mut {ty})
-> *mut u8;
}}
Expand Down Expand Up @@ -1194,10 +1227,13 @@ impl<'a> Generator<'a> {
t!(writeln!(
self.rust,
r#"
#[allow(non_snake_case)]
#[inline(never)]
fn size_align_{ty}() {{
extern {{
#[allow(non_snake_case)]
fn __test_size_{ty}() -> u64;
#[allow(non_snake_case)]
fn __test_align_{ty}() -> u64;
}}
unsafe {{
Expand Down Expand Up @@ -1253,8 +1289,10 @@ impl<'a> Generator<'a> {
self.rust,
r#"
#[inline(never)]
#[allow(non_snake_case)]
fn sign_{ty}() {{
extern {{
#[allow(non_snake_case)]
fn __test_signed_{ty}() -> u32;
}}
unsafe {{
Expand Down Expand Up @@ -1313,8 +1351,10 @@ impl<'a> Generator<'a> {
self.rust,
r#"
#[inline(never)]
#[allow(non_snake_case)]
fn const_{name}() {{
extern {{
#[allow(non_snake_case)]
fn __test_const_{name}() -> *const *const u8;
}}
let val = {name};
Expand All @@ -1332,8 +1372,10 @@ impl<'a> Generator<'a> {
t!(writeln!(
self.rust,
r#"
#[allow(non_snake_case)]
fn const_{name}() {{
extern {{
#[allow(non_snake_case)]
fn __test_const_{name}() -> *const {ty};
}}
let val = {name};
Expand Down Expand Up @@ -1395,9 +1437,11 @@ impl<'a> Generator<'a> {
t!(writeln!(
self.rust,
r#"
#[allow(non_snake_case)]
#[inline(never)]
fn fn_{name}() {{
extern {{
#[allow(non_snake_case)]
fn __test_fn_{name}() -> *mut u32;
}}
unsafe {{
Expand Down Expand Up @@ -1445,8 +1489,10 @@ impl<'a> Generator<'a> {
self.rust,
r#"
#[inline(never)]
#[allow(non_snake_case)]
fn static_{name}() {{
extern {{
#[allow(non_snake_case)]
fn __test_static_{name}() -> {ty};
}}
unsafe {{
Expand Down Expand Up @@ -1487,8 +1533,10 @@ impl<'a> Generator<'a> {
self.rust,
r#"
#[inline(never)]
#[allow(non_snake_case)]
fn static_{name}() {{
extern {{
#[allow(non_snake_case)]
fn __test_static_{name}() -> *{mutbl} {ty};
}}
unsafe {{
Expand Down Expand Up @@ -1522,9 +1570,11 @@ impl<'a> Generator<'a> {
t!(writeln!(
self.rust,
r#"
#[allow(non_snake_case)]
#[inline(never)]
fn static_{name}() {{
extern {{
#[allow(non_snake_case)]
fn __test_static_{name}() -> *{mutbl} {ty};
}}
unsafe {{
Expand Down
2 changes: 1 addition & 1 deletion testcrate/src/bin/t1.rs
@@ -1,5 +1,5 @@
#![cfg(not(test))]
#![allow(bad_style)]
#![deny(warnings)]

extern crate libc;
extern crate testcrate;
Expand Down
2 changes: 1 addition & 1 deletion testcrate/src/bin/t1_cxx.rs
@@ -1,5 +1,5 @@
#![cfg(not(test))]
#![allow(bad_style)]
#![deny(warnings)]

extern crate libc;
extern crate testcrate;
Expand Down
2 changes: 1 addition & 1 deletion testcrate/src/bin/t2.rs
@@ -1,5 +1,5 @@
#![cfg(not(test))]
#![allow(bad_style)]
#![deny(warnings)]

extern crate testcrate;
use testcrate::t2::*;
Expand Down
2 changes: 1 addition & 1 deletion testcrate/src/bin/t2_cxx.rs
@@ -1,5 +1,5 @@
#![cfg(not(test))]
#![allow(bad_style)]
#![deny(warnings)]

extern crate testcrate;
use testcrate::t2::*;
Expand Down

0 comments on commit af6d9a3

Please sign in to comment.