Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from waldirborbajr/gitvalidation
Rand Message: /malicious-woman
- Loading branch information
Showing
2 changed files
with
44 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
use anyhow::{anyhow, Result}; | ||
use std::process::Command; | ||
|
||
// Validate if git is installed. If not, return an error message. | ||
pub fn find_git_command() -> Result<(), String> { | ||
let gitcommand = Command::new("git").arg("--version").output().expect("Error"); | ||
|
||
match gitcommand.status.success() { | ||
true => Ok(()), | ||
false => Err("Error: 'git' command exited with non-zero status.".to_string()), | ||
} | ||
pub fn validate_git_command() -> Result<()> { | ||
Command::new("git") | ||
.arg("--version") | ||
.status() | ||
.map_err(|err| anyhow!("Failed to check git availability: {}", err)) | ||
.and_then( | ||
|status| { | ||
if status.success() { | ||
Ok(()) | ||
} else { | ||
Err(anyhow!("git command not found")) | ||
} | ||
}, | ||
) | ||
// let output = Command::new("git").arg("--version").output()?; | ||
// | ||
// if output.status.success() { | ||
// Ok(()) | ||
// } else { | ||
// Err(Error::msg("Git command not found")) | ||
// } | ||
} | ||
|
||
// #[cfg(test)] | ||
// mod tests { | ||
// use super::*; | ||
// | ||
// #[test] | ||
// fn test_validate_git_command() { | ||
// assert!(validate_git_command().is_ok()); | ||
// } | ||
// } | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_git_exists() { | ||
// Mock successful git execution | ||
Command::new("gitA").arg("--version").output().expect("Failed to mock git command"); | ||
|
||
assert!(find_git_command().is_ok()); | ||
fn test_validate_git_exists_success() { | ||
assert!(validate_git_command().is_ok()); // Assuming git is available | ||
} | ||
|
||
#[test] | ||
fn test_git_not_found() { | ||
// Mock unsuccessful git execution | ||
Command::new("invalid_command") | ||
.arg("--version") | ||
.output() | ||
.expect("Failed to mock invalid command"); | ||
#[should_panic] | ||
fn test_validate_git_exists_fail() { | ||
let mut cmd = Command::new("git"); | ||
cmd.env_remove("PATH"); // Simulate git not being available | ||
assert!(cmd.arg("--version").status().is_err()); | ||
|
||
assert!(find_git_command().is_err()); | ||
validate_git_command().unwrap(); // Should panic with a custom error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters