|
10 | 10 | //! |
11 | 11 | //! Tests for compile command and subcommands |
12 | 12 |
|
13 | | -use std::process::{Command, Output}; |
| 13 | +use std::process::Command; |
14 | 14 |
|
15 | | -fn run_cmd(cmd: &str) -> Output { |
16 | | - Command::new("cargo") |
17 | | - .args(format!("run -- {}", cmd).split_whitespace()) |
18 | | - .output() |
19 | | - .unwrap() |
| 15 | +fn run_cmd(cmd: &str) -> Result<String, String> { |
| 16 | + let full_cmd = format!("run --features compiler -- {}", cmd); |
| 17 | + let args = shlex::split(&full_cmd).unwrap(); |
| 18 | + |
| 19 | + let output = Command::new("cargo").args(args).output().unwrap(); |
| 20 | + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 21 | + let stderr = String::from_utf8_lossy(&output.stderr).to_string(); |
| 22 | + |
| 23 | + if output.status.success() { |
| 24 | + Ok(stdout) |
| 25 | + } else { |
| 26 | + Err(stderr) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn test_compile_taproot() { |
| 32 | + let stdout = run_cmd(r#"compile "pk(ABC)" -t tr"#).unwrap(); |
| 33 | + let json: serde_json::Value = serde_json::from_str(&stdout).unwrap(); |
| 34 | + |
| 35 | + assert!(json.get("descriptor").is_some()); |
| 36 | + assert!(json.get("r").is_some()); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn test_compile_sh() { |
| 41 | + let stdout = run_cmd(r#"compile "pk(ABC)" -t sh"#).unwrap(); |
| 42 | + let json: serde_json::Value = serde_json::from_str(&stdout).unwrap(); |
| 43 | + |
| 44 | + assert!(json.get("descriptor").is_some()); |
| 45 | + assert!(json.get("r").is_none()); |
20 | 46 | } |
21 | 47 |
|
22 | 48 | #[test] |
23 | 49 | fn test_invalid_cases() { |
24 | 50 | // Test invalid policy syntax |
25 | | - let output = run_cmd("compile invalid_policy"); |
26 | | - assert!(!output.status.success()); |
| 51 | + let stderr = run_cmd(r#"compile "invalid_policy""#).unwrap_err(); |
| 52 | + assert!(stderr.contains("Miniscript error")); |
27 | 53 |
|
28 | 54 | // Test invalid script type |
29 | | - let output = run_cmd("compile pk(A) -t invalid_type"); |
30 | | - assert!(!output.status.success()); |
| 55 | + let stderr = run_cmd(r#"compile "pk(A)" -t invalid_type"#).unwrap_err(); |
| 56 | + assert!(stderr.contains("error: invalid value 'invalid_type' for '--type <SCRIPT_TYPE>'")); |
31 | 57 |
|
32 | 58 | // Test empty policy |
33 | | - let output = run_cmd("compile"); |
34 | | - assert!(!output.status.success()); |
| 59 | + let stderr = run_cmd("compile").unwrap_err(); |
| 60 | + assert!(stderr.contains("error: the following required arguments were not provided")); |
| 61 | + assert!(stderr.contains("<POLICY>")); |
35 | 62 |
|
36 | 63 | // Test malformed policy with unmatched parentheses |
37 | | - let output = run_cmd(r#"compile "pk(A"#); |
38 | | - assert!(!output.status.success()); |
| 64 | + let stderr = run_cmd(r#"compile "pk(A""#).unwrap_err(); |
| 65 | + assert!(stderr.contains("Miniscript error: expected )")); |
39 | 66 |
|
40 | 67 | // Test policy with unknown function |
41 | | - let output = run_cmd(r#"compile "unknown_func(A)"#); |
42 | | - assert!(!output.status.success()); |
| 68 | + let stderr = run_cmd(r#"compile "unknown_func(A)""#).unwrap_err(); |
| 69 | + assert!(stderr.contains("Miniscript error: unexpected «unknown_func»")); |
43 | 70 | } |
0 commit comments