command_display.rs (3319B)
1 #![forbid(unsafe_code)] 2 3 use std::path::Path; 4 use std::process::{Command, Output, Stdio}; 5 use std::time::{Duration, Instant}; 6 7 fn output(command: &mut Command, directory: &Path) -> Output { 8 let mut child = command 9 .current_dir(directory) 10 .stdin(Stdio::null()) 11 .stdout(Stdio::piped()) 12 .stderr(Stdio::piped()) 13 .spawn() 14 .unwrap(); 15 let deadline = Instant::now() + Duration::from_secs(10); 16 while child.try_wait().unwrap().is_none() { 17 if Instant::now() >= deadline { 18 child.kill().unwrap(); 19 child.wait().unwrap(); 20 panic!("command-display process exceeded its ten-second deadline"); 21 } 22 std::thread::sleep(Duration::from_millis(10)); 23 } 24 let output = child.wait_with_output().unwrap(); 25 assert!(output.stdout.len() <= 16 * 1024 && output.stderr.len() <= 16 * 1024); 26 output 27 } 28 29 #[test] 30 fn declared_help_succeeds_without_bootstrap_or_created_state() { 31 for arguments in [ 32 vec!["--help"], 33 vec!["config", "--help"], 34 vec!["state", "restore", "--help"], 35 ] { 36 let root = tempfile::tempdir().unwrap(); 37 let output = output( 38 Command::new(env!("CARGO_BIN_EXE_rhi")) 39 .arg("--config") 40 .arg(root.path().join("absent.toml")) 41 .args(&arguments), 42 root.path(), 43 ); 44 assert!(output.status.success(), "{arguments:?}"); 45 assert!(!output.stdout.is_empty()); 46 assert!(output.stderr.is_empty()); 47 assert!(!String::from_utf8_lossy(&output.stdout).contains("absent.toml")); 48 assert!(std::fs::read_dir(root.path()).unwrap().next().is_none()); 49 } 50 } 51 52 #[test] 53 fn help_is_bounded_static_and_diagnostics_remain_redacted() { 54 let error = rhi::parse_rhi_cli_v1_from([ 55 "caller-private-program", 56 "--instance", 57 "caller-private-instance", 58 "--config", 59 "/caller-private-path.toml", 60 "state", 61 "restore", 62 "--help", 63 ]) 64 .unwrap_err(); 65 assert_eq!(error.kind(), rhi::RhiCliV1ErrorKind::HelpRequested); 66 let help = error.help_text().unwrap(); 67 assert!(help.len() <= 16 * 1024 && help.contains("rhi state restore")); 68 assert!(!help.contains("caller-private")); 69 assert!(!format!("{error} {error:?}").contains("Usage:")); 70 let malformed = 71 rhi::parse_rhi_cli_v1_from(["caller-private-program", "--caller-private-invalid"]) 72 .unwrap_err(); 73 assert_eq!(malformed.kind(), rhi::RhiCliV1ErrorKind::InvalidArguments); 74 assert!(malformed.help_text().is_none()); 75 assert!(!format!("{malformed} {malformed:?}").contains("caller-private")); 76 } 77 78 #[test] 79 fn malformed_arguments_keep_the_exact_redacted_process_result() { 80 let root = tempfile::tempdir().unwrap(); 81 let output = output( 82 Command::new(env!("CARGO_BIN_EXE_rhi")).arg("--caller-private-invalid"), 83 root.path(), 84 ); 85 assert_eq!(output.status.code(), Some(2)); 86 assert!(output.stdout.is_empty()); 87 assert_eq!(output.stderr, b"{\"schema\":\"radroots.rhi.log.v1\",\"contract_version\":1,\"service\":\"rhi\",\"level\":\"error\",\"event\":\"process_result\",\"code\":\"input_or_configuration\",\"exit_code\":2}\n"); 88 assert!(std::fs::read_dir(root.path()).unwrap().next().is_none()); 89 }