Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streams the stdout and stderr of pre-commit execution #18

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/commit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::{anyhow, Result};

use std::fs;
use std::io::Write;
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;
use std::process::{exit, Command, Output};
use std::process::{exit, Command, Output, Stdio};
use std::{fs, thread};

pub fn git_exec(args: &[&str]) -> Result<Output> {
let output = Command::new("git").args(args).output();
Expand Down Expand Up @@ -61,16 +61,28 @@ pub fn write_cached_commit(commit_message: &str) -> Result<()> {
pub fn pre_commit_check(pre_commit_command: Option<String>, message: &str) -> Result<()> {
if let Some(command) = pre_commit_command {
println!("Running pre-commit command...");
let output = Command::new(command).env("MSG", message).output()?;
let message = String::from_utf8_lossy(&output.stdout);
println!("{message}");
if !output.status.success() {
let error_message = String::from_utf8_lossy(&output.stderr);
return Err(anyhow!(
"Pre-commit command failed with code: {}\n{}",
output.status.code().unwrap_or_default(),
error_message
));
let mut process = Command::new(command)
.env("MSG", message)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let stdout = process.stdout.take().expect("Unable to get stdout");
let stderr = process.stderr.take().expect("Unable to get stderr");
thread::spawn(move || {
let lines = BufReader::new(stdout).lines();
for line in lines {
println!("{}", line.expect("Unable to get line"));
}
});
thread::spawn(move || {
let lines = BufReader::new(stderr).lines();
for line in lines {
eprintln!("{}", line.expect("Unable to get line"));
}
});
let status = process.wait()?;
if !status.success() {
return Err(anyhow!("Pre-commit command failed with {}", status));
}
}
Ok(())
Expand Down
12 changes: 1 addition & 11 deletions src/config/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::env::{current_dir, set_current_dir};
use std::env::set_current_dir;

use super::*;
use assert_fs::prelude::*;
Expand All @@ -13,7 +13,6 @@ fn select_custom_config_path_test() -> Result<()> {
let selected_config_path = select_custom_config_path(config_path.clone())?;
assert_eq!(config_path.unwrap().to_str(), selected_config_path.to_str());

let last_dir = current_dir()?;
set_current_dir(temp_dir.path())?;
let config_path_default = dirs::config_dir().unwrap().join("commit/commit.json");
let selected_config_path = select_custom_config_path(None)?;
Expand All @@ -24,23 +23,18 @@ fn select_custom_config_path_test() -> Result<()> {
Err(err) => assert_eq!(err.to_string(), "Config file does not exist: "),
_ => unreachable!(),
}
set_current_dir(last_dir)?;
temp_dir.close()?;
Ok(())
}

#[test]
fn get_config_path_test() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let last_dir = current_dir()?;
set_current_dir(temp_dir.path())?;
let config_file = dirs::config_dir()
.ok_or_else(|| anyhow!("Could not find config directory"))?
.join("commit/commit.json");
let config_path = get_config_path();
assert_eq!(config_file.to_str(), config_path?.to_str());
set_current_dir(last_dir)?;
temp_dir.close()?;
Ok(())
}

Expand All @@ -57,22 +51,18 @@ fn get_config_path_content_test() -> Result<()> {
config_file.write_str(expected)?;
let content = get_config_path_content(config_path)?;
assert_eq!(content, expected);
temp_dir.close()?;
Ok(())
}

#[test]
fn get_pattern_test() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let last_dir = current_dir()?;
set_current_dir(temp_dir.path())?;
let pattern = get_pattern(None)?;
assert_eq!(pattern.config.type_prefix, None);
assert_eq!(pattern.config.type_suffix, None);
assert_eq!(pattern.config.subject_separator, ": ");
assert_eq!(pattern.config.scope_prefix, "(");
assert_eq!(pattern.config.scope_suffix, ")");
set_current_dir(last_dir)?;
temp_dir.close()?;
Ok(())
}
Loading