diff options
Diffstat (limited to 'ofborg/tickborg/src/evalchecker.rs')
| -rw-r--r-- | ofborg/tickborg/src/evalchecker.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/ofborg/tickborg/src/evalchecker.rs b/ofborg/tickborg/src/evalchecker.rs new file mode 100644 index 0000000000..ac1b4f8d39 --- /dev/null +++ b/ofborg/tickborg/src/evalchecker.rs @@ -0,0 +1,62 @@ +use std::fs::File; +use std::io::Write; +use std::path::Path; +use std::process::Command; + +/// A generic check that can be run against a checkout +pub struct EvalChecker { + name: String, + command: String, + args: Vec<String>, +} + +impl EvalChecker { + pub fn new(name: &str, command: &str, args: Vec<String>) -> EvalChecker { + EvalChecker { + name: name.to_owned(), + command: command.to_owned(), + args, + } + } + + pub fn name(&self) -> &str { + &self.name + } + + pub fn execute(&self, path: &Path) -> Result<File, File> { + let output = Command::new(&self.command) + .args(&self.args) + .current_dir(path) + .output(); + + let tmp = tempfile::NamedTempFile::new().expect("Failed to create temp file"); + let tmp_path = tmp.into_temp_path().to_path_buf(); + + match output { + Ok(result) => { + let mut f = File::create(&tmp_path).expect("Failed to create output file"); + f.write_all(&result.stdout).ok(); + f.write_all(&result.stderr).ok(); + drop(f); + let file = File::open(&tmp_path).expect("Failed to open output file"); + if result.status.success() { + Ok(file) + } else { + Err(file) + } + } + Err(e) => { + let mut f = File::create(&tmp_path).expect("Failed to create output file"); + write!(f, "Failed to execute {}: {}", self.command, e).ok(); + drop(f); + Err(File::open(&tmp_path).expect("Failed to open output file")) + } + } + } + + pub fn cli_cmd(&self) -> String { + let mut cli = vec![self.command.clone()]; + cli.append(&mut self.args.clone()); + cli.join(" ") + } +} |
