From 2bb3e9ba662e122047c0333bdede876e28100859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 30 Mar 2019 16:47:21 +0100 Subject: [PATCH] Use cargo test instead of test.py. This makes cargo test test all the stuff in tests/rust, and thus makes tests.py unnecessary. Co-authored-by: Axel Nennker --- .travis.yml | 3 +- build.rs | 58 ++++++++ src/bindgen/config.rs | 4 +- test.py | 133 ------------------ tests/expectations/both/docstyle_doxylight.c | 9 -- tests/expectations/docstyle_doxylight.c | 9 -- tests/expectations/docstyle_doxylight.cpp | 12 -- tests/expectations/tag/docstyle_doxylight.c | 9 -- tests/tests.rs | 135 +++++++++++++++++++ 9 files changed, 196 insertions(+), 176 deletions(-) create mode 100644 build.rs delete mode 100755 test.py delete mode 100644 tests/expectations/both/docstyle_doxylight.c delete mode 100644 tests/expectations/docstyle_doxylight.c delete mode 100644 tests/expectations/docstyle_doxylight.cpp delete mode 100644 tests/expectations/tag/docstyle_doxylight.c create mode 100644 tests/tests.rs diff --git a/.travis.yml b/.travis.yml index 3c49d41..a863c01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,5 +16,4 @@ script: - export CXX=g++-7 - cargo fmt --all -- --check - cargo build --verbose - - cargo test --verbose - - python test.py -v + - CBINDGEN_TEST_VERIFY=1 cargo test --verbose diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..768db4d --- /dev/null +++ b/build.rs @@ -0,0 +1,58 @@ +fn generate_tests() { + use std::env; + use std::ffi::OsStr; + use std::fs::{self, File}; + use std::io::Write; + use std::path::{Path, PathBuf}; + + let profile = env::var("PROFILE").unwrap(); + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let mut dst = File::create(Path::new(&out_dir).join("tests.rs")).unwrap(); + + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let tests_dir = manifest_dir.join("tests").join("rust"); + let tests = fs::read_dir(&tests_dir).unwrap(); + + let entries = tests.map(|t| t.expect("Couldn't read test file")); + + println!("cargo:rerun-if-changed={}", tests_dir.display()); + + for entry in entries { + let path_segment = if entry.file_type().unwrap().is_file() { + match entry.path().extension().and_then(OsStr::to_str) { + Some("rs") => {} + _ => continue, + }; + + entry + .path() + .file_stem() + .unwrap() + .to_str() + .unwrap() + .to_owned() + } else { + entry.file_name().to_str().unwrap().to_owned() + }; + + let identifier = path_segment + .replace(|c| !char::is_alphanumeric(c), "_") + .replace("__", "_"); + + writeln!( + dst, + "test_file!({}, test_{}, {:?}, {:?});", + profile, + identifier, + path_segment, + entry.path(), + ) + .unwrap(); + } + + dst.flush().unwrap(); +} + +fn main() { + generate_tests(); +} diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index d227a31..3d7f621 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -23,7 +23,7 @@ pub use bindgen::rename::RenameRule; pub const VERSION: &'static str = env!("CARGO_PKG_VERSION"); /// A language type to generate bindings for. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub enum Language { Cxx, C, @@ -129,7 +129,7 @@ impl FromStr for DocumentationStyle { deserialize_enum_str!(DocumentationStyle); /// A style of Style to use when generating structs and enums. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub enum Style { Both, Tag, diff --git a/test.py b/test.py deleted file mode 100755 index 33587bc..0000000 --- a/test.py +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/env python - -import os -import glob -import subprocess -import sys -import filecmp - -def build_cbindgen(): - try: - subprocess.check_output(["cargo", "build"]) - return True - except subprocess.CalledProcessError: - return False - -def cbindgen(path, out, c, style, verify): - bin = ["target/debug/cbindgen"] - compile = [path, "-o", out] - flags = [] - - if c: - flags += ["--lang", "c"] - - if style: - flags += ["--style", style] - - if verify: - flags += ["--verify"] - - config = path.replace(".rs", ".toml") - if not os.path.isdir(path) and os.path.exists(config): - flags += ["--config", config] - - command = bin + flags + compile - print(command) - subprocess.check_output(bin + flags + compile) - -def gcc(src): - gcc_bin = os.environ.get('CC') - if gcc_bin == None: - gcc_bin = 'gcc' - - subprocess.check_output([gcc_bin, "-D", "DEFINED", "-c", src, "-o", "tests/expectations/tmp.o"]) - os.remove("tests/expectations/tmp.o") - -def gxx(src): - gxx_bin = os.environ.get('CXX') - if gxx_bin == None: - gxx_bin = 'g++' - - subprocess.check_output([gxx_bin, "-D", "DEFINED", "-std=c++17", "-c", src, "-o", "tests/expectations/tmp.o"]) - os.remove("tests/expectations/tmp.o") - -def run_compile_test(rust_src, verify, c, style=""): - is_crate = os.path.isdir(rust_src) - - test_name = rust_src - if is_crate: - test_name = os.path.basename(rust_src[0:-1]) - else: - test_name = os.path.splitext(os.path.basename(rust_src))[0] - - expectation = True - if test_name.startswith("fail-"): - expectation = False - - if c: - subdir = style if style != "type" else "" - out = os.path.join('tests/expectations/', subdir, test_name + ".c") - else: - out = os.path.join('tests/expectations/', test_name + ".cpp") - - try: - cbindgen(rust_src, out, c, style, verify) - except subprocess.CalledProcessError: - return False; - - try: - if c: - gcc(out) - else: - gxx(out) - except subprocess.CalledProcessError: - return expectation == False - - return expectation == True - -if not build_cbindgen(): - exit() - -args = sys.argv[1:] -files = [x for x in args if not x.startswith("-")] -flags = [x for x in args if x.startswith("-")] - -verify = False - -for flag in flags: - if flag == "-v": - verify = True - -tests = [] -if len(files) == 0: - tests = glob.glob("tests/rust/*.rs") + glob.glob("tests/rust/*/") -else: - tests = files - -num_pass = 0 -num_fail = 0 - -# C - -for test in tests: - for style in ["type", "tag", "both"]: - if run_compile_test(test, verify, True, style): - num_pass += 1 - print("Pass - %s" % test) - else: - num_fail += 1 - print("Fail - %s" % test) - -# C++ - -for test in tests: - if run_compile_test(test, verify, False): - num_pass += 1 - print("Pass - %s" % test) - else: - num_fail += 1 - print("Fail - %s" % test) - -print("Tests complete. %i passed, %i failed." % (num_pass, num_fail)) -if num_fail > 0: - sys.exit(1) diff --git a/tests/expectations/both/docstyle_doxylight.c b/tests/expectations/both/docstyle_doxylight.c deleted file mode 100644 index 86bcd0c..0000000 --- a/tests/expectations/both/docstyle_doxylight.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include -#include - -/** - * The root of all evil. - */ -void root(void); diff --git a/tests/expectations/docstyle_doxylight.c b/tests/expectations/docstyle_doxylight.c deleted file mode 100644 index 86bcd0c..0000000 --- a/tests/expectations/docstyle_doxylight.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include -#include - -/** - * The root of all evil. - */ -void root(void); diff --git a/tests/expectations/docstyle_doxylight.cpp b/tests/expectations/docstyle_doxylight.cpp deleted file mode 100644 index 48127b3..0000000 --- a/tests/expectations/docstyle_doxylight.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include - -extern "C" { - -/** - * The root of all evil. - */ -void root(); - -} // extern "C" diff --git a/tests/expectations/tag/docstyle_doxylight.c b/tests/expectations/tag/docstyle_doxylight.c deleted file mode 100644 index 86bcd0c..0000000 --- a/tests/expectations/tag/docstyle_doxylight.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include -#include - -/** - * The root of all evil. - */ -void root(void); diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..dfea81f --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,135 @@ +extern crate cbindgen; + +use cbindgen::*; +use std::path::Path; +use std::process::Command; +use std::{env, fs}; + +fn run_cbindgen( + profile: &'static str, + path: &Path, + output: &Path, + language: Language, + style: Option