Skip to content

Commit

Permalink
Added a show command to cargo-insta to print out a snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Sep 27, 2022
1 parent 7d74731 commit 3280a56
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
22 changes: 21 additions & 1 deletion cargo-insta/src/cli.rs
Expand Up @@ -9,7 +9,7 @@ use std::{io, process};
use console::{set_colors_enabled, style, Key, Term};
use ignore::{Walk, WalkBuilder};
use insta::Snapshot;
use insta::_cargo_insta_support::print_snapshot_diff;
use insta::_cargo_insta_support::{print_snapshot, print_snapshot_diff};
use serde::Serialize;
use structopt::clap::AppSettings;
use structopt::StructOpt;
Expand Down Expand Up @@ -58,6 +58,9 @@ pub enum Command {
/// Print a summary of all pending snapshots.
#[structopt(name = "pending-snapshots")]
PendingSnapshots(PendingSnapshotsCommand),
/// Shows a specific snapshot
#[structopt(name = "show")]
Show(ShowCommand),
}

#[derive(StructOpt, Debug, Clone)]
Expand Down Expand Up @@ -165,6 +168,15 @@ pub struct PendingSnapshotsCommand {
pub as_json: bool,
}

#[derive(StructOpt, Debug)]
#[structopt(rename_all = "kebab-case")]
pub struct ShowCommand {
#[structopt(flatten)]
pub target_args: TargetArgs,
/// The path to the snapshot file.
pub path: PathBuf,
}

#[allow(clippy::too_many_arguments)]
fn query_snapshot(
workspace_root: &Path,
Expand Down Expand Up @@ -776,6 +788,13 @@ fn prepare_test_runner<'snapshot_ref>(
Ok((proc, snapshot_ref_file))
}

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);
Ok(())
}

fn pending_snapshots_cmd(cmd: PendingSnapshotsCommand) -> Result<(), Box<dyn Error>> {
let loc = handle_target_args(&cmd.target_args)?;
let mut snapshot_containers = load_snapshot_containers(&loc)?;
Expand Down Expand Up @@ -827,6 +846,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
Command::Accept(cmd) => process_snapshots(cmd, Some(Operation::Accept)),
Command::Reject(cmd) => process_snapshots(cmd, Some(Operation::Reject)),
Command::Test(cmd) => test_run(cmd, color),
Command::Show(cmd) => show_cmd(cmd),
Command::PendingSnapshots(cmd) => pending_snapshots_cmd(cmd),
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Expand Up @@ -248,7 +248,9 @@ pub mod internals {
#[doc(hidden)]
pub mod _cargo_insta_support {
pub use crate::{
output::print_snapshot_diff, snapshot::PendingInlineSnapshot, snapshot::SnapshotContents,
output::{print_snapshot, print_snapshot_diff},
snapshot::PendingInlineSnapshot,
snapshot::SnapshotContents,
};
}

Expand Down
62 changes: 46 additions & 16 deletions src/output.rs
Expand Up @@ -73,6 +73,34 @@ pub fn print_snapshot_diff(
print_changeset(old_contents, new_contents, new.metadata(), show_info);
}

/// Prints the snapshot not as diff.
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(), width);
}
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),);
}

pub fn print_snapshot_diff_with_title(
workspace_root: &Path,
new_snapshot: &Snapshot,
Expand Down Expand Up @@ -127,22 +155,7 @@ pub fn print_changeset(old: &str, new: &str, metadata: &MetaData, show_info: boo
print_line(width);

if show_info {
if let Some(expr) = metadata.expression() {
println!("Expression: {}", style(format_rust_expression(expr)));
print_line(width);
}

if let Some(descr) = metadata.description() {
println!("{}", descr);
print_line(width);
}

if let Some(info) = metadata.private_info() {
let out = yaml::to_string(info);
// TODO: does the yaml output always start with '---'?
println!("{}", out.trim().strip_prefix("---").unwrap().trim_start());
print_line(width);
}
print_info(metadata, width);
}

if !old.is_empty() {
Expand Down Expand Up @@ -222,3 +235,20 @@ pub fn print_changeset(old: &str, new: &str, metadata: &MetaData, show_info: boo

println!("────────────┴{:─^1$}", "", width.saturating_sub(13),);
}

fn print_info(metadata: &MetaData, width: usize) {
if let Some(expr) = metadata.expression() {
println!("Expression: {}", style(format_rust_expression(expr)));
print_line(width);
}
if let Some(descr) = metadata.description() {
println!("{}", descr);
print_line(width);
}
if let Some(info) = metadata.private_info() {
let out = yaml::to_string(info);
// TODO: does the yaml output always start with '---'?
println!("{}", out.trim().strip_prefix("---").unwrap().trim_start());
print_line(width);
}
}

0 comments on commit 3280a56

Please sign in to comment.