Skip to content

Commit

Permalink
Made sure the wat2wasm runner test exercises stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Oct 27, 2023
1 parent 89defc0 commit a9f369f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
13 changes: 12 additions & 1 deletion lib/virtual-fs/src/arc_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
};
use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};

#[derive(Derivative, Clone)]
#[derive(Derivative)]
#[derivative(Debug)]
pub struct ArcFile<T>
where
Expand Down Expand Up @@ -153,6 +153,17 @@ where
}
}

impl<T> Clone for ArcFile<T>
where
T: VirtualFile + Send + Sync + 'static,
{
fn clone(&self) -> Self {
ArcFile {
inner: self.inner.clone(),
}
}
}

impl<T> ClonableVirtualFile for ArcFile<T>
where
T: VirtualFile + Send + Sync + 'static,
Expand Down
21 changes: 14 additions & 7 deletions lib/wasix/tests/runners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use wasmer_wasix::{
use webc::Container;

mod wasi {
use tracing_subscriber::fmt::format::FmtSpan;
use virtual_fs::{AsyncReadExt, AsyncSeekExt};
use wasmer_wasix::{bin_factory::BinaryPackage, runners::wasi::WasiRunner, WasiError};

use super::*;
Expand All @@ -39,28 +39,32 @@ mod wasi {

#[tokio::test(flavor = "multi_thread")]
async fn wat2wasm() {
tracing_subscriber::fmt::fmt()
.with_max_level(tracing::level_filters::LevelFilter::TRACE)
.with_span_events(FmtSpan::FULL)
.init();
let webc = download_cached("https://wasmer.io/wasmer/[email protected]").await;
let container = Container::from_bytes(webc).unwrap();
let (rt, tasks) = runtime();
let pkg = BinaryPackage::from_webc(&container, &rt).await.unwrap();
let mut stdout = virtual_fs::ArcFile::new(Box::new(virtual_fs::BufferFile::default()));

// Note: we don't have any way to intercept stdin or stdout, so blindly
// assume that everything is fine if it runs successfully.
let stdout_2 = stdout.clone();
let handle = std::thread::spawn(move || {
let _guard = tasks.runtime_handle().enter();
WasiRunner::new()
.with_args(["--version"])
.with_stdin(Box::new(virtual_fs::NullFile::default()))
.with_stdout(Box::new(stdout_2) as Box<_>)
.with_stderr(Box::new(virtual_fs::NullFile::default()))
.run_command("wat2wasm", &pkg, Arc::new(rt))
});

handle
.join()
.unwrap()
.expect("The runner encountered an error");

stdout.rewind().await.unwrap();
let mut output = Vec::new();
stdout.read_to_end(&mut output).await.unwrap();
assert_eq!(String::from_utf8(output).unwrap(), "1.0.37 (git~v1.0.37)\n");
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -74,6 +78,9 @@ mod wasi {
let _guard = tasks.runtime_handle().enter();
WasiRunner::new()
.with_args(["-c", "import sys; sys.exit(42)"])
.with_stdin(Box::new(virtual_fs::NullFile::default()))
.with_stdout(Box::new(virtual_fs::NullFile::default()))
.with_stderr(Box::new(virtual_fs::NullFile::default()))
.run_command("python", &pkg, Arc::new(rt))
});

Expand Down

0 comments on commit a9f369f

Please sign in to comment.