Skip to content

Commit

Permalink
feat: Do not emit indentation for empty lines. Fixes mitsuhiko#88
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 23, 2020
1 parent 8cc12dc commit 816df88
Show file tree
Hide file tree
Showing 46 changed files with 65 additions and 104 deletions.
26 changes: 23 additions & 3 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,18 @@ impl SnapshotContents {
out.extend(
contents
.lines()
// `lines` removes the final line ending - add back
.chain("\n".lines())
// newline needs to be at the start, since we don't want the end
// finishing with a newline - the closing suffix should be on the same line
.map(|l| format!("\n{:width$}{l}", "", width = indentation, l = l)),
.map(|l| {
format!(
"\n{:width$}{l}",
"",
width = if l.is_empty() { 0 } else { indentation },
l = l
)
})
// `lines` removes the final line ending - add back
.chain(Some(format!("\n{:width$}", "", width = indentation)).into_iter()),
);
} else {
out.push_str(contents);
Expand Down Expand Up @@ -338,6 +345,19 @@ b"[1..];
\"###"
);

let t = &"
a
b"[1..];
assert_eq!(
SnapshotContents(t.to_string()).to_inline(0),
"r###\"
a
b
\"###"
);

let t = "ab";
assert_eq!(SnapshotContents(t.to_string()).to_inline(0), r##""ab""##);
}
10 changes: 4 additions & 6 deletions src/snapshots/test__embedded.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Created: 2019-01-20T21:06:59.853399+00:00
Creator: insta@0.2.2
Expression: "Just a string"
Line: 3
Source: src/test.rs

