Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mitsuhiko/insta
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.6.3
Choose a base ref
...
head repository: mitsuhiko/insta
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.7.0
Choose a head ref
  • 7 commits
  • 14 files changed
  • 1 contributor

Commits on Feb 19, 2021

  1. Copy the full SHA
    2a51441 View commit details

Commits on Feb 27, 2021

  1. Copy the full SHA
    a23d35f View commit details
  2. Copy the full SHA
    fd7cb13 View commit details
  3. Copy the full SHA
    f47c4a1 View commit details
  4. Copy the full SHA
    0335552 View commit details
  5. Update changelog

    mitsuhiko committed Feb 27, 2021
    Copy the full SHA
    c29e929 View commit details
  6. 1.7.0

    mitsuhiko committed Feb 27, 2021
    Copy the full SHA
    d8ed427 View commit details
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,12 @@

All notable changes to insta and cargo-insta are documented here.

## 1.7.0

* Added support for u128/i128. (#169)
* Normalize newlines to unix before before asserting. (#172)
* Switch diffing to patience. (#173)

## 1.6.3

* Fix a bug with empty lines in inline snapshots. (#166)
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "insta"
version = "1.6.3"
version = "1.7.0"
license = "Apache-2.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
description = "A snapshot testing library for Rust"
@@ -47,4 +47,7 @@ toml = { version = "0.5.7", optional = true }
globset = { version = "0.4.6", optional = true }
walkdir = { version = "2.3.1", optional = true }
uuid = "0.8.1"
similar = { version = "1.2.2", features = ["inline"] }
similar = { version = "1.3.0", features = ["inline"] }

[dev-dependencies]
similar-asserts = "1.1.0"
8 changes: 4 additions & 4 deletions cargo-insta/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cargo-insta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-insta"
version = "1.6.3"
version = "1.7.0"
license = "Apache-2.0"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
description = "A review tool for the insta snapshot testing library for Rust"
@@ -12,7 +12,7 @@ edition = "2018"
readme = "README.md"

[dependencies]
insta = { version = "1.6.3", path = "..", features = ["redactions"] }
insta = { version = "1.7.0", path = "..", features = ["redactions"] }
console = "0.14.0"
clap = { version = "2.33.3", default-features = false }
difference = "2.0.0"
75 changes: 75 additions & 0 deletions src/content.rs
Original file line number Diff line number Diff line change
@@ -32,11 +32,13 @@ pub enum Content {
U16(u16),
U32(u32),
U64(u64),
U128(u128),

I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),

F32(f32),
F64(f64),
@@ -86,6 +88,8 @@ pub enum Key<'a> {
U64(u64),
I64(i64),
F64(f64),
U128(u128),
I128(i128),
Str(&'a str),
Bytes(&'a [u8]),
Other,
@@ -114,10 +118,12 @@ impl_from!(u8, U8);
impl_from!(u16, U16);
impl_from!(u32, U32);
impl_from!(u64, U64);
impl_from!(u128, U128);
impl_from!(i8, I8);
impl_from!(i16, I16);
impl_from!(i32, I32);
impl_from!(i64, I64);
impl_from!(i128, I128);
impl_from!(f32, F32);
impl_from!(f64, F64);
impl_from!(char, Char);
@@ -206,9 +212,11 @@ impl Content {
Content::U16(val) => Key::U64(val.into()),
Content::U32(val) => Key::U64(val.into()),
Content::U64(val) => Key::U64(val),
Content::U128(val) => Key::U128(val),
Content::I16(val) => Key::I64(val.into()),
Content::I32(val) => Key::I64(val.into()),
Content::I64(val) => Key::I64(val),
Content::I128(val) => Key::I128(val),
Content::F32(val) => Key::F64(val.into()),
Content::F64(val) => Key::F64(val),
Content::String(ref val) => Key::Str(&val.as_str()),
@@ -232,14 +240,39 @@ impl Content {
Content::U16(v) => Some(u64::from(v)),
Content::U32(v) => Some(u64::from(v)),
Content::U64(v) => Some(v),
Content::U128(v) => {
let rv = v as u64;
if rv as u128 == v {
Some(rv)
} else {
None
}
}
Content::I8(v) if v >= 0 => Some(v as u64),
Content::I16(v) if v >= 0 => Some(v as u64),
Content::I32(v) if v >= 0 => Some(v as u64),
Content::I64(v) if v >= 0 => Some(v as u64),
Content::I128(v) => {
let rv = v as u64;
if rv as i128 == v {
Some(rv)
} else {
None
}
}
_ => None,
}
}

/// Returns the value as u128
pub fn as_u128(&self) -> Option<u128> {
match *self.resolve_inner() {
Content::U128(v) => Some(v),
Content::I128(v) if v >= 0 => Some(v as u128),
_ => self.as_u64().map(u128::from),
}
}

/// Returns the value as i64
pub fn as_i64(&self) -> Option<i64> {
match *self.resolve_inner() {
@@ -254,14 +287,46 @@ impl Content {
None
}
}
Content::U128(v) => {
let rv = v as i64;
if rv as u128 == v {
Some(rv)
} else {
None
}
}
Content::I8(v) => Some(i64::from(v)),
Content::I16(v) => Some(i64::from(v)),
Content::I32(v) => Some(i64::from(v)),
Content::I64(v) => Some(v),
Content::I128(v) => {
let rv = v as i64;
if rv as i128 == v {
Some(rv)
} else {
None
}
}
_ => None,
}
}

/// Returns the value as i128
pub fn as_i128(&self) -> Option<i128> {
match *self.resolve_inner() {
Content::U128(v) => {
let rv = v as i128;
if rv as u128 == v {
Some(rv)
} else {
None
}
}
Content::I128(v) => Some(v),
_ => self.as_i64().map(i128::from),
}
}

/// Returns the value as f64
pub fn as_f64(&self) -> Option<f64> {
match *self.resolve_inner() {
@@ -350,10 +415,12 @@ impl Serialize for Content {
Content::U16(u) => serializer.serialize_u16(u),
Content::U32(u) => serializer.serialize_u32(u),
Content::U64(u) => serializer.serialize_u64(u),
Content::U128(u) => serializer.serialize_u128(u),
Content::I8(i) => serializer.serialize_i8(i),
Content::I16(i) => serializer.serialize_i16(i),
Content::I32(i) => serializer.serialize_i32(i),
Content::I64(i) => serializer.serialize_i64(i),
Content::I128(i) => serializer.serialize_i128(i),
Content::F32(f) => serializer.serialize_f32(f),
Content::F64(f) => serializer.serialize_f64(f),
Content::Char(c) => serializer.serialize_char(c),
@@ -466,6 +533,10 @@ where
Ok(Content::I64(v))
}

fn serialize_i128(self, v: i128) -> Result<Content, E> {
Ok(Content::I128(v))
}

fn serialize_u8(self, v: u8) -> Result<Content, E> {
Ok(Content::U8(v))
}
@@ -482,6 +553,10 @@ where
Ok(Content::U64(v))
}

fn serialize_u128(self, v: u128) -> Result<Content, E> {
Ok(Content::U128(v))
}

fn serialize_f32(self, v: f32) -> Result<Content, E> {
Ok(Content::F32(v))
}
1 change: 1 addition & 0 deletions src/redaction.rs
Original file line number Diff line number Diff line change
@@ -469,6 +469,7 @@ impl<'a> Selector<'a> {

#[test]
fn test_range_checks() {
use similar_asserts::assert_eq;
assert_eq!(PathItem::Index(0, 10).range_check(None, Some(-1)), true);
assert_eq!(PathItem::Index(9, 10).range_check(None, Some(-1)), false);
assert_eq!(PathItem::Index(0, 10).range_check(Some(1), Some(-1)), false);
16 changes: 13 additions & 3 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -9,9 +9,10 @@ use std::process::{Command, Stdio};
use std::str;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;

use lazy_static::lazy_static;
use similar::{ChangeTag, TextDiff};
use similar::{Algorithm, ChangeTag, TextDiff};

use serde::Deserialize;

@@ -208,7 +209,10 @@ pub fn get_cargo_workspace(manifest_dir: &str) -> &Path {

fn print_changeset(old: &str, new: &str, expr: Option<&str>) {
let width = term_width();
let diff = TextDiff::from_lines(old, new);
let diff = TextDiff::configure()
.algorithm(Algorithm::Patience)
.timeout(Duration::from_millis(500))
.diff_lines(old, new);

if let Some(expr) = expr {
println!("{:─^1$}", "", width,);
@@ -566,6 +570,8 @@ fn generate_snapshot_name_for_thread(module_path: &str) -> Result<String, &'stat
/// frozen value string. If the string starts with the '⋮' character
/// (optionally prefixed by whitespace) the alternative serialization format
/// is picked which has slightly improved indentation semantics.
///
/// This also changes all newlines to \n
pub(super) fn get_inline_snapshot_value(frozen_value: &str) -> String {
// TODO: could move this into the SnapshotContents `from_inline` method
// (the only call site)
@@ -640,6 +646,7 @@ fn min_indentation(snapshot: &str) -> usize {

#[test]
fn test_min_indentation() {
use similar_asserts::assert_eq;
let t = r#"
1
2
@@ -693,6 +700,7 @@ a
}

// Removes excess indentation, removes excess whitespace at start & end
// and changes newlines to \n.
fn normalize_inline_snapshot(snapshot: &str) -> String {
let indentation = min_indentation(snapshot);
snapshot
@@ -706,6 +714,7 @@ fn normalize_inline_snapshot(snapshot: &str) -> String {

#[test]
fn test_normalize_inline_snapshot() {
use similar_asserts::assert_eq;
// here we do exact matching (rather than `assert_snapshot`)
// to ensure we're not incorporating the modifications this library makes
let t = r#"
@@ -924,6 +933,7 @@ pub fn assert_snapshot(
};

let new_snapshot_contents: SnapshotContents = new_snapshot.into();

let new = Snapshot::from_components(
module_path.replace("::", "__"),
snapshot_name.as_ref().map(|x| x.to_string()),
@@ -952,7 +962,7 @@ pub fn assert_snapshot(

// if the snapshot matches we're done.
if let Some(ref old_snapshot) = old {
if old_snapshot.contents() == new.contents() {
if dbg!(old_snapshot.contents()) == dbg!(new.contents()) {
// let's just make sure there are no more pending files lingering
// around.
if let Some(ref snapshot_file) = snapshot_file {
8 changes: 6 additions & 2 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -269,6 +269,7 @@ impl SnapshotContents {
pub fn from_inline(value: &str) -> SnapshotContents {
SnapshotContents(get_inline_snapshot_value(value))
}

pub fn to_inline(&self, indentation: usize) -> String {
let contents = &self.0;
let mut out = String::new();
@@ -306,13 +307,15 @@ impl SnapshotContents {

impl From<&str> for SnapshotContents {
fn from(value: &str) -> SnapshotContents {
SnapshotContents(value.to_string())
// make sure we have unix newlines consistently
SnapshotContents(value.replace("\r\n", "\n").to_string())
}
}

impl From<String> for SnapshotContents {
fn from(value: String) -> SnapshotContents {
SnapshotContents(value)
// make sure we have unix newlines consistently
SnapshotContents(value.replace("\r\n", "\n").to_string())
}
}

@@ -330,6 +333,7 @@ impl PartialEq for SnapshotContents {

#[test]
fn test_snapshot_contents() {
use similar_asserts::assert_eq;
let snapshot_contents = SnapshotContents("testing".to_string());
assert_eq!(snapshot_contents.to_inline(0), r#""testing""#);

8 changes: 8 additions & 0 deletions tests/snapshots/test_bugs__crlf.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/test_bugs.rs
expression: "\"foo\\r\\nbar\\r\\nbaz\""

---
foo
bar
baz
9 changes: 9 additions & 0 deletions tests/snapshots/test_bugs__trailing_crlf.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/test_bugs.rs
expression: "\"foo\\r\\nbar\\r\\nbaz\\r\\n\""

---
foo
bar
baz

6 changes: 6 additions & 0 deletions tests/test_basic.rs
Original file line number Diff line number Diff line change
@@ -66,3 +66,9 @@ fn test_unnamed_display() {
assert_display_snapshot!(td);
assert_display_snapshot!("whatever");
}

#[test]
fn test_u128_json() {
let x: u128 = 42;
assert_json_snapshot!(&x, @"42");
}
Loading