Skip to content

Commit 2ef9c04

Browse files
Assert that comments are not reordered by formatter (#318) (#323)
Signed-off-by: John Kastner <[email protected]> Co-authored-by: John Kastner <[email protected]>
1 parent e1d1761 commit 2ef9c04

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

cedar-drt/fuzz/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rayon = { version = "1.5", optional = true }
2626
rand = { version = "0.8", optional = true }
2727
clap = { version = "4.0", features = ["derive"], optional = true }
2828
rand_chacha = { version = "0.3", optional = true }
29+
similar-asserts = "1.5.0"
2930

3031
[dependencies.uuid]
3132
version = "1.3.1"

cedar-drt/fuzz/fuzz_targets/formatter.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use cedar_policy_generators::{
2727
use libfuzzer_sys::arbitrary::{self, Arbitrary, Unstructured};
2828
use log::debug;
2929
use serde::Serialize;
30+
use similar_asserts::SimpleDiff;
3031
use smol_str::SmolStr;
3132
use std::collections::HashMap;
3233
use uuid::Uuid;
@@ -104,12 +105,31 @@ fn round_trip(p: &StaticPolicy) -> Result<StaticPolicy, parser::err::ParseErrors
104105
line_width: 80,
105106
};
106107
let mut uuids = Vec::new();
108+
let commented = attach_comment(&p.to_string(), &mut uuids);
107109
let formatted_policy_str =
108-
&policies_str_to_pretty(&attach_comment(&p.to_string(), &mut uuids), &config)
109-
.expect("pretty-printing should not fail");
110+
&policies_str_to_pretty(&commented, &config).expect("pretty-printing should not fail");
110111
// check if pretty-printing drops any comment
112+
let mut formatted_policy_tail: &str = formatted_policy_str.as_str();
111113
for u in &uuids {
112-
assert!(formatted_policy_str.contains(u), "missing comment: {}\n", u);
114+
match formatted_policy_tail.split_once(u) {
115+
Some((_, after_uuid)) => formatted_policy_tail = after_uuid,
116+
None => {
117+
println!(
118+
"{}",
119+
SimpleDiff::from_str(
120+
&commented,
121+
formatted_policy_str,
122+
"Commented Policy",
123+
"Commented and formatted"
124+
)
125+
);
126+
if formatted_policy_str.contains(u) {
127+
panic!("It looks like we skipped over comment `{u}`. The formatter might have reordered the comment")
128+
} else {
129+
panic!("Failed to find comment `{u}` anywhere. The formatter might have dropped the comment")
130+
}
131+
}
132+
}
113133
}
114134
parse_policy(None, formatted_policy_str)
115135
}

0 commit comments

Comments
 (0)