---
source: src/test.rs
expression: "\"Just a string\""
---
Just a string
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__debug_vector.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-05-29T04:21:15.246465Z"
creator: insta@0.8.1
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_basic__display.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-03-03T12:38:56.348258Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: td
---
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__json_vector.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-01-29T23:35:18.809083Z"
creator: insta@0.6.0
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_debug_vector-2.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-05-29T04:22:50.837055Z"
creator: insta@0.8.1
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3 , 4]"
expression: "vec![1, 2, 3, 4]"
---
[
1,
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_debug_vector-3.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-05-29T04:23:34.575648Z"
creator: insta@0.8.1
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3 , 4 , 5]"
expression: "vec![1, 2, 3, 4, 5]"
---
[
1,
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_debug_vector.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-05-29T04:21:15.246794Z"
creator: insta@0.8.1
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_basic__unnamed_display-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-03-05T09:04:26.857647Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "\"whatever\""
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_basic__unnamed_display.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-03-05T09:04:26.812351Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: td
---
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_json_vector-2.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-03-05T09:00:05.773633Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3 , 4]"
expression: "vec![1, 2, 3, 4]"
---
[
1,
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_json_vector-3.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-03-05T09:00:05.815658Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3 , 4 , 5]"
expression: "vec![1, 2, 3, 4, 5]"
---
[
1,
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_json_vector.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-03-05T09:00:05.725268Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_yaml_vector-2.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-03-05T09:03:05.339305Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3 , 4]"
expression: "vec![1, 2, 3, 4]"
---
- 1
- 2
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_yaml_vector-3.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-03-05T09:03:05.376897Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3 , 4 , 5]"
expression: "vec![1, 2, 3, 4, 5]"
---
- 1
- 2
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__unnamed_yaml_vector.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-03-05T09:03:05.274574Z"
creator: insta@0.6.3
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
- 1
- 2
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/test_basic__yaml_vector.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-01-29T23:40:11.452071Z"
creator: insta@0.6.0
source: tests/test_basic.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
- 1
- 2
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_inline__something-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-08-02T12:56:32.092623600Z"
creator: insta@0.9.0
source: tests/test_inline.rs
expression: "\"Testing-thread-2\""
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_inline__something.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-08-02T12:56:32.045665300Z"
creator: insta@0.9.0
source: tests/test_inline.rs
expression: "\"Testing-thread\""
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_inline__unnamed_single_line-2.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-07-16T10:35:19.553505Z"
creator: insta@0.8.1
source: tests/test_inline.rs
expression: "\"Testing-2\""
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_inline__unnamed_single_line.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-07-16T10:31:50.245097Z"
creator: insta@0.8.1
source: tests/test_inline.rs
expression: "\"Testing\""
---
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/test_legacy__debug_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/test_legacy__json_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3 , 4]"
expression: "vec![1, 2, 3, 4]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3 , 4 , 5]"
expression: "vec![1, 2, 3, 4, 5]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3 , 4]"
expression: "vec![1, 2, 3, 4]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3 , 4 , 5]"
expression: "vec![1, 2, 3, 4, 5]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
[
1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3 , 4]"
expression: "vec![1, 2, 3, 4]"
---
- 1
- 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3 , 4 , 5]"
expression: "vec![1, 2, 3, 4, 5]"
---
- 1
- 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
- 1
- 2
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/test_legacy__yaml_vector.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tests/test_legacy.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
- 1
- 2
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__foo_bar.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-05-29T14:16:16.625625Z"
creator: insta@0.8.1
source: tests/test_redaction.rs
expression: "Selector::parse(\".foo.bar\").unwrap()"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__foo_bar_alt.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-05-29T14:53:37.492888Z"
creator: insta@0.8.1
source: tests/test_redaction.rs
expression: "Selector::parse(\".foo[\\\"bar\\\"]\").unwrap()"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__foo_bar_full_range.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-05-29T15:01:31.190888Z"
creator: insta@0.8.1
source: tests/test_redaction.rs
expression: "Selector::parse(\".foo.bar[]\").unwrap()"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__foo_bar_range.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-05-29T15:02:21.911637Z"
creator: insta@0.8.1
source: tests/test_redaction.rs
expression: "Selector::parse(\".foo.bar[10:20]\").unwrap()"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__foo_bar_range_from.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-05-29T15:01:53.352063Z"
creator: insta@0.8.1
source: tests/test_redaction.rs
expression: "Selector::parse(\".foo.bar[10:]\").unwrap()"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__foo_bar_range_to.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-05-29T15:01:42.070559Z"
creator: insta@0.8.1
source: tests/test_redaction.rs
expression: "Selector::parse(\".foo.bar[:10]\").unwrap()"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__user.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-02-01T13:47:54.698177Z"
creator: insta@0.6.0
source: tests/test_redaction.rs
expression: "&User{id: Uuid::new_v4(),\n username: \"john_doe\".to_string(),\n email: Email(\"john@example.com\".to_string()),\n extra: \"\".to_string(),}"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__user_json.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-02-01T13:47:54.698186Z"
creator: insta@0.6.0
source: tests/test_redaction.rs
expression: "&User{id: Uuid::new_v4(),\n username: \"jason_doe\".to_string(),\n email: Email(\"jason@example.com\".to_string()),\n extra: \"ssn goes here\".to_string(),}"
---
Expand Down
2 changes: 0 additions & 2 deletions tests/snapshots/test_redaction__user_json_settings.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-08-23T20:50:31.800965Z"
creator: insta@0.10.1
source: tests/test_redaction.rs
expression: "&User{id: Uuid::new_v4(),\n username: \"jason_doe\".to_string(),\n email: Email(\"jason@example.com\".to_string()),\n extra: \"ssn goes here\".to_string(),}"
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
created: "2019-08-23T21:11:06.365815Z"
creator: insta@0.10.1
source: tests/test_redaction.rs
expression: "&User{id: Uuid::new_v4(),\n username: \"jason_doe\".to_string(),\n email: Email(\"jason@example.com\".to_string()),\n extra: \"ssn goes here\".to_string(),}"
---
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots2/test_settings__snapshot_path.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
created: "2019-08-23T22:20:37.202188Z"
creator: insta@0.10.1
source: tests/test_settings.rs
expression: "vec![1 , 2 , 3]"
expression: "vec![1, 2, 3]"
---
- 1
- 2
Expand Down

0 comments on commit 816df88

Please sign in to comment.