Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parsing bug in treebuilder testsuite, fix crash in scriptdata state #98

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/emitters/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,14 @@ where
return false;
}

crate::utils::trace_log!("last_start_tag = {:?}", self.emitter_state.last_start_tag);
crate::utils::trace_log!("current_tag = {:?}", self.emitter_state.current_tag_name);
crate::utils::trace_log!(
"current_is_appropriate_end_tag_token: last_start_tag = {:?}",
self.emitter_state.last_start_tag
);
crate::utils::trace_log!(
"current_is_appropriate_end_tag_token: current_tag = {:?}",
self.emitter_state.current_tag_name
);
self.emitter_state.last_start_tag == self.emitter_state.current_tag_name
}
}
2 changes: 1 addition & 1 deletion src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub(crate) mod states {
c => {
slf.emitter.emit_string(b"</");
slf.machine_helper.flush_buffer_characters(&mut slf.emitter);
reconsume_in!(slf, c, Data)
reconsume_in!(slf, c, ScriptData)
}
}
)
Expand Down
10 changes: 10 additions & 0 deletions tests/custom-html5lib-tests/tree-construction/custom.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#data
<script></code><a href=foo></a></script>
#errors
#document
| <html>
| <head>
| <script>
| "</code><a href=foo></a>"
| <body>
#errors
2 changes: 1 addition & 1 deletion tests/html5lib_tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ fn main() {
produce_testcases_from_file(&mut tests, &entry.unwrap());
}

for entry in glob("tests/custom-html5lib-tests/*.test").unwrap() {
for entry in glob("tests/custom-html5lib-tests/tokenizer/*.test").unwrap() {
produce_testcases_from_file(&mut tests, &entry.unwrap());
}

Expand Down
13 changes: 11 additions & 2 deletions tests/html5lib_tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ impl Testcase {
}
}

None
if has_errors {
Some(rv)
} else {
None
}
}
}

Expand Down Expand Up @@ -138,6 +142,7 @@ fn map_tokenizer_state(input: State) -> html5gum::State {
State::Plaintext => html5gum::State::PlainText,
State::RawData(RawKind::Rcdata) => html5gum::State::RcData,
State::RawData(RawKind::Rawtext) => html5gum::State::RawText,
State::RawData(RawKind::ScriptData) => html5gum::State::ScriptData,
x => todo!("{:?}", x),
}
}
Expand Down Expand Up @@ -170,7 +175,7 @@ fn build_test(testcase: Testcase, fname: &str, i: usize, scripting: bool) -> Tri

let token_emitter = Html5everEmitter::new(&mut tree_builder);

let input = &testcase.data[..testcase.data.len() - 1];
let input = &testcase.data[..testcase.data.len().saturating_sub(1)];
let mut tokenizer = Tokenizer::new_with_emitter(input, token_emitter);
if let Some(state) = initial_state {
tokenizer.set_state(state);
Expand Down Expand Up @@ -290,6 +295,10 @@ fn main() {
let args = Arguments::from_args();
let mut tests = Vec::new();

for entry in glob("tests/custom-html5lib-tests/tree-construction/*.dat").unwrap() {
produce_testcases_from_file(&mut tests, &entry.unwrap());
}

for entry in glob("tests/html5lib-tests/tree-construction/*.dat").unwrap() {
produce_testcases_from_file(&mut tests, &entry.unwrap());
}
Expand Down
43 changes: 42 additions & 1 deletion tests/testutils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
use std::backtrace::BacktraceStatus;
use std::ops::Deref;
use std::panic::UnwindSafe;
use std::panic::{self, UnwindSafe};
use std::sync::Once;

use libtest_mimic::Failed;

use html5gum::testutils::OUTPUT;

/// Improved panic messages for the html5lib-tests test suite.
///
/// This function catches panics, prepends the log message to the panic message, and captures
/// stacktraces.
///
/// libtest_mimic already catches panics but doesn't provide stacktraces for some reason.
///
/// Because custom test harnesses in Rust do not support capturing of stdout, we have to implement
/// our own "log buffer", and append it to the error message in case of test failure. OUTPUT is the
/// log buffer -- it is bound in size and compiled out in release mode. Code can use the
/// `crate::trace_log` macro to write lines to it.
pub fn catch_unwind_and_report(f: impl FnOnce() + UnwindSafe) -> Result<(), Failed> {
static PANIC_HOOK: Once = Once::new();
PANIC_HOOK.call_once(|| {
panic::set_hook(Box::new(|_info| {
let backtrace = std::backtrace::Backtrace::capture();
if backtrace.status() != BacktraceStatus::Captured {
html5gum::testutils::trace_log(
"PANIC BACKTRACE: did not capture, use RUST_BACKTRACE=1",
);
} else {
// clean up noisy frames from backtrace
let mut backtrace_str = String::new();
let mut seen_begin_unwind = false;
for line in format!("{:#?}", backtrace).lines() {
if line.contains("\"std::panicking::try::do_call\"") {
break;
} else if seen_begin_unwind {
backtrace_str.push_str(line);
backtrace_str.push('\n');
} else if line.contains("\"core::panicking::panic_fmt\"") {
seen_begin_unwind = true;
}
}
html5gum::testutils::trace_log(&format!("\nPANIC BACKTRACE:\n{}", backtrace_str));
}
}));
});

let result = std::panic::catch_unwind(f);

let mut msg = String::new();
Expand All @@ -28,6 +68,7 @@ pub fn catch_unwind_and_report(f: impl FnOnce() + UnwindSafe) -> Result<(), Fail
// If that fails, try to turn it into a &'static str
.or_else(|| e.downcast_ref::<&'static str>().map(Deref::deref))
{
msg.push_str("PANIC: ");
msg.push_str(s);
}

Expand Down
Loading