diff --git a/cargo-insta/src/cli.rs b/cargo-insta/src/cli.rs index 9f1e54d8..81ae011e 100644 --- a/cargo-insta/src/cli.rs +++ b/cargo-insta/src/cli.rs @@ -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; @@ -228,6 +227,7 @@ fn query_snapshot( n: usize, snapshot_file: Option<&Path>, show_info: &mut bool, + show_diff: &mut bool, ) -> Result> { loop { term.clear_screen()?; @@ -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!( @@ -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()? { @@ -283,6 +291,10 @@ fn query_snapshot( *show_info = !*show_info; break; } + Key::Char('d') => { + *show_diff = !*show_diff; + break; + } _ => {} } } @@ -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(); @@ -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 { @@ -880,7 +894,11 @@ fn prepare_test_runner<'snapshot_ref>( fn show_cmd(cmd: ShowCommand) -> Result<(), Box> { 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(()) } diff --git a/src/lib.rs b/src/lib.rs index 5f58156d..afc28701 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, diff --git a/src/output.rs b/src/output.rs index 65ab9774..9a074a1d 100644 --- a/src/output.rs +++ b/src/output.rs @@ -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) { @@ -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(); @@ -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, - 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); }