Skip to content

Commit

Permalink
Added support for hiding diffs / showing snapshots in review
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Feb 15, 2023
1 parent da63687 commit a2cda1a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
24 changes: 21 additions & 3 deletions cargo-insta/src/cli.rs
Expand Up @@ -8,8 +8,7 @@ use std::{io, process};
use console::{set_colors_enabled, style, Key, Term};
use insta::Snapshot;
use insta::_cargo_insta_support::{
is_ci, print_snapshot, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig,
UnreferencedSnapshots,
is_ci, SnapshotPrinter, SnapshotUpdate, TestRunner, ToolConfig, UnreferencedSnapshots,
};
use serde::Serialize;
use structopt::clap::AppSettings;
Expand Down Expand Up @@ -228,6 +227,7 @@ fn query_snapshot(
n: usize,
snapshot_file: Option<&Path>,
show_info: &mut bool,
show_diff: &mut bool,
) -> Result<Operation, Box<dyn Error>> {
loop {
term.clear_screen()?;
Expand All @@ -250,6 +250,8 @@ fn query_snapshot(
printer.set_snapshot_file(snapshot_file);
printer.set_line(line);
printer.set_show_info(*show_info);
printer.set_show_diff(*show_diff);
printer.print();

println!();
println!(
Expand All @@ -273,6 +275,12 @@ fn query_snapshot(
if *show_info { "hide" } else { "show" },
style("toggles extended snapshot info").dim()
);
println!(
" {} {} diff {}",
style("d").cyan().bold(),
if *show_diff { "hide" } else { "show" },
style("toggle snapshot diff").dim()
);

loop {
match term.read_key()? {
Expand All @@ -283,6 +291,10 @@ fn query_snapshot(
*show_info = !*show_info;
break;
}
Key::Char('d') => {
*show_diff = !*show_diff;
break;
}
_ => {}
}
}
Expand Down Expand Up @@ -424,6 +436,7 @@ fn process_snapshots(
let mut skipped = vec![];
let mut num = 0;
let mut show_info = true;
let mut show_diff = true;

for (snapshot_container, package) in snapshot_containers.iter_mut() {
let target_file = snapshot_container.target_file().to_path_buf();
Expand Down Expand Up @@ -456,6 +469,7 @@ fn process_snapshots(
snapshot_count,
snapshot_file.as_ref().map(|x| x.as_path()),
&mut show_info,
&mut show_diff,
)?,
};
match op {
Expand Down Expand Up @@ -880,7 +894,11 @@ fn prepare_test_runner<'snapshot_ref>(
fn show_cmd(cmd: ShowCommand) -> Result<(), Box<dyn Error>> {
let loc = handle_target_args(&cmd.target_args)?;
let snapshot = Snapshot::from_file(&cmd.path)?;
print_snapshot(&loc.workspace_root, &snapshot, Some(&cmd.path), None, true);
let mut printer = SnapshotPrinter::new(&loc.workspace_root, None, &snapshot);
printer.set_snapshot_file(Some(&cmd.path));
printer.set_show_info(true);
printer.set_show_diff(false);
printer.print();
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -295,7 +295,7 @@ pub mod _cargo_insta_support {
Error as ToolConfigError, OutputBehavior, SnapshotUpdate, TestRunner, ToolConfig,
UnreferencedSnapshots,
},
output::{print_snapshot, SnapshotPrinter},
output::SnapshotPrinter,
snapshot::PendingInlineSnapshot,
snapshot::SnapshotContents,
utils::is_ci,
Expand Down
52 changes: 22 additions & 30 deletions src/output.rs
Expand Up @@ -80,7 +80,11 @@ impl<'a> SnapshotPrinter<'a> {

fn print_snapshot_diff(&self) {
self.print_snapshot_summary();
self.print_changeset();
if self.show_diff {
self.print_changeset();
} else {
self.print_snapshot();
}
}

fn print_snapshot_summary(&self) {
Expand All @@ -96,6 +100,23 @@ impl<'a> SnapshotPrinter<'a> {
print_info(self.new_snapshot.metadata());
}

fn print_snapshot(&self) {
print_line(term_width());

let new_contents = self.new_snapshot.contents_str();

let width = term_width();
if self.show_info {
self.print_info();
}
println!("Snapshot Contents:");
println!("──────┬{:─^1$}", "", width.saturating_sub(13));
for (idx, line) in new_contents.lines().enumerate() {
println!("{:>5} │ {}", style(idx + 1).cyan().dim().bold(), line);
}
println!("──────┴{:─^1$}", "", width.saturating_sub(13),);
}

fn print_changeset(&self) {
let old = self.old_snapshot.as_ref().map_or("", |x| x.contents_str());
let new = self.new_snapshot.contents_str();
Expand Down Expand Up @@ -245,35 +266,6 @@ pub fn print_snapshot_summary(
}
}

/// Prints the snapshot not as diff.
#[cfg(feature = "_cargo_insta_internal")]
pub fn print_snapshot(
workspace_root: &Path,
new: &Snapshot,
snapshot_file: Option<&Path>,
mut line: Option<u32>,
show_info: bool,
) {
// default to old assertion line from snapshot.
if line.is_none() {
line = new.metadata().assertion_line();
}

print_snapshot_summary(workspace_root, new, snapshot_file, line);
let new_contents = new.contents_str();

let width = term_width();
if show_info {
print_info(new.metadata());
}
println!("Snapshot Contents:");
println!("──────┬{:─^1$}", "", width.saturating_sub(13));
for (idx, line) in new_contents.lines().enumerate() {
println!("{:>5} │ {}", style(idx + 1).cyan().dim().bold(), line);
}
println!("──────┴{:─^1$}", "", width.saturating_sub(13),);
}

fn print_line(width: usize) {
println!("{:─^1$}", "", width);
}
Expand Down

0 comments on commit a2cda1a

Please sign in to comment.