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

Added support for hiding diffs / showing snapshots in review #348

Merged
merged 2 commits into from Feb 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ All notable changes to insta and cargo-insta are documented here.
- Added `allow_duplicates!` to enable multiple assertions for a
single snapshot. (#346)
- Ensure that expressions formatted with `rustfmt` use unix newlines.
- Added a way to disable diffing in review. (#348)

## 1.27.0

Expand Down
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