-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.rs
More file actions
77 lines (64 loc) · 2.12 KB
/
test_utils.rs
File metadata and controls
77 lines (64 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::fs;
use std::path::PathBuf;
use std::process::Command;
pub fn get_binary_path() -> PathBuf {
// In tests, the binary is in target/debug/mq
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("target");
path.push("debug");
path.push("mq");
// If not found, try deps directory (for test builds)
if !path.exists() {
let mut deps_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
deps_path.push("target");
deps_path.push("debug");
deps_path.push("deps");
deps_path.push("mq");
if deps_path.exists() {
return deps_path;
}
}
path
}
pub fn run_mq_with_config(config_content: &str, subcommand: &str) -> (i32, String, String) {
run_mq_with_config_and_args(config_content, subcommand, &[])
}
pub fn run_mq_with_config_and_args(
config_content: &str,
subcommand: &str,
args: &[&str],
) -> (i32, String, String) {
let binary = get_binary_path();
if !binary.exists() {
panic!("Binary not found at {:?}. Run 'cargo build' first.", binary);
}
// Create a temporary directory for the test with unique name
let temp_dir = std::env::temp_dir().join(format!(
"mq_test_{}_{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir_all(&temp_dir).unwrap();
// Create .config directory
let config_dir = temp_dir.join(".config");
fs::create_dir_all(&config_dir).unwrap();
// Write the config file
let config_file = config_dir.join("mq.toml");
fs::write(&config_file, config_content).unwrap();
// Run the binary
let mut cmd = Command::new(&binary);
cmd.arg(subcommand);
cmd.args(args);
cmd.current_dir(&temp_dir);
let output = cmd.output().expect("Failed to execute binary");
// Clean up
let _ = fs::remove_dir_all(&temp_dir);
(
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stdout).to_string(),
String::from_utf8_lossy(&output.stderr).to_string(),
)
}