Skip to content

Commit 5af3196

Browse files
committed
fix test new command base cargo bin
1 parent 63f4b29 commit 5af3196

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

cmd/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ tokio = { version = "1.43.0", features = ["rt-multi-thread", "macros", "signal",
2020
[dev-dependencies]
2121
assert_fs = "1.1.2"
2222
tokio = { version = "1.43.0", features = ["process"] }
23-
axum = "0.8.1"
23+
axum = "0.8.1"

cmd/tests/command.rs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use assert_fs::prelude::*;
22
use axum::routing::get;
33
use axum::Router;
4+
use std::path::PathBuf;
45
use std::process::Stdio;
56
use tokio::io::AsyncWriteExt;
67
use tokio::process::Command;
78

89
#[tokio::test]
910
async fn test_command_repl() -> Result<(), Box<dyn std::error::Error>> {
10-
let mut child = Command::new("basjoofan")
11-
.stdin(Stdio::piped())
12-
.stdout(Stdio::piped())
13-
.spawn()
14-
.expect("Failed to spawn child process");
11+
let mut command = new_command();
12+
command.stdout(Stdio::piped());
13+
command.stdin(Stdio::piped());
14+
let mut child = command.spawn().expect("Failed to spawn child process");
1515
if let Some(mut stdin) = child.stdin.take() {
1616
stdin.write_all("let x = 1 + 1; println(\"{x}\", x);\n".as_bytes()).await?;
1717
stdin.write_all("exit".as_bytes()).await?;
@@ -24,16 +24,20 @@ async fn test_command_repl() -> Result<(), Box<dyn std::error::Error>> {
2424

2525
#[tokio::test]
2626
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?;
27+
let mut command = new_command();
28+
let output = command.arg("eval").arg(r#"print("{integer}", 1 + 1 )"#).output().await?;
2929
assert!(output.status.success());
3030
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?;
31+
let mut command = new_command();
32+
let output = command.arg("eval").arg(r#"let x = 1 + 1; print("{integer}", x);"#).output().await?;
3333
assert!(output.status.success());
3434
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?;
35+
let mut command = new_command();
36+
let output = command
37+
.arg("eval")
38+
.arg(r#"println("{string}", "Hello Basjoofan!")"#)
39+
.output()
40+
.await?;
3741
assert!(output.status.success());
3842
assert_eq!(String::from_utf8(output.stdout)?, "Hello Basjoofan!\nnull\n");
3943
Ok(())
@@ -62,7 +66,7 @@ async fn test_command_test() -> Result<(), Box<dyn std::error::Error>> {
6266
"#;
6367
file.write_str(text)?;
6468
// command test
65-
let mut command = Command::new("basjoofan");
69+
let mut command = new_command();
6670
command.current_dir(&temp);
6771
let output = command.arg("test").output().await?;
6872
assert!(output.status.success());
@@ -72,16 +76,47 @@ async fn test_command_test() -> Result<(), Box<dyn std::error::Error>> {
7276
println!("stderr:{}", stderr);
7377
assert!(stdout.contains("--- PASS hello ("));
7478
// command test call
75-
let mut command = Command::new("basjoofan");
79+
let mut command = new_command();
7680
command.current_dir(&temp);
7781
let output = command.arg("test").arg("call").output().await?;
7882
assert!(output.status.success());
7983
assert!(String::from_utf8(output.stdout)?.contains("--- PASS hello ("));
8084
// command test blank
81-
let mut command = Command::new("basjoofan");
85+
let mut command = new_command();
8286
command.current_dir(&temp);
8387
let output = command.arg("test").arg("blank").output().await?;
8488
assert!(output.status.success());
8589
assert_eq!(String::from_utf8(output.stdout)?, "Test not found: blank\n");
8690
Ok(())
8791
}
92+
93+
fn new_command() -> Command {
94+
Command::new(cargo_bin("basjoofan"))
95+
}
96+
97+
/// Look up the path to a cargo-built binary within an integration test.
98+
fn cargo_bin<S: AsRef<str>>(name: S) -> PathBuf {
99+
cargo_bin_str(name.as_ref())
100+
}
101+
102+
fn cargo_bin_str(name: &str) -> PathBuf {
103+
let env_var = format!("CARGO_BIN_EXE_{}", name);
104+
std::env::var_os(env_var)
105+
.map(|p| p.into())
106+
.unwrap_or_else(|| target_dir().join(format!("{}{}", name, std::env::consts::EXE_SUFFIX)))
107+
}
108+
109+
// Adapted from
110+
// https://github.com/rust-lang/cargo/blob/485670b3983b52289a2f353d589c57fae2f60f82/tests/testsuite/support/mod.rs#L507
111+
fn target_dir() -> PathBuf {
112+
std::env::current_exe()
113+
.ok()
114+
.map(|mut path| {
115+
path.pop();
116+
if path.ends_with("deps") {
117+
path.pop();
118+
}
119+
path
120+
})
121+
.expect("this should only be used where a `current_exe` can be set")
122+
}

0 commit comments

Comments
 (0)