Skip to content

Commit

Permalink
Merge pull request #44 from waldirborbajr/gitvalidation
Browse files Browse the repository at this point in the history
Rand Message: /malicious-woman
  • Loading branch information
waldirborbajr committed Feb 15, 2024
2 parents c9ef84d + 17ed99b commit 7f3dec6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
59 changes: 40 additions & 19 deletions src/lib.rs
@@ -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
}
}
11 changes: 4 additions & 7 deletions src/main.rs
Expand Up @@ -5,16 +5,15 @@ mod version;
use anyhow::Result;
use colorful::Colorful;
use git::gitpush;
use gitpushup::find_git_command;
use gitpushup::validate_git_command;
use randmessage::rand_message;
use version::show_version;

fn main() -> Result<()> {
let status = find_git_command();
match status {
Ok(_) => (),
match validate_git_command() {
Ok(_) => {}
Err(_) => {
eprintln!("{} {}", "🛑 git".red().bold(), "not found. Please install before using".red());
eprintln!("{}", "🛑 Error: Git command not found".red().bold());
std::process::exit(1)
}
}
Expand All @@ -32,7 +31,5 @@ fn main() -> Result<()> {
// add + commit + push
gitpush(&message);

// std::process::exit(0)

Ok(())
}

0 comments on commit 7f3dec6

Please sign in to comment.