diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/cli.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/cli.rs b/tests/cli.rs new file mode 100644 index 0000000..04fd637 --- /dev/null +++ b/tests/cli.rs | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | use std::fs; | ||
| 2 | use std::path::PathBuf; | ||
| 3 | use std::process::Command; | ||
| 4 | |||
| 5 | fn agent() -> Command { | ||
| 6 | Command::new(env!("CARGO_BIN_EXE_agent686")) | ||
| 7 | } | ||
| 8 | |||
| 9 | fn temp_home(tag: &str) -> PathBuf { | ||
| 10 | let dir = std::env::temp_dir().join(format!("agent686-cli-{tag}-{}", std::process::id())); | ||
| 11 | let _ = fs::remove_dir_all(&dir); | ||
| 12 | fs::create_dir_all(&dir).unwrap(); | ||
| 13 | dir | ||
| 14 | } | ||
| 15 | |||
| 16 | #[test] | ||
| 17 | fn a_flag_without_a_value_is_a_usage_error() { | ||
| 18 | for flag in ["--session", "-m", "--base-url"] { | ||
| 19 | let output = agent().arg(flag).output().unwrap(); | ||
| 20 | assert_eq!(output.status.code(), Some(2), "{flag}"); | ||
| 21 | let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 22 | assert!(stderr.contains("needs a value"), "{flag}: {stderr}"); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | #[test] | ||
| 27 | fn double_dash_makes_everything_a_prompt() { | ||
| 28 | let home = temp_home("ddash"); | ||
| 29 | // Without `--` this would print the version and exit 0. | ||
| 30 | let output = agent() | ||
| 31 | .env("HOME", &home) | ||
| 32 | .env_remove("OPENCODE_API_KEY") | ||
| 33 | .args(["--", "--version"]) | ||
| 34 | .output() | ||
| 35 | .unwrap(); | ||
| 36 | |||
| 37 | assert_eq!(output.status.code(), Some(2)); | ||
| 38 | let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 39 | assert!(stderr.contains("no API key"), "{stderr}"); | ||
| 40 | assert!(!String::from_utf8_lossy(&output.stdout).contains("agent686 0")); | ||
| 41 | } | ||
| 42 | |||
| 43 | #[test] | ||
| 44 | fn session_ids_cannot_escape_the_sessions_directory() { | ||
| 45 | for id in ["../escape", "a/b", "x".repeat(65).as_str()] { | ||
| 46 | let output = agent().args(["--session", id]).output().unwrap(); | ||
| 47 | assert_eq!(output.status.code(), Some(2), "{id:?}"); | ||
| 48 | let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 49 | assert!(stderr.contains("invalid session id"), "{id:?}: {stderr}"); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | #[test] | ||
| 54 | fn conflicting_flags_are_rejected() { | ||
| 55 | let output = agent() | ||
| 56 | .args(["-c", "--session", "abc"]) | ||
| 57 | .output() | ||
| 58 | .unwrap(); | ||
| 59 | assert_eq!(output.status.code(), Some(2)); | ||
| 60 | assert!(String::from_utf8_lossy(&output.stderr).contains("cannot be combined")); | ||
| 61 | |||
| 62 | let output = agent().args(["--list", "--doctor"]).output().unwrap(); | ||
| 63 | assert_eq!(output.status.code(), Some(2)); | ||
| 64 | assert!(String::from_utf8_lossy(&output.stderr).contains("cannot be combined")); | ||
| 65 | } | ||
| 66 | |||
| 67 | #[test] | ||
| 68 | fn unknown_flags_go_to_stderr_with_usage() { | ||
| 69 | let output = agent().arg("--nope").output().unwrap(); | ||
| 70 | assert_eq!(output.status.code(), Some(2)); | ||
| 71 | assert!( | ||
| 72 | String::from_utf8_lossy(&output.stdout).is_empty(), | ||
| 73 | "usage must not pollute stdout" | ||
| 74 | ); | ||
| 75 | let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 76 | assert!(stderr.contains("unknown flag"), "{stderr}"); | ||
| 77 | assert!(stderr.contains("USAGE"), "{stderr}"); | ||
| 78 | } | ||
