From 89cd5232cfd12e220f77cb96ec9fd42a3ab4a98d Mon Sep 17 00:00:00 2001 From: Steppy Date: Wed, 5 Feb 2025 23:56:44 +0100 Subject: [PATCH] Remove old command module --- README.md | 4 - src/command.rs | 188 ------------------------------- src/main.rs | 1 - test-ressources/python/exit_1.py | 1 - 4 files changed, 194 deletions(-) delete mode 100644 src/command.rs delete mode 100644 test-ressources/python/exit_1.py diff --git a/README.md b/README.md index 2cded86..a0a973a 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,3 @@ Once you have that installed, just run cargo build --release ``` and you will find an executable in `target/release`. - -### Unit tests - -In order for the unit tests to pass, you will need `python3`. diff --git a/src/command.rs b/src/command.rs deleted file mode 100644 index 8d76811..0000000 --- a/src/command.rs +++ /dev/null @@ -1,188 +0,0 @@ -#[deprecated] - -use crate::log; -use crate::logger::{LogLevel, Logger}; -use crate::shell_interface::command_to_string; -use std::error::Error; -use std::fmt::{Debug, Display, Formatter}; -use std::io; -use std::process::{Command, ExitStatus, Output}; - -pub trait LogRunnable { - fn run(&mut self, logger: &Logger) -> Result<(), CommandSpecificError>; - fn collect_output(&mut self) -> Result>; - fn collect_full_output(&mut self) -> Result>; -} - -impl LogRunnable for Command { - fn run(&mut self, logger: &Logger) -> Result<(), CommandSpecificError> { - run(self, logger).map_err(|error| CommandSpecificError { - command: self, - error, - }) - } - - fn collect_output(&mut self) -> Result> { - collect_output(self, None).map_err(|error| CommandSpecificError { - command: self, - error, - }) - } - - fn collect_full_output(&mut self) -> Result> { - collect_full_output(self).map_err(|error| CommandSpecificError { - command: self, - error, - }) - } -} - -fn run(command: &mut Command, logger: &Logger) -> Result<(), ExecutionError> { - match logger.level { - LogLevel::Debug | LogLevel::Info => { - let status = command.status()?; - if !status.success() { - Err(status)?; - } - } - LogLevel::Error => { - collect_output(command, Some(logger))?; - } - } - Ok(()) -} - -fn collect_output( - command: &mut Command, - logger: Option<&Logger>, -) -> Result { - let output = collect_full_output(command)?; - if !output.status.success() { - if let Some(logger) = logger { - log!(logger, error, "{}", String::from_utf8_lossy(&output.stdout)); - log!(logger, error, "{}", String::from_utf8_lossy(&output.stderr)); - } - Err(output.status)?; - } - Ok(output) -} - -fn collect_full_output(command: &mut Command) -> Result { - Ok(command.output()?) -} - -#[derive(Debug)] -pub struct CommandSpecificError<'a, E> { - pub command: &'a Command, - pub error: E, -} - -impl Display for CommandSpecificError<'_, E> -where - E: Display, -{ - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!( - f, - "Failed to execute command '{}': {}", - command_to_string(self.command), - self.error - ) - } -} - -impl Error for CommandSpecificError<'_, E> where E: Debug + Display {} - -#[derive(Debug)] -pub struct StartError(io::Error); - -impl From for StartError { - fn from(value: io::Error) -> Self { - StartError(value) - } -} - -impl Display for StartError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "Failed to start command: {}", self.0) - } -} - -impl Error for StartError {} - -#[derive(Debug)] -pub enum ExecutionError { - StartError(StartError), - BadExitStatus(ExitStatus), -} - -impl From for ExecutionError { - fn from(value: io::Error) -> Self { - Self::StartError(StartError(value)) - } -} - -impl From for ExecutionError { - fn from(value: StartError) -> Self { - Self::StartError(value) - } -} - -impl From for ExecutionError { - fn from(value: ExitStatus) -> Self { - Self::BadExitStatus(value) - } -} - -impl Display for ExecutionError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self { - ExecutionError::StartError(e) => Display::fmt(e, f), - ExecutionError::BadExitStatus(status) => write!(f, "Command failed with {}", status), - } - } -} - -impl Error for ExecutionError {} - -#[cfg(test)] -mod test { - use crate::command::{CommandSpecificError, ExecutionError, LogRunnable}; - use crate::logger::Logger; - use std::path::PathBuf; - use std::process::Command; - - #[test] - fn test_unknown_command() { - let mut command = Command::new("python7"); - let Err( - e @ CommandSpecificError { - error: ExecutionError::StartError(_), - .. - }, - ) = command - .args([PathBuf::from("test-ressources/python/exit_1.py")]) - .run(&Logger::default()) - else { - panic!("command shouldn't exist"); - }; - assert_eq!(e.to_string(), "Failed to execute command 'python7': Failed to start command: No such file or directory (os error 2)"); - } - - #[test] - fn test_error() { - let mut command = Command::new("python3"); - let Err( - e @ CommandSpecificError { - error: ExecutionError::BadExitStatus(_), - .. - }, - ) = command - .arg("test-ressources/python/exit_1.py") - .run(&Logger::default()) - else { - panic!("command should return exit-code 1") - }; - assert_eq!(e.to_string(), "Failed to execute command 'python3 test-ressources/python/exit_1.py': Command failed with exit status: 1"); - } -} diff --git a/src/main.rs b/src/main.rs index 199d4c7..de5d64b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ mod action; -mod command; mod environment; mod file; mod logger; diff --git a/test-ressources/python/exit_1.py b/test-ressources/python/exit_1.py deleted file mode 100644 index b11a11b..0000000 --- a/test-ressources/python/exit_1.py +++ /dev/null @@ -1 +0,0 @@ -exit(1) \ No newline at end of file