Skip to content

Commit 63f4b29

Browse files
committed
use tokio process test command
1 parent d195aa8 commit 63f4b29

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

cmd/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ clap = { version = "4.5.28", features = ["derive"] }
1818
tokio = { version = "1.43.0", features = ["rt-multi-thread", "macros", "signal", "time", "sync", "io-std"] }
1919

2020
[dev-dependencies]
21-
assert_cmd = "2.0.16"
2221
assert_fs = "1.1.2"
23-
predicates = "3.1.3"
22+
tokio = { version = "1.43.0", features = ["process"] }
23+
axum = "0.8.1"

cmd/tests/command.rs

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,87 @@
1-
use assert_cmd::prelude::*;
21
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;
67

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")
1111
.stdin(Stdio::piped())
1212
.stdout(Stdio::piped())
1313
.spawn()
1414
.expect("Failed to spawn child process");
1515
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?;
2018
}
21-
let output = child.wait_with_output().expect("Failed to read stdout");
19+
let output = child.wait_with_output().await?;
2220
println!("output:{}", String::from_utf8_lossy(&output.stdout).trim());
2321
assert!(String::from_utf8_lossy(&output.stdout).trim() == "2\nnull");
2422
Ok(())
2523
}
2624

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");
3839
Ok(())
3940
}
4041

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+
});
4349
let temp = assert_fs::TempDir::new().unwrap();
44-
let file = temp.child(format!("request.{}", "fan"));
50+
let file = temp.child("request.fan");
4551
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
4955
Host: {host}
50-
Connection: close
5156
`[status == 200];
5257
5358
test call {
54-
let response = get();
59+
let response = hello();
5560
response.status
5661
}
5762
"#;
5863
file.write_str(text)?;
5964
// command test
60-
let mut command = Command::cargo_bin("basjoofan")?;
65+
let mut command = Command::new("basjoofan");
6166
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 ("));
6474
// command test call
65-
let mut command = Command::cargo_bin("basjoofan")?;
75+
let mut command = Command::new("basjoofan");
6676
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 ("));
6980
// command test blank
70-
let mut command = Command::cargo_bin("basjoofan")?;
81+
let mut command = Command::new("basjoofan");
7182
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");
7586
Ok(())
7687
}

0 commit comments

Comments
 (0)