Feature-gate test-programs

This commit feature-gates `test-programs` as a workaround for
`cargo test` not allowing for optional dev-dependencies.

This commit addresses #595.
This commit is contained in:
Jakub Konka
2019-11-19 13:00:55 +01:00
committed by Jakub Konka
parent 3cb238366d
commit a58709d99e
2 changed files with 172 additions and 162 deletions

View File

@@ -3,17 +3,20 @@
//! By generating a separate `#[test]` test for each file, we allow cargo test
//! to automatically run the files in parallel.
use std::env;
use std::fs::{read_dir, DirEntry, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
fn main() {
build_and_generate_tests()
#[cfg(features = "test-programs")]
wasi_tests::build_and_generate_tests()
}
fn build_and_generate_tests() {
#[cfg(features = "test-programs")]
mod wasi_tests {
use std::env;
use std::fs::{read_dir, DirEntry, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
fn build_and_generate_tests() {
// Validate if any of test sources are present and if they changed
// This should always work since there is no submodule to init anymore
let bin_tests = std::fs::read_dir("wasi-tests/src/bin").unwrap();
@@ -28,15 +31,16 @@ fn build_and_generate_tests() {
}
}
// Build tests to OUT_DIR (target/*/build/wasi-common-*/out/wasm32-wasi/release/*.wasm)
let out_dir =
PathBuf::from(env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set"));
let out_dir = PathBuf::from(
env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
);
let mut out =
File::create(out_dir.join("wasi_tests.rs")).expect("error generating test source file");
build_tests("wasi-tests", &out_dir).expect("building tests");
test_directory(&mut out, "wasi-tests", &out_dir).expect("generating tests");
}
}
fn build_tests(testsuite: &str, out_dir: &Path) -> io::Result<()> {
fn build_tests(testsuite: &str, out_dir: &Path) -> io::Result<()> {
let mut cmd = Command::new("cargo");
cmd.args(&[
"build",
@@ -59,9 +63,9 @@ fn build_tests(testsuite: &str, out_dir: &Path) -> io::Result<()> {
}
Ok(())
}
}
fn test_directory(out: &mut File, testsuite: &str, out_dir: &Path) -> io::Result<()> {
fn test_directory(out: &mut File, testsuite: &str, out_dir: &Path) -> io::Result<()> {
let mut dir_entries: Vec<_> = read_dir(out_dir.join("wasm32-wasi/release"))
.expect("reading testsuite directory")
.map(|r| r.expect("reading testsuite directory entry"))
@@ -102,9 +106,13 @@ fn test_directory(out: &mut File, testsuite: &str, out_dir: &Path) -> io::Result
}
writeln!(out, "}}")?;
Ok(())
}
}
fn write_testsuite_tests(out: &mut File, dir_entry: DirEntry, testsuite: &str) -> io::Result<()> {
fn write_testsuite_tests(
out: &mut File,
dir_entry: DirEntry,
testsuite: &str,
) -> io::Result<()> {
let path = dir_entry.path();
let stemstr = path
.file_stem()
@@ -149,9 +157,9 @@ fn write_testsuite_tests(out: &mut File, dir_entry: DirEntry, testsuite: &str) -
writeln!(out, " }}")?;
writeln!(out)?;
Ok(())
}
}
cfg_if::cfg_if! {
cfg_if::cfg_if! {
if #[cfg(not(windows))] {
/// Ignore tests that aren't supported yet.
fn ignore(_testsuite: &str, _name: &str) -> bool {
@@ -176,10 +184,10 @@ cfg_if::cfg_if! {
}
}
}
}
}
/// Mark tests which do not require preopens
fn no_preopens(testsuite: &str, name: &str) -> bool {
/// Mark tests which do not require preopens
fn no_preopens(testsuite: &str, name: &str) -> bool {
if testsuite == "wasi-tests" {
match name {
"big_random_buf" => true,
@@ -190,4 +198,5 @@ fn no_preopens(testsuite: &str, name: &str) -> bool {
} else {
unreachable!()
}
}
}

View File

@@ -1,3 +1,4 @@
#![cfg(features = "test-programs")]
mod runtime;
mod utils;