mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
moved all crates into seperate folder + related path fixes
This commit is contained in:
parent
12ef03bb86
commit
eee85fa45d
1063 changed files with 92 additions and 93 deletions
154
crates/repl_test/src/cli.rs
Normal file
154
crates/repl_test/src/cli.rs
Normal file
|
@ -0,0 +1,154 @@
|
|||
use std::env;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, ExitStatus, Stdio};
|
||||
|
||||
use roc_repl_cli::{INSTRUCTIONS, WELCOME_MESSAGE};
|
||||
use roc_test_utils::assert_multiline_str_eq;
|
||||
|
||||
const ERROR_MESSAGE_START: char = '─';
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Out {
|
||||
stdout: String,
|
||||
stderr: String,
|
||||
status: ExitStatus,
|
||||
}
|
||||
|
||||
fn path_to_roc_binary() -> PathBuf {
|
||||
// Adapted from https://github.com/volta-cli/volta/blob/cefdf7436a15af3ce3a38b8fe53bb0cfdb37d3dd/tests/acceptance/support/sandbox.rs#L680
|
||||
// by the Volta Contributors - license information can be found in
|
||||
// the LEGAL_DETAILS file in the root directory of this distribution.
|
||||
//
|
||||
// Thank you, Volta contributors!
|
||||
let mut path = env::var_os("CARGO_BIN_PATH")
|
||||
.map(PathBuf::from)
|
||||
.or_else(|| {
|
||||
env::current_exe().ok().map(|mut path| {
|
||||
path.pop();
|
||||
if path.ends_with("deps") {
|
||||
path.pop();
|
||||
}
|
||||
path
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| panic!("CARGO_BIN_PATH wasn't set, and couldn't be inferred from context. Can't run CLI tests."));
|
||||
|
||||
path.push("roc");
|
||||
|
||||
path
|
||||
}
|
||||
|
||||
fn repl_eval(input: &str) -> Out {
|
||||
let mut cmd = Command::new(path_to_roc_binary());
|
||||
|
||||
cmd.arg("repl");
|
||||
|
||||
let mut child = cmd
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute compiled `roc` binary in CLI test");
|
||||
|
||||
{
|
||||
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
|
||||
|
||||
// Send the input expression
|
||||
stdin
|
||||
.write_all(input.as_bytes())
|
||||
.expect("Failed to write input to stdin");
|
||||
|
||||
// Evaluate the expression
|
||||
stdin
|
||||
.write_all(b"\n")
|
||||
.expect("Failed to write newline to stdin");
|
||||
|
||||
// Gracefully exit the repl
|
||||
stdin
|
||||
.write_all(b":exit\n")
|
||||
.expect("Failed to write :exit to stdin");
|
||||
}
|
||||
|
||||
let output = child
|
||||
.wait_with_output()
|
||||
.expect("Error waiting for REPL child process to exit.");
|
||||
|
||||
// Remove the initial instructions from the output.
|
||||
|
||||
let expected_instructions = format!("{}{}", WELCOME_MESSAGE, INSTRUCTIONS);
|
||||
let stdout = String::from_utf8(output.stdout).unwrap();
|
||||
|
||||
assert!(
|
||||
stdout.starts_with(&expected_instructions),
|
||||
"Unexpected repl output: {}",
|
||||
stdout
|
||||
);
|
||||
|
||||
let (_, answer) = stdout.split_at(expected_instructions.len());
|
||||
let answer = if answer.is_empty() {
|
||||
// The repl crashed before completing the evaluation.
|
||||
// This is most likely due to a segfault.
|
||||
if output.status.to_string() == "signal: 11" {
|
||||
panic!(
|
||||
"repl segfaulted during the test. Stderr was {:?}",
|
||||
String::from_utf8(output.stderr).unwrap()
|
||||
);
|
||||
} else {
|
||||
panic!("repl exited unexpectedly before finishing evaluation. Exit status was {:?} and stderr was {:?}", output.status, String::from_utf8(output.stderr).unwrap());
|
||||
}
|
||||
} else {
|
||||
let expected_after_answer = "\n".to_string();
|
||||
|
||||
assert!(
|
||||
answer.ends_with(&expected_after_answer),
|
||||
"Unexpected repl output after answer: {}",
|
||||
answer
|
||||
);
|
||||
|
||||
// Use [1..] to trim the leading '\n'
|
||||
// and (len - 1) to trim the trailing '\n'
|
||||
let (answer, _) = answer[1..].split_at(answer.len() - expected_after_answer.len() - 1);
|
||||
|
||||
// Remove ANSI escape codes from the answer - for example:
|
||||
//
|
||||
// Before: "42 \u{1b}[35m:\u{1b}[0m Num *"
|
||||
// After: "42 : Num *"
|
||||
strip_ansi_escapes::strip(answer).unwrap()
|
||||
};
|
||||
|
||||
Out {
|
||||
stdout: String::from_utf8(answer).unwrap(),
|
||||
stderr: String::from_utf8(output.stderr).unwrap(),
|
||||
status: output.status,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_success(input: &str, expected: &str) {
|
||||
let out = repl_eval(input);
|
||||
|
||||
assert_multiline_str_eq!("", out.stderr.as_str());
|
||||
assert_multiline_str_eq!(expected, out.stdout.as_str());
|
||||
assert!(out.status.success());
|
||||
}
|
||||
|
||||
pub fn expect_failure(input: &str, expected: &str) {
|
||||
let out = repl_eval(input);
|
||||
|
||||
// there may be some other stuff printed (e.g. unification errors)
|
||||
// so skip till the header of the first error
|
||||
match out.stdout.find(ERROR_MESSAGE_START) {
|
||||
Some(index) => {
|
||||
assert_multiline_str_eq!("", out.stderr.as_str());
|
||||
assert_multiline_str_eq!(expected, &out.stdout[index..]);
|
||||
assert!(out.status.success());
|
||||
}
|
||||
None => {
|
||||
assert_multiline_str_eq!("", out.stderr.as_str());
|
||||
assert!(out.status.success());
|
||||
panic!(
|
||||
"I expected a failure, but there is no error message in stdout:\n\n{}",
|
||||
&out.stdout
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
12
crates/repl_test/src/lib.rs
Normal file
12
crates/repl_test/src/lib.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
#[allow(unused_imports)]
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(all(test, not(feature = "wasm")))]
|
||||
mod cli;
|
||||
|
||||
#[cfg(all(test, feature = "wasm"))]
|
||||
mod wasm;
|
1181
crates/repl_test/src/tests.rs
Normal file
1181
crates/repl_test/src/tests.rs
Normal file
File diff suppressed because it is too large
Load diff
278
crates/repl_test/src/wasm.rs
Normal file
278
crates/repl_test/src/wasm.rs
Normal file
|
@ -0,0 +1,278 @@
|
|||
use std::{
|
||||
cell::RefCell,
|
||||
fs,
|
||||
ops::{Deref, DerefMut},
|
||||
path::Path,
|
||||
sync::Mutex,
|
||||
thread_local,
|
||||
};
|
||||
use wasmer::{
|
||||
imports, ChainableNamedResolver, Function, ImportObject, Instance, Module, Store, Value,
|
||||
};
|
||||
use wasmer_wasi::WasiState;
|
||||
|
||||
const WASM_REPL_COMPILER_PATH: &str = "../target/wasm32-wasi/release/roc_repl_wasm.wasm";
|
||||
|
||||
thread_local! {
|
||||
static REPL_STATE: RefCell<Option<ReplState>> = RefCell::new(None)
|
||||
}
|
||||
|
||||
// The compiler Wasm instance.
|
||||
// This takes several *seconds* to initialise, so we only want to do it once for all tests.
|
||||
// Every test mutates compiler memory in `unsafe` ways, so we run them sequentially using a Mutex.
|
||||
// Even if Cargo uses many threads, these tests won't go any faster. But that's fine, they're quick.
|
||||
lazy_static! {
|
||||
static ref COMPILER: Instance = init_compiler();
|
||||
static ref TEST_MUTEX: Mutex<()> = Mutex::new(());
|
||||
}
|
||||
|
||||
/// Load the compiler .wasm file and get it ready to execute
|
||||
/// THIS FUNCTION TAKES 4 SECONDS TO RUN
|
||||
fn init_compiler() -> Instance {
|
||||
let path = Path::new(WASM_REPL_COMPILER_PATH);
|
||||
let wasm_module_bytes = match fs::read(&path) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(e) => panic!("{}", format_compiler_load_error(e)),
|
||||
};
|
||||
|
||||
let store = Store::default();
|
||||
|
||||
// This is the slow line. Skipping validation checks reduces module compilation time from 5s to 4s.
|
||||
// Safety: We trust rustc to produce a valid module.
|
||||
let wasmer_module =
|
||||
unsafe { Module::from_binary_unchecked(&store, &wasm_module_bytes).unwrap() };
|
||||
|
||||
// Specify the external functions the Wasm module needs to link to
|
||||
// We only use WASI so that we can debug test failures more easily with println!(), dbg!(), etc.
|
||||
let mut wasi_env = WasiState::new("compiler").finalize().unwrap();
|
||||
let wasi_import_obj = wasi_env
|
||||
.import_object(&wasmer_module)
|
||||
.unwrap_or_else(|_| ImportObject::new());
|
||||
let repl_import_obj = imports! {
|
||||
"env" => {
|
||||
"wasmer_create_app" => Function::new_native(&store, wasmer_create_app),
|
||||
"wasmer_run_app" => Function::new_native(&store, wasmer_run_app),
|
||||
"wasmer_get_result_and_memory" => Function::new_native(&store, wasmer_get_result_and_memory),
|
||||
"wasmer_copy_input_string" => Function::new_native(&store, wasmer_copy_input_string),
|
||||
"wasmer_copy_output_string" => Function::new_native(&store, wasmer_copy_output_string),
|
||||
"now" => Function::new_native(&store, dummy_system_time_now),
|
||||
}
|
||||
};
|
||||
// "Chain" the import objects together. Wasmer will look up the REPL object first, then the WASI object
|
||||
let import_object = wasi_import_obj.chain_front(repl_import_obj);
|
||||
|
||||
// Make a fully-linked instance with its own block of memory
|
||||
Instance::new(&wasmer_module, &import_object).unwrap()
|
||||
}
|
||||
|
||||
struct ReplState {
|
||||
src: &'static str,
|
||||
app: Option<Instance>,
|
||||
result_addr: Option<u32>,
|
||||
output: Option<String>,
|
||||
}
|
||||
|
||||
fn wasmer_create_app(app_bytes_ptr: u32, app_bytes_len: u32) -> u32 {
|
||||
let app: Instance = {
|
||||
let memory = COMPILER.exports.get_memory("memory").unwrap();
|
||||
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
|
||||
|
||||
// Find the slice of bytes for the compiled Roc app
|
||||
let ptr = app_bytes_ptr as usize;
|
||||
let len = app_bytes_len as usize;
|
||||
let app_module_bytes: &[u8] = &memory_bytes[ptr..][..len];
|
||||
|
||||
// Parse the bytes into a Wasmer module
|
||||
let store = Store::default();
|
||||
let wasmer_module = match Module::new(&store, app_module_bytes) {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
println!("Failed to create Wasm module\n{:?}", e);
|
||||
if false {
|
||||
let path = "/tmp/roc_repl_test_invalid_app.wasm";
|
||||
fs::write(path, app_module_bytes).unwrap();
|
||||
println!("Wrote invalid wasm to {}", path);
|
||||
}
|
||||
return false.into();
|
||||
}
|
||||
};
|
||||
|
||||
// Get the WASI imports for the app
|
||||
let mut wasi_env = WasiState::new("app").finalize().unwrap();
|
||||
let import_object = wasi_env
|
||||
.import_object(&wasmer_module)
|
||||
.unwrap_or_else(|_| imports!());
|
||||
|
||||
// Create an executable instance
|
||||
match Instance::new(&wasmer_module, &import_object) {
|
||||
Ok(instance) => instance,
|
||||
Err(e) => {
|
||||
println!("Failed to create Wasm instance {:?}", e);
|
||||
return false.into();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
REPL_STATE.with(|f| {
|
||||
if let Some(state) = f.borrow_mut().deref_mut() {
|
||||
state.app = Some(app)
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
});
|
||||
|
||||
return true.into();
|
||||
}
|
||||
|
||||
fn wasmer_run_app() -> u32 {
|
||||
REPL_STATE.with(|f| {
|
||||
if let Some(state) = f.borrow_mut().deref_mut() {
|
||||
if let Some(app) = &state.app {
|
||||
let wrapper = app.exports.get_function("wrapper").unwrap();
|
||||
|
||||
let result_addr: i32 = match wrapper.call(&[]) {
|
||||
Err(e) => panic!("{:?}", e),
|
||||
Ok(result) => result[0].unwrap_i32(),
|
||||
};
|
||||
state.result_addr = Some(result_addr as u32);
|
||||
|
||||
let memory = app.exports.get_memory("memory").unwrap();
|
||||
memory.size().bytes().0 as u32
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn wasmer_get_result_and_memory(buffer_alloc_addr: u32) -> u32 {
|
||||
REPL_STATE.with(|f| {
|
||||
if let Some(state) = f.borrow().deref() {
|
||||
if let Some(app) = &state.app {
|
||||
let app_memory = app.exports.get_memory("memory").unwrap();
|
||||
let result_addr = state.result_addr.unwrap();
|
||||
|
||||
let app_memory_bytes: &[u8] = unsafe { app_memory.data_unchecked() };
|
||||
|
||||
let buf_addr = buffer_alloc_addr as usize;
|
||||
let len = app_memory_bytes.len();
|
||||
|
||||
let memory = COMPILER.exports.get_memory("memory").unwrap();
|
||||
let compiler_memory_bytes: &mut [u8] = unsafe { memory.data_unchecked_mut() };
|
||||
compiler_memory_bytes[buf_addr..][..len].copy_from_slice(app_memory_bytes);
|
||||
|
||||
result_addr
|
||||
} else {
|
||||
panic!("REPL app not found")
|
||||
}
|
||||
} else {
|
||||
panic!("REPL state not found")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn wasmer_copy_input_string(src_buffer_addr: u32) {
|
||||
let src = REPL_STATE.with(|rs| {
|
||||
if let Some(state) = rs.borrow_mut().deref_mut() {
|
||||
std::mem::take(&mut state.src)
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
});
|
||||
|
||||
let memory = COMPILER.exports.get_memory("memory").unwrap();
|
||||
let memory_bytes: &mut [u8] = unsafe { memory.data_unchecked_mut() };
|
||||
|
||||
let buf_addr = src_buffer_addr as usize;
|
||||
let len = src.len();
|
||||
memory_bytes[buf_addr..][..len].copy_from_slice(src.as_bytes());
|
||||
}
|
||||
|
||||
fn wasmer_copy_output_string(output_ptr: u32, output_len: u32) {
|
||||
let output: String = {
|
||||
let memory = COMPILER.exports.get_memory("memory").unwrap();
|
||||
let memory_bytes: &[u8] = unsafe { memory.data_unchecked() };
|
||||
|
||||
// Find the slice of bytes for the output string
|
||||
let ptr = output_ptr as usize;
|
||||
let len = output_len as usize;
|
||||
let output_bytes: &[u8] = &memory_bytes[ptr..][..len];
|
||||
|
||||
// Copy it out of the Wasm module
|
||||
let copied_bytes = output_bytes.to_vec();
|
||||
unsafe { String::from_utf8_unchecked(copied_bytes) }
|
||||
};
|
||||
|
||||
REPL_STATE.with(|f| {
|
||||
if let Some(state) = f.borrow_mut().deref_mut() {
|
||||
state.output = Some(output)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn format_compiler_load_error(e: std::io::Error) -> String {
|
||||
if matches!(e.kind(), std::io::ErrorKind::NotFound) {
|
||||
format!(
|
||||
"\n\n {}\n\n",
|
||||
[
|
||||
"ROC COMPILER WASM BINARY NOT FOUND",
|
||||
"Please run these tests using repl_test/run_wasm.sh!",
|
||||
"It will build a .wasm binary for the compiler, and a native binary for the tests themselves",
|
||||
]
|
||||
.join("\n ")
|
||||
)
|
||||
} else {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
}
|
||||
|
||||
fn dummy_system_time_now() -> f64 {
|
||||
0.0
|
||||
}
|
||||
|
||||
fn run(src: &'static str) -> (bool, String) {
|
||||
REPL_STATE.with(|rs| {
|
||||
*rs.borrow_mut().deref_mut() = Some(ReplState {
|
||||
src,
|
||||
app: None,
|
||||
result_addr: None,
|
||||
output: None,
|
||||
});
|
||||
});
|
||||
|
||||
let ok = if let Ok(_guard) = TEST_MUTEX.lock() {
|
||||
let entrypoint = COMPILER
|
||||
.exports
|
||||
.get_function("entrypoint_from_test")
|
||||
.unwrap();
|
||||
|
||||
let src_len = Value::I32(src.len() as i32);
|
||||
let wasm_ok: i32 = entrypoint.call(&[src_len]).unwrap().deref()[0].unwrap_i32();
|
||||
wasm_ok != 0
|
||||
} else {
|
||||
panic!(
|
||||
"Failed to acquire test mutex! A previous test must have panicked while holding it, running Wasm"
|
||||
)
|
||||
};
|
||||
|
||||
let final_state: ReplState = REPL_STATE.with(|rs| rs.take()).unwrap();
|
||||
let output: String = final_state.output.unwrap();
|
||||
|
||||
(ok, output)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_success(input: &'static str, expected: &str) {
|
||||
let (ok, output) = run(input);
|
||||
assert_eq!(ok, true);
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_failure(input: &'static str, expected: &str) {
|
||||
let (ok, output) = run(input);
|
||||
assert_eq!(ok, false);
|
||||
assert_eq!(output, expected);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue