2019-06-10 14:56:17 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Read, Result};
|
2019-09-05 16:45:04 +00:00
|
|
|
use std::path::Path;
|
2019-11-27 22:03:08 +00:00
|
|
|
use std::process::Command;
|
2019-06-10 14:56:17 +00:00
|
|
|
|
|
|
|
/// Return the string contents of a file
|
2019-09-05 16:45:04 +00:00
|
|
|
pub fn read_file<P: AsRef<Path>>(file_name: P) -> Result<String> {
|
2019-06-10 14:56:17 +00:00
|
|
|
let mut file = File::open(file_name)?;
|
|
|
|
let mut data = String::new();
|
|
|
|
|
|
|
|
file.read_to_string(&mut data)?;
|
|
|
|
Ok(data)
|
|
|
|
}
|
2019-11-27 22:03:08 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CommandOutput {
|
|
|
|
pub stdout: String,
|
|
|
|
pub stderr: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for CommandOutput {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.stdout == other.stdout && self.stderr == other.stderr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a command and return the output on stdout and stderr if sucessful
|
2020-01-11 15:08:32 +00:00
|
|
|
#[cfg(not(test))]
|
2019-11-27 22:03:08 +00:00
|
|
|
pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
|
2020-01-11 15:08:32 +00:00
|
|
|
internal_exec_cmd(&cmd, &args)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
|
|
|
|
let command = match args.len() {
|
|
|
|
0 => String::from(cmd),
|
|
|
|
_ => format!("{} {}", cmd, args.join(" ")),
|
|
|
|
};
|
|
|
|
match command.as_str() {
|
2020-02-06 03:57:04 +00:00
|
|
|
"elm --version" => Some(CommandOutput {
|
|
|
|
stdout: String::from("0.19.1"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
2020-01-26 22:37:18 +00:00
|
|
|
"node --version" => Some(CommandOutput {
|
|
|
|
stdout: String::from("v12.0.0"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
2020-02-04 23:27:06 +00:00
|
|
|
"crystal --version" => Some(CommandOutput {
|
|
|
|
stdout: String::from("Crystal 0.32.1 (2019-12-18)"),
|
|
|
|
stderr: String::default(),
|
|
|
|
}),
|
2020-01-11 15:08:32 +00:00
|
|
|
"dummy_command" => Some(CommandOutput {
|
|
|
|
stdout: String::from("stdout ok!"),
|
|
|
|
stderr: String::from("stderr ok!"),
|
|
|
|
}),
|
|
|
|
// If we don't have a mocked command fall back to executing the command
|
|
|
|
_ => internal_exec_cmd(&cmd, &args),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn internal_exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
|
2019-11-27 22:03:08 +00:00
|
|
|
log::trace!("Executing command '{:?}' with args '{:?}'", cmd, args);
|
|
|
|
match Command::new(cmd).args(args).output() {
|
|
|
|
Ok(output) => {
|
|
|
|
let stdout_string = String::from_utf8(output.stdout).unwrap();
|
|
|
|
let stderr_string = String::from_utf8(output.stderr).unwrap();
|
|
|
|
|
|
|
|
if !output.status.success() {
|
|
|
|
log::trace!("Non-zero exit code '{:?}'", output.status.code());
|
|
|
|
log::trace!("stdout: {}", stdout_string);
|
|
|
|
log::trace!("stderr: {}", stderr_string);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(CommandOutput {
|
|
|
|
stdout: stdout_string,
|
|
|
|
stderr: stderr_string,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(not(windows))] // While the exec_cmd should work on Windows these tests assume a Unix-like environment.
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-01-11 15:08:32 +00:00
|
|
|
#[test]
|
|
|
|
fn exec_mocked_command() {
|
|
|
|
let result = exec_cmd("dummy_command", &[]);
|
|
|
|
let expected = Some(CommandOutput {
|
|
|
|
stdout: String::from("stdout ok!"),
|
|
|
|
stderr: String::from("stderr ok!"),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(result, expected)
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:03:08 +00:00
|
|
|
#[test]
|
|
|
|
fn exec_no_output() {
|
2020-01-11 15:08:32 +00:00
|
|
|
let result = internal_exec_cmd("true", &[]);
|
2019-11-27 22:03:08 +00:00
|
|
|
let expected = Some(CommandOutput {
|
|
|
|
stdout: String::from(""),
|
|
|
|
stderr: String::from(""),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(result, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exec_with_output_stdout() {
|
2020-02-04 17:48:01 +00:00
|
|
|
let result = internal_exec_cmd("/bin/sh", &["-c", "echo hello"]);
|
2019-11-27 22:03:08 +00:00
|
|
|
let expected = Some(CommandOutput {
|
2020-02-04 17:48:01 +00:00
|
|
|
stdout: String::from("hello\n"),
|
2019-11-27 22:03:08 +00:00
|
|
|
stderr: String::from(""),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(result, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exec_with_output_stderr() {
|
2020-01-11 15:08:32 +00:00
|
|
|
let result = internal_exec_cmd("/bin/sh", &["-c", "echo hello >&2"]);
|
2019-11-27 22:03:08 +00:00
|
|
|
let expected = Some(CommandOutput {
|
|
|
|
stdout: String::from(""),
|
|
|
|
stderr: String::from("hello\n"),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(result, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exec_with_output_both() {
|
2020-01-11 15:08:32 +00:00
|
|
|
let result = internal_exec_cmd("/bin/sh", &["-c", "echo hello; echo world >&2"]);
|
2019-11-27 22:03:08 +00:00
|
|
|
let expected = Some(CommandOutput {
|
|
|
|
stdout: String::from("hello\n"),
|
|
|
|
stderr: String::from("world\n"),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(result, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exec_with_non_zero_exit_code() {
|
2020-01-11 15:08:32 +00:00
|
|
|
let result = internal_exec_cmd("false", &[]);
|
2019-11-27 22:03:08 +00:00
|
|
|
let expected = None;
|
|
|
|
|
|
|
|
assert_eq!(result, expected)
|
|
|
|
}
|
|
|
|
}
|