Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/wasix/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
*/*.test.wasm
*/*.test.wasm
*/*.out
wasix-tests
*/*/*
!*/*/build.sh
!*/*/*.c
!*/*/*.cpp
!*/*/*.hpp
!*/*/*.h
14 changes: 14 additions & 0 deletions lib/wasix/tests/basic_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Basic tests from wasix-tests directory
//!
//! These tests verify fundamental functionality:
//! - helloworld: Basic printf and return 0

mod wasixcc_test_utils;
use wasixcc_test_utils::{run_build_script, run_wasm};

#[test]
fn test_helloworld() {
let wasm_path = run_build_script(file!(), "helloworld").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}
3 changes: 3 additions & 0 deletions lib/wasix/tests/basic_tests/helloworld/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC main.c -o main
6 changes: 6 additions & 0 deletions lib/wasix/tests/basic_tests/helloworld/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}
124 changes: 59 additions & 65 deletions lib/wasix/tests/context_switching.rs
Original file line number Diff line number Diff line change
@@ -1,162 +1,156 @@
use std::path::PathBuf;
use std::process::Command;
use wasmer::Module;
use wasmer_types::ModuleHash;
use wasmer_wasix::runners::wasi::{RuntimeOrEngine, WasiRunner};

fn test_with_wasixcc(name: &str) -> Result<(), anyhow::Error> {
eprintln!("Compiling test case: {}", name);
let test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join(PathBuf::from(
file!()
.split('/')
.next_back()
.unwrap()
.trim_end_matches(".rs"),
));
let main_c = test_dir.join(format!("{name}.c"));
let main_wasm = test_dir.join(format!("{name}.test.wasm"));

// Compile with wasixcc
let mut command = Command::new("wasixcc");
command
.arg(&main_c)
.arg("-fwasm-exceptions")
.arg("-o")
.arg(&main_wasm)
.current_dir(&test_dir);
eprintln!("Running wasixcc: {:?}", command);
let compile_status = command.status().expect("Failed to run wasixcc");
assert!(compile_status.success(), "wasixcc compilation failed");

// Load the compiled WASM module
let wasm_bytes = std::fs::read(&main_wasm).expect("Failed to read compiled WASM file");
let engine = wasmer::Engine::default();
let module = Module::new(&engine, &wasm_bytes).expect("Failed to create module");

// Run the WASM module using WasiRunner
let runner = WasiRunner::new();
runner.run_wasm(
RuntimeOrEngine::Engine(engine),
"wasix-test",
module,
ModuleHash::random(),
)
}
mod wasixcc_test_utils;
use wasixcc_test_utils::{run_build_script, run_wasm};

// macOS is currently disabled, because cranelift does not
// support exception handling on that platform yet.
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_simple_switching() {
test_with_wasixcc("simple_switching").unwrap();
let wasm_path = run_build_script(file!(), "simple_switching").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_switching_with_main() {
test_with_wasixcc("switching_with_main").unwrap();
let wasm_path = run_build_script(file!(), "switching_with_main").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_switching_to_a_deleted_context() {
test_with_wasixcc("switching_to_a_deleted_context").unwrap();
let wasm_path = run_build_script(file!(), "switching_to_a_deleted_context").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_switching_threads() {
test_with_wasixcc("switching_in_threads").unwrap();
let wasm_path = run_build_script(file!(), "switching_in_threads").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_multiple_contexts() {
test_with_wasixcc("multiple_contexts").unwrap();
let wasm_path = run_build_script(file!(), "multiple_contexts").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_error_handling() {
test_with_wasixcc("error_handling").unwrap();
let wasm_path = run_build_script(file!(), "error_handling").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_nested_switches() {
test_with_wasixcc("nested_switches").unwrap();
let wasm_path = run_build_script(file!(), "nested_switches").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_contexts_with_mutexes() {
test_with_wasixcc("contexts_with_mutexes").unwrap();
let wasm_path = run_build_script(file!(), "contexts_with_mutexes").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_contexts_with_env_vars() {
test_with_wasixcc("contexts_with_env_vars").unwrap();
let wasm_path = run_build_script(file!(), "contexts_with_env_vars").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_contexts_with_signals() {
test_with_wasixcc("contexts_with_signals").unwrap();
let wasm_path = run_build_script(file!(), "contexts_with_signals").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_contexts_with_timers() {
test_with_wasixcc("contexts_with_timers").unwrap();
let wasm_path = run_build_script(file!(), "contexts_with_timers").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_contexts_with_pipes() {
test_with_wasixcc("contexts_with_pipes").unwrap();
let wasm_path = run_build_script(file!(), "contexts_with_pipes").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_pending_file_operations() {
test_with_wasixcc("pending_file_operations").unwrap();
let wasm_path = run_build_script(file!(), "pending_file_operations").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_recursive_host_calls() {
test_with_wasixcc("recursive_host_calls").unwrap();
let wasm_path = run_build_script(file!(), "recursive_host_calls").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_malloc_during_switch() {
test_with_wasixcc("malloc_during_switch").unwrap();
let wasm_path = run_build_script(file!(), "malloc_during_switch").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_nested_host_call_switch() {
test_with_wasixcc("nested_host_call_switch").unwrap();
let wasm_path = run_build_script(file!(), "nested_host_call_switch").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_switch_to_never_resumed() {
test_with_wasixcc("switch_to_never_resumed").unwrap();
let wasm_path = run_build_script(file!(), "switch_to_never_resumed").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_three_way_recursion() {
test_with_wasixcc("three_way_recursion").unwrap();
let wasm_path = run_build_script(file!(), "three_way_recursion").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_contexts_with_setjmp() {
test_with_wasixcc("contexts_with_setjmp").unwrap();
let wasm_path = run_build_script(file!(), "contexts_with_setjmp").unwrap();
let test_dir = wasm_path.parent().unwrap();
run_wasm(&wasm_path, test_dir).unwrap();
}
Comment on lines 8 to 156
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repetitive code pattern across all test functions. Each test function follows the exact same three-line pattern. Consider creating a helper macro or function to reduce code duplication, which would improve maintainability and reduce the chance of copy-paste errors.

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC contexts_with_env_vars.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC contexts_with_mutexes.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC contexts_with_pipes.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC contexts_with_setjmp.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC contexts_with_signals.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC contexts_with_timers.c -o main
3 changes: 3 additions & 0 deletions lib/wasix/tests/context_switching/error_handling/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC error_handling.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC malloc_during_switch.c -o main
3 changes: 3 additions & 0 deletions lib/wasix/tests/context_switching/multiple_contexts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC multiple_contexts.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC nested_host_call_switch.c -o main
3 changes: 3 additions & 0 deletions lib/wasix/tests/context_switching/nested_switches/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC nested_switches.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC pending_file_operations.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC recursive_host_calls.c -o main
3 changes: 3 additions & 0 deletions lib/wasix/tests/context_switching/simple_switching/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC simple_switching.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC switch_to_never_resumed.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC switching_in_threads.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC switching_to_a_deleted_context.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC switching_with_main.c -o main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
$CC three_way_recursion.c -o main
Loading
Loading