|
1 | | -use assert_cmd::prelude::*; |
2 | 1 | use assert_fs::prelude::*; |
3 | | -use predicates::prelude::*; |
4 | | -use std::io::Write; |
5 | | -use std::process::{Command, Stdio}; |
| 2 | +use axum::routing::get; |
| 3 | +use axum::Router; |
| 4 | +use std::process::Stdio; |
| 5 | +use tokio::io::AsyncWriteExt; |
| 6 | +use tokio::process::Command; |
6 | 7 |
|
7 | | -#[test] |
8 | | -#[allow(clippy::zombie_processes)] |
9 | | -fn test_command_repl() -> Result<(), Box<dyn std::error::Error>> { |
10 | | - let mut child = Command::cargo_bin("basjoofan")? |
| 8 | +#[tokio::test] |
| 9 | +async fn test_command_repl() -> Result<(), Box<dyn std::error::Error>> { |
| 10 | + let mut child = Command::new("basjoofan") |
11 | 11 | .stdin(Stdio::piped()) |
12 | 12 | .stdout(Stdio::piped()) |
13 | 13 | .spawn() |
14 | 14 | .expect("Failed to spawn child process"); |
15 | 15 | if let Some(mut stdin) = child.stdin.take() { |
16 | | - stdin |
17 | | - .write_all("let x = 1 + 1; println(\"{x}\", x);\n".as_bytes()) |
18 | | - .expect("Failed to write to stdin"); |
19 | | - stdin.write_all("exit".as_bytes()).expect("Failed to write to stdin"); |
| 16 | + stdin.write_all("let x = 1 + 1; println(\"{x}\", x);\n".as_bytes()).await?; |
| 17 | + stdin.write_all("exit".as_bytes()).await?; |
20 | 18 | } |
21 | | - let output = child.wait_with_output().expect("Failed to read stdout"); |
| 19 | + let output = child.wait_with_output().await?; |
22 | 20 | println!("output:{}", String::from_utf8_lossy(&output.stdout).trim()); |
23 | 21 | assert!(String::from_utf8_lossy(&output.stdout).trim() == "2\nnull"); |
24 | 22 | Ok(()) |
25 | 23 | } |
26 | 24 |
|
27 | | -#[test] |
28 | | -fn test_command_eval() -> Result<(), Box<dyn std::error::Error>> { |
29 | | - let mut cmd = Command::cargo_bin("basjoofan")?; |
30 | | - cmd.arg("eval").arg(r#"print("{integer}", 1 + 1 )"#); |
31 | | - cmd.assert().success().stdout(predicate::str::diff("2null\n")); |
32 | | - let mut cmd = Command::cargo_bin("basjoofan")?; |
33 | | - cmd.arg("eval").arg(r#"let x = 1 + 1; print("{integer}", x);"#); |
34 | | - cmd.assert().success().stdout(predicate::str::diff("2null\n")); |
35 | | - let mut cmd = Command::cargo_bin("basjoofan")?; |
36 | | - cmd.arg("eval").arg(r#"println("{string}", "Hello Basjoofan!")"#); |
37 | | - cmd.assert().success().stdout(predicate::str::diff("Hello Basjoofan!\nnull\n")); |
| 25 | +#[tokio::test] |
| 26 | +async fn test_command_eval() -> Result<(), Box<dyn std::error::Error>> { |
| 27 | + let mut cmd = Command::new("basjoofan"); |
| 28 | + let output = cmd.arg("eval").arg(r#"print("{integer}", 1 + 1 )"#).output().await?; |
| 29 | + assert!(output.status.success()); |
| 30 | + assert_eq!(String::from_utf8(output.stdout)?, "2null\n"); |
| 31 | + let mut cmd = Command::new("basjoofan"); |
| 32 | + let output = cmd.arg("eval").arg(r#"let x = 1 + 1; print("{integer}", x);"#).output().await?; |
| 33 | + assert!(output.status.success()); |
| 34 | + assert_eq!(String::from_utf8(output.stdout)?, "2null\n"); |
| 35 | + let mut cmd = Command::new("basjoofan"); |
| 36 | + let output = cmd.arg("eval").arg(r#"println("{string}", "Hello Basjoofan!")"#).output().await?; |
| 37 | + assert!(output.status.success()); |
| 38 | + assert_eq!(String::from_utf8(output.stdout)?, "Hello Basjoofan!\nnull\n"); |
38 | 39 | Ok(()) |
39 | 40 | } |
40 | 41 |
|
41 | | -#[test] |
42 | | -fn test_command_test() -> Result<(), Box<dyn std::error::Error>> { |
| 42 | +#[tokio::test] |
| 43 | +async fn test_command_test() -> Result<(), Box<dyn std::error::Error>> { |
| 44 | + let router = Router::new().route("/hello", get(|| async { "Hello, World!" })); |
| 45 | + let listener = tokio::net::TcpListener::bind(("127.0.0.1", 8888)).await.unwrap(); |
| 46 | + tokio::spawn(async move { |
| 47 | + axum::serve(listener, router).await.unwrap(); |
| 48 | + }); |
43 | 49 | let temp = assert_fs::TempDir::new().unwrap(); |
44 | | - let file = temp.child(format!("request.{}", "fan")); |
| 50 | + let file = temp.child("request.fan"); |
45 | 51 | let text = r#" |
46 | | - let host = "httpbin.org"; |
47 | | - request get` |
48 | | - GET http://{host}/get |
| 52 | + let host = "localhost:8888"; |
| 53 | + request hello` |
| 54 | + GET http://{host}/hello |
49 | 55 | Host: {host} |
50 | | - Connection: close |
51 | 56 | `[status == 200]; |
52 | 57 |
|
53 | 58 | test call { |
54 | | - let response = get(); |
| 59 | + let response = hello(); |
55 | 60 | response.status |
56 | 61 | } |
57 | 62 | "#; |
58 | 63 | file.write_str(text)?; |
59 | 64 | // command test |
60 | | - let mut command = Command::cargo_bin("basjoofan")?; |
| 65 | + let mut command = Command::new("basjoofan"); |
61 | 66 | command.current_dir(&temp); |
62 | | - command.arg("test"); |
63 | | - command.assert().success().stdout(predicate::str::contains("--- PASS get (")); |
| 67 | + let output = command.arg("test").output().await?; |
| 68 | + assert!(output.status.success()); |
| 69 | + let stdout = String::from_utf8(output.stdout)?; |
| 70 | + let stderr = String::from_utf8(output.stderr)?; |
| 71 | + println!("stdout:{}", stdout); |
| 72 | + println!("stderr:{}", stderr); |
| 73 | + assert!(stdout.contains("--- PASS hello (")); |
64 | 74 | // command test call |
65 | | - let mut command = Command::cargo_bin("basjoofan")?; |
| 75 | + let mut command = Command::new("basjoofan"); |
66 | 76 | command.current_dir(&temp); |
67 | | - command.arg("test").arg("call"); |
68 | | - command.assert().success().stdout(predicate::str::contains("--- PASS get (")); |
| 77 | + let output = command.arg("test").arg("call").output().await?; |
| 78 | + assert!(output.status.success()); |
| 79 | + assert!(String::from_utf8(output.stdout)?.contains("--- PASS hello (")); |
69 | 80 | // command test blank |
70 | | - let mut command = Command::cargo_bin("basjoofan")?; |
| 81 | + let mut command = Command::new("basjoofan"); |
71 | 82 | command.current_dir(&temp); |
72 | | - command.arg("test").arg("blank"); |
73 | | - command.assert().success().stdout(predicate::str::diff("Test not found: blank\n")); |
74 | | - |
| 83 | + let output = command.arg("test").arg("blank").output().await?; |
| 84 | + assert!(output.status.success()); |
| 85 | + assert_eq!(String::from_utf8(output.stdout)?, "Test not found: blank\n"); |
75 | 86 | Ok(()) |
76 | 87 | } |
0 commit comments