Skip to content

Commit

Permalink
Added the ability to redact collection keys (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Sep 10, 2021
1 parent 15ad420 commit baf6f8b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

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

## 1.8.0

- Added the ability to redact into a key. (#192)

## 1.7.2

- Fixed an issue where selectors could not start with underscore. (#189)
Expand Down
7 changes: 6 additions & 1 deletion src/redaction.rs
Expand Up @@ -417,10 +417,15 @@ impl<'a> Selector<'a> {
Content::Map(map) => Content::Map(
map.into_iter()
.map(|(key, value)| {
path.push(PathItem::Field("$key"));
let new_key = self.redact_impl(key.clone(), redaction, path);
path.pop();

path.push(PathItem::Content(key.clone()));
let new_value = self.redact_impl(value, redaction, path);
path.pop();
(key, new_value)

(new_key, new_value)
})
.collect(),
),
Expand Down
2 changes: 1 addition & 1 deletion src/select_grammar.pest
@@ -1,6 +1,6 @@
WHITESPACE = _{ WHITE_SPACE }

ident = @{ ( "_" | XID_START ) ~ XID_CONTINUE* }
ident = @{ ( "_" | "$" | XID_START ) ~ XID_CONTINUE* }
deep_wildcard = { "." ~ "**" }
wildcard = { "." ~ "*" }
key = @{ "." ~ ident }
Expand Down
10 changes: 10 additions & 0 deletions tests/snapshots/test_redaction__map_key_redaction.snap
@@ -0,0 +1,10 @@
---
source: tests/test_redaction.rs
expression: foo
---
hm:
? bucket: "[bucket]"
value: 0
: 42
btm:
"[key]": 23
34 changes: 34 additions & 0 deletions tests/test_redaction.rs
@@ -1,5 +1,7 @@
#![cfg(feature = "redactions")]

use std::collections::{BTreeMap, HashMap};

use insta::_macro_support::Selector;
use insta::{
assert_debug_snapshot, assert_json_snapshot, assert_yaml_snapshot, with_settings, Settings,
Expand Down Expand Up @@ -307,3 +309,35 @@ fn test_struct_array_redaction() {
"[].products[].product_name" => "[product_name]",
});
}

#[test]
fn test_map_key_redaction() {
#[derive(Serialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
struct Key {
bucket: u32,
value: u32,
}

#[derive(Serialize)]
struct Foo {
hm: HashMap<Key, u32>,
btm: BTreeMap<(u32, u32), u32>,
}

let mut hm = HashMap::new();
hm.insert(
Key {
bucket: 1,
value: 0,
},
42,
);
let mut btm = BTreeMap::new();
btm.insert((0, 0), 23);
let foo = Foo { hm, btm };

insta::assert_yaml_snapshot!(foo, {
".hm.$key.bucket" => "[bucket]",
".btm.$key" => "[key]",
});
}

0 comments on commit baf6f8b

Please sign in to comment.