Skip to content

Commit

Permalink
fix(fuzz): skip tests marked with :skip & don't report errors on te…
Browse files Browse the repository at this point in the history
…sts marked with `:error`

(cherry picked from commit 99dbbbc)
  • Loading branch information
WillLillis authored and amaanq committed Sep 22, 2024
1 parent 8e1dbb4 commit 83509ad
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions cli/src/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ pub fn fuzz_language_corpus(
}
let tests = flatten_tests(main_tests, options.filter.as_ref());

let mut skipped = options.skipped.as_ref().map(|x| {
x.iter()
.map(|x| (x.as_str(), 0))
.collect::<HashMap<&str, usize>>()
});
let get_test_name = |test: &FlattenedTest| format!("{language_name} - {}", test.name);

let mut skipped = options
.skipped
.take()
.unwrap_or_default()
.into_iter()
.chain(tests.iter().filter(|x| x.skip).map(get_test_name))
.map(|x| (x, 0))
.collect::<HashMap<String, usize>>();

let mut failure_count = 0;

Expand All @@ -125,13 +130,11 @@ pub fn fuzz_language_corpus(

println!();
for (test_index, test) in tests.iter().enumerate() {
let test_name = format!("{language_name} - {}", test.name);
if let Some(skipped) = skipped.as_mut() {
if let Some(counter) = skipped.get_mut(test_name.as_str()) {
println!(" {test_index}. {test_name} - SKIPPED");
*counter += 1;
continue;
}
let test_name = get_test_name(test);
if let Some(counter) = skipped.get_mut(test_name.as_str()) {
println!(" {test_index}. {test_name} - SKIPPED");
*counter += 1;
continue;
}

println!(" {test_index}. {test_name}");
Expand All @@ -143,6 +146,11 @@ pub fn fuzz_language_corpus(
set_included_ranges(&mut parser, &test.input, test.template_delimiters);

let tree = parser.parse(&test.input, None).unwrap();

if test.error {
return true;
}

let mut actual_output = tree.root_node().to_sexp();
if !test.has_fields {
actual_output = strip_sexp_fields(&actual_output);
Expand Down Expand Up @@ -240,7 +248,7 @@ pub fn fuzz_language_corpus(
actual_output = strip_sexp_fields(&actual_output);
}

if actual_output != test.output {
if actual_output != test.output && !test.error {
println!("Incorrect parse for {test_name} - seed {seed}");
print_diff_key();
print_diff(&actual_output, &test.output, true);
Expand Down Expand Up @@ -272,16 +280,14 @@ pub fn fuzz_language_corpus(
eprintln!("{failure_count} {language_name} corpus tests failed fuzzing");
}

if let Some(skipped) = skipped.as_mut() {
skipped.retain(|_, v| *v == 0);
skipped.retain(|_, v| *v == 0);

if !skipped.is_empty() {
println!("Non matchable skip definitions:");
for k in skipped.keys() {
println!(" {k}");
}
panic!("Non matchable skip definitions needs to be removed");
if !skipped.is_empty() {
println!("Non matchable skip definitions:");
for k in skipped.keys() {
println!(" {k}");
}
panic!("Non matchable skip definitions needs to be removed");
}
}

Expand All @@ -290,6 +296,8 @@ pub struct FlattenedTest {
pub input: Vec<u8>,
pub output: String,
pub languages: Vec<Box<str>>,
pub error: bool,
pub skip: bool,
pub has_fields: bool,
pub template_delimiters: Option<(&'static str, &'static str)>,
}
Expand Down Expand Up @@ -327,6 +335,8 @@ pub fn flatten_tests(test: TestEntry, filter: Option<&Regex>) -> Vec<FlattenedTe
output,
has_fields,
languages: attributes.languages,
error: attributes.error,
skip: attributes.skip,
template_delimiters: None,
});
}
Expand Down

0 comments on commit 83509ad

Please sign in to comment.