Skip to content

Commit

Permalink
Moved processors out of build
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Apr 16, 2020
1 parent c541521 commit dfb7432
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 111 deletions.
114 changes: 3 additions & 111 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,122 +9,16 @@ use std::fs;
use std::path::PathBuf;
use std::process::Command;
use test_generator::{
build_ignores_from_textfile, extract_name, test_directory, test_directory_module,
with_backends, with_test_module, Test, Testsuite,
build_ignores_from_textfile, emscripten_processor, test_directory,
test_directory_module, wasi_processor, wast_processor, with_backends, with_test_module,
Testsuite,
};

static EMTESTS_ENV_VAR: &str = "WASM_EMSCRIPTEN_GENERATE_EMTESTS";
static WASITESTS_ENV_VAR: &str = "WASM_WASI_GENERATE_WASITESTS";
static WASITESTS_SET_UP_TOOLCHAIN: &str = "WASM_WASI_SET_UP_TOOLCHAIN";
static WASITESTS_GENERATE_ALL: &str = "WASI_TEST_GENERATE_ALL";

/// Given a Testsuite and a path, process the path in case is a wast
/// file.
fn wast_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
if ext != "wast" {
return None;
}

// Ignore files starting with `.`, which could be editor temporary files
if p.file_stem()?.to_str()?.starts_with(".") {
return None;
}

let testname = extract_name(&p);
let body = format!(
"crate::run_wast(r#\"{}\"#, \"{}\")",
p.display(),
out.path.get(0).unwrap()
);

Some(Test {
name: testname.to_string(),
body: body.to_string(),
})
}

/// Given a Testsuite and a path, process the path in case is a Emscripten
/// wasm file.
fn emscripten_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
if ext != "wasm" {
return None;
}

let outfile = {
let mut out_ext = p.clone();
out_ext.set_extension("out");
// let mut txt_ext = p.clone();
// txt_ext.set_extension("txt");
if out_ext.exists() {
out_ext
}
// else if txt_ext.exists() {
// txt_ext
// }
else {
return None;
}
};

let testname = extract_name(&p);
let compiler = out.path.get(0).unwrap();
let body = format!(
"crate::run_emscripten(r#\"{}\"#, r#\"{}\"#, \"{}\")",
p.display(),
outfile.display(),
compiler
);

Some(Test {
name: testname.to_string(),
body: body.to_string(),
})
}

/// Given a Testsuite and a path, process the path in case is a WASI
/// wasm file.
fn wasi_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
if ext != "wasm" {
return None;
}

let outfile = {
let mut out_ext = p.clone();
out_ext.set_extension("out");
// let mut txt_ext = p.clone();
// txt_ext.set_extension("txt");
if out_ext.exists() {
out_ext
}
// else if txt_ext.exists() {
// txt_ext
// }
else {
return None;
}
};

let testname = extract_name(&p);
let compiler = out.path.get(0).unwrap();
let body = format!(
"crate::run_wasi(r#\"{}\"#, r#\"{}\"#, \"{}\")",
p.display(),
outfile.display(),
compiler
);

Some(Test {
name: testname.to_string(),
body: body.to_string(),
})
}

fn is_truthy_env(name: &str) -> bool {
env::var(name).map(|n| n == "1").unwrap_or_default()
}
Expand Down Expand Up @@ -225,8 +119,6 @@ fn main() -> anyhow::Result<()> {
let emtests_output = out_dir.join("generated_emtests.rs");
fs::write(&emtests_output, emtests.buffer)?;

// println!("WASI: {}", wasitests.buffer);

let wasitests_output = out_dir.join("generated_wasitests.rs");
fs::write(&wasitests_output, wasitests.buffer)?;

Expand Down
2 changes: 2 additions & 0 deletions tests/test-generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
//!
//! > This program is inspired/forked from:
//! > https://github.com/bytecodealliance/wasmtime/blob/master/build.rs
mod processors;

pub use crate::processors::{emscripten_processor, wasi_processor, wast_processor};
use anyhow::Context;
use std::collections::HashSet;
use std::fmt::Write;
Expand Down
101 changes: 101 additions & 0 deletions tests/test-generator/src/processors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! Here we define the processors usable for each test genrator
use crate::{Test, Testsuite, extract_name};
use std::path::PathBuf;

/// Given a Testsuite and a path, process the path in case is a wast
/// file.
pub fn wast_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
if ext != "wast" {
return None;
}

// Ignore files starting with `.`, which could be editor temporary files
if p.file_stem()?.to_str()?.starts_with(".") {
return None;
}

let testname = extract_name(&p);
let compiler = out.path.get(0).unwrap();

// The implementation of `run_wast` lives in /tests/spectest.rs
let body = format!("crate::run_wast(r#\"{}\"#, \"{}\")", p.display(), compiler);

Some(Test {
name: testname.to_string(),
body: body.to_string(),
})
}

/// Given a Testsuite and a path, process the path in case is a Emscripten
/// wasm file.
pub fn emscripten_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
if ext != "wasm" {
return None;
}

let outfile = {
let mut out_ext = p.clone();
out_ext.set_extension("out");
if out_ext.exists() {
out_ext
} else {
return None;
}
};

let testname = extract_name(&p);
let compiler = out.path.get(0).unwrap();

// The implementation of `run_emscripten` lives in /tests/emtest.rs
let body = format!(
"crate::run_emscripten(r#\"{}\"#, r#\"{}\"#, \"{}\")",
p.display(),
outfile.display(),
compiler
);

Some(Test {
name: testname.to_string(),
body: body.to_string(),
})
}

/// Given a Testsuite and a path, process the path in case is a WASI
/// wasm file.
pub fn wasi_processor(out: &mut Testsuite, p: PathBuf) -> Option<Test> {
let ext = p.extension()?;
// Only look at wast files.
if ext != "wasm" {
return None;
}

let outfile = {
let mut out_ext = p.clone();
out_ext.set_extension("out");
if out_ext.exists() {
out_ext
} else {
return None;
}
};

let testname = extract_name(&p);
let compiler = out.path.get(0).unwrap();

// The implementation of `run_wasi` lives in /tests/wasitest.rs
let body = format!(
"crate::run_wasi(r#\"{}\"#, r#\"{}\"#, \"{}\")",
p.display(),
outfile.display(),
compiler
);

Some(Test {
name: testname.to_string(),
body: body.to_string(),
})
}

0 comments on commit dfb7432

Please sign in to comment.