()
| 970 | |
| 971 | #[test] |
| 972 | fn preview2_stdin() -> Result<()> { |
| 973 | let test = "tests/all/cli_tests/count-stdin.wat"; |
| 974 | let cmd = || -> Result<_> { |
| 975 | let mut cmd = get_wasmtime_command()?; |
| 976 | cmd.arg("--invoke=count").arg("-Spreview2").arg(test); |
| 977 | Ok(cmd) |
| 978 | }; |
| 979 | |
| 980 | // read empty pipe is ok |
| 981 | let output = cmd()?.output()?; |
| 982 | assert!(output.status.success()); |
| 983 | assert_eq!(String::from_utf8_lossy(&output.stdout), "0\n"); |
| 984 | |
| 985 | // read itself is ok |
| 986 | let file = File::open(test)?; |
| 987 | let size = file.metadata()?.len(); |
| 988 | let output = cmd()?.stdin(File::open(test)?).output()?; |
| 989 | assert!(output.status.success()); |
| 990 | assert_eq!(String::from_utf8_lossy(&output.stdout), format!("{size}\n")); |
| 991 | |
| 992 | // read piped input ok is ok |
| 993 | let mut child = cmd()? |
| 994 | .stdin(Stdio::piped()) |
| 995 | .stdout(Stdio::piped()) |
| 996 | .stderr(Stdio::piped()) |
| 997 | .spawn()?; |
| 998 | let mut stdin = child.stdin.take().unwrap(); |
| 999 | std::thread::spawn(move || { |
| 1000 | stdin.write_all(b"hello").unwrap(); |
| 1001 | }); |
| 1002 | let output = child.wait_with_output()?; |
| 1003 | assert!(output.status.success()); |
| 1004 | assert_eq!(String::from_utf8_lossy(&output.stdout), "5\n"); |
| 1005 | |
| 1006 | let count_up_to = |n: usize| -> Result<_> { |
| 1007 | let mut child = get_wasmtime_command()? |
| 1008 | .arg("--invoke=count-up-to") |
| 1009 | .arg("-Spreview2") |
| 1010 | .arg(test) |
| 1011 | .arg(n.to_string()) |
| 1012 | .stdin(Stdio::piped()) |
| 1013 | .stdout(Stdio::piped()) |
| 1014 | .stderr(Stdio::piped()) |
| 1015 | .spawn()?; |
| 1016 | let mut stdin = child.stdin.take().unwrap(); |
| 1017 | let t = std::thread::spawn(move || { |
| 1018 | let mut written = 0; |
| 1019 | let bytes = [0; 64 * 1024]; |
| 1020 | loop { |
| 1021 | written += match stdin.write(&bytes) { |
| 1022 | Ok(n) => n, |
| 1023 | Err(_) => break written, |
| 1024 | }; |
| 1025 | } |
| 1026 | }); |
| 1027 | let output = child.wait_with_output()?; |
| 1028 | assert!(output.status.success()); |
| 1029 | let written = t.join().unwrap(); |
nothing calls this directly
no test coverage detected