Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Nov 21, 2024
1 parent 8dc9937 commit ce14163
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub(crate) async fn run(
// Clear any unstaged changes from the git working directory.
let mut _guard = None;
if should_stash {
_guard = Some(WorkTreeKeeper::clean(&store, printer).await?);
_guard = Some(WorkTreeKeeper::clean(&store).await?);
}

let mut filenames = all_filenames(
Expand Down
41 changes: 20 additions & 21 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Arc, Mutex};

use anstream::ColorChoice;
use anstream::{eprintln, ColorChoice};
use anyhow::Result;
use fancy_regex::{self as regex, Regex};
use owo_colors::{OwoColorize, Style};
Expand Down Expand Up @@ -168,7 +168,7 @@ pub async fn run_hooks(
ColorChoice::Always | ColorChoice::AlwaysAnsi => "--color=always",
ColorChoice::Never => "--color=never",
};
git::git_cmd("git diff")?
git_cmd("git diff")?
.arg("--no-pager")
.arg("diff")
.arg("--no-ext-diff")
Expand Down Expand Up @@ -486,16 +486,16 @@ impl IntentToAddKeeper {
impl Drop for IntentToAddKeeper {
fn drop(&mut self) {
if let Err(err) = self.restore() {
let _ = writeln!(
std::io::stderr(),
"Failed to restore intent-to-add changes: {err}"
eprintln!(
"{}",
format!("Failed to restore intent-to-add changes: {err}").red()
);
}
}
}

impl WorkingTreeKeeper {
async fn clean(patch_dir: &Path, printer: Printer) -> Result<Self> {
async fn clean(patch_dir: &Path) -> Result<Self> {
let tree = git::write_tree().await?;

let mut cmd = git_cmd("git diff-index")?;
Expand Down Expand Up @@ -531,15 +531,14 @@ impl WorkingTreeKeeper {
);
let patch_path = patch_dir.join(&patch_name);

writeln!(
printer.stdout(),
eprintln!(
"{}",
format!(
"Non-staged changes detected, saving to `{}`",
patch_path.user_display()
)
.yellow()
)?;
);
fs_err::create_dir_all(patch_dir)?;
fs_err::write(&patch_path, output.stdout)?;

Expand Down Expand Up @@ -595,34 +594,34 @@ impl WorkingTreeKeeper {
// Try to apply the patch
if Self::git_apply(patch).is_err() {
error!("Failed to apply the patch, rolling back changes");
writeln!(
std::io::stderr(),
"Failed to apply the patch, rolling back changes"
)?;
eprintln!(
"{}",
"Failed to apply the patch, rolling back changes".red()
);

Self::checkout_working_tree()?;
Self::git_apply(patch)?;
};

writeln!(
std::io::stderr(),
eprintln!(
"{}",
format!(
"\nRestored working tree changes from `{}`",
patch.user_display()
)
.yellow()
)?;
);

Ok(())
}
}

impl Drop for WorkingTreeKeeper {
fn drop(&mut self) {
if let Err(err) = self.restore() {
let _ = writeln!(
std::io::stderr(),
"Failed to restore working tree changes: {err}"
eprintln!(
"{}",
format!("Failed to restore working tree changes: {err}").red()
);
}
}
Expand Down Expand Up @@ -650,10 +649,10 @@ impl Drop for RestoreGuard {
impl WorkTreeKeeper {
/// Clear intent-to-add changes from the index and clear the non-staged changes from the working directory.
/// Restore them when the instance is dropped.
pub async fn clean(store: &Store, printer: Printer) -> Result<RestoreGuard> {
pub async fn clean(store: &Store) -> Result<RestoreGuard> {
let cleaner = Self {
intent_to_add: Some(IntentToAddKeeper::clean().await?),
working_tree: Some(WorkingTreeKeeper::clean(store.path(), printer).await?),
working_tree: Some(WorkingTreeKeeper::clean(store.path()).await?),
};

// Set to the global for the cleanup hook.
Expand Down
6 changes: 4 additions & 2 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,15 @@ fn staged_files_only() -> Result<()> {
success: true
exit_code: 0
----- stdout -----
Non-staged changes detected, saving to [HOME]/1732193790067-10870.patch
trailing-whitespace......................................................Passed
- hook id: trailing-whitespace
- duration: 0.04s
- duration: 0.03s
Hello, world!
----- stderr -----
Non-staged changes detected, saving to `[HOME]/1732196801370-22887.patch`
Restored working tree changes from `[HOME]/1732196801370-22887.patch`
"#);

let content = context.read("file.txt");
Expand Down

0 comments on commit ce14163

Please sign in to comment.