rainbow_dash/
lib.rs

1#![deny(missing_docs)]
2#![doc = include_str!("../readme.md")]
3
4use std::io::Result;
5use std::process::{Command, ExitStatus, Output};
6
7/// Executes a Linux/macOS or Windows command with a Result of ExitStatus.
8pub fn execute_command(cmd: &str) -> Result<ExitStatus> {
9	#[cfg(not(target_os = "windows"))]
10	return execute_unix_command(cmd);
11	#[cfg(target_os = "windows")]
12	return execute_windows_command(cmd);
13}
14
15/// Executes a Linux/macOS or Windows command with a Result of the output.
16pub fn execute_command_with_return(cmd: &str) -> Result<Output> {
17	#[cfg(not(target_os = "windows"))]
18	return execute_unix_command_with_return(cmd);
19	#[cfg(target_os = "windows")]
20	return execute_windows_command_with_return(cmd);
21}
22
23/// Executes a Windows command with a Result of ExitStatus.
24pub fn execute_windows_command(cmd: &str) -> Result<ExitStatus> {
25	Command::new("cmd").args(["/C", cmd]).status()
26}
27
28/// Executes a Linux/macOS command with a Result of ExitStatus.
29pub fn execute_unix_command(cmd: &str) -> Result<ExitStatus> {
30	Command::new("sh").arg("-c").arg(cmd).status()
31}
32
33/// Executes a Windows command with a Result of the output.
34pub fn execute_windows_command_with_return(cmd: &str) -> Result<Output> {
35	Command::new("cmd").args(["/C", cmd]).output()
36}
37
38/// Executes a Linux/macOS command with a Result of the output.
39pub fn execute_unix_command_with_return(cmd: &str) -> Result<Output> {
40	Command::new("sh").arg("-c").arg(cmd).output()
41}
42
43#[cfg(test)]
44mod tests {
45	use super::*;
46	use std::error::Error;
47	#[test]
48	fn command_status() -> std::result::Result<(), Box<dyn Error>> {
49		let status = execute_command("exit 0")?;
50		assert_eq!(status.success(), true);
51		Ok(())
52	}
53	#[test]
54	fn command_status_failure() -> std::result::Result<(), Box<dyn Error>> {
55		let status = execute_command("exit 1")?;
56		assert_eq!(status.success(), false);
57		Ok(())
58	}
59	#[test]
60	fn command_output() -> std::result::Result<(), Box<dyn Error>> {
61		let answer = "Pinkie Pie is best pony!";
62		let output = execute_command_with_return(&format!("echo '{answer}'"))?;
63		assert_eq!(String::from_utf8(output.stdout)?, format!("{answer}\n"));
64		Ok(())
65	}
66	#[test]
67	#[cfg(not(target_os = "windows"))]
68	fn unix_status() -> std::result::Result<(), Box<dyn Error>> {
69		let status = execute_unix_command("exit 0")?;
70		assert_eq!(status.success(), true);
71		Ok(())
72	}
73	#[test]
74	#[cfg(not(target_os = "windows"))]
75	fn unix_status_failure() -> std::result::Result<(), Box<dyn Error>> {
76		let status = execute_unix_command("exit 1")?;
77		assert_eq!(status.success(), false);
78		Ok(())
79	}
80	#[test]
81	#[cfg(target_os = "windows")]
82	fn windows_status() -> std::result::Result<(), Box<dyn Error>> {
83		let status = execute_windows_command("exit 0")?;
84		assert_eq!(status.success(), true);
85		Ok(())
86	}
87	#[test]
88	#[cfg(target_os = "windows")]
89	fn windows_status_failure() -> std::result::Result<(), Box<dyn Error>> {
90		let status = execute_windows_command("exit 1")?;
91		assert_eq!(status.success(), false);
92		Ok(())
93	}
94	#[test]
95	#[cfg(not(target_os = "windows"))]
96	fn unix_output() -> std::result::Result<(), Box<dyn Error>> {
97		let answer = "Pinkie Pie is best pony!";
98		let output = execute_unix_command_with_return(&format!("echo '{answer}'"))?;
99		assert_eq!(String::from_utf8(output.stdout)?, format!("{answer}\n"));
100		Ok(())
101	}
102	#[test]
103	#[cfg(target_os = "windows")]
104	fn windows_output() -> std::result::Result<(), Box<dyn Error>> {
105		let answer = "Pinkie Pie is best pony!";
106		let output = execute_windows_command_with_return(&format!("echo '{answer}'"))?;
107		assert_eq!(String::from_utf8(output.stdout)?, format!("{answer}\n"));
108		Ok(())
109	}
110}