Skip to content

Commit

Permalink
test: add test coverage describing how pushparser handles empty docs
Browse files Browse the repository at this point in the history
The behavior is different between Java and C impls in a way that I
don't care enough to fix. Let's document the difference and move on
with our lives.
  • Loading branch information
flavorjones committed Jul 3, 2024
1 parent 2a6c6bb commit c208799
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/nokogiri/xml/sax/push_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def write(chunk, last_chunk = false)
###
# Finish the parsing. This method is only necessary for
# Nokogiri::XML::SAX::Document#end_document to be called.
#
# ⚠ Note that empty documents are treated as an error when using the libxml2-based
# implementation (CRuby), but are fine when using the Xerces-based implementation (JRuby).
def finish
write("", true)
end
Expand Down
33 changes: 30 additions & 3 deletions test/xml/sax/test_push_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,36 @@

it :test_empty_doc do
parser.options |= Nokogiri::XML::ParseOptions::RECOVER
parser.write("", true)
assert_nil parser.document.start_elements
assert_nil parser.document.end_elements
parser.finish

assert_nil(parser.document.start_elements)
assert_nil(parser.document.end_elements)
if Nokogiri.jruby?
assert_empty(parser.document.errors)
elsif Nokogiri.uses_libxml?(">= 2.12.0") # gnome/libxml2@53050b1d
assert_match(/Document is empty/, parser.document.errors.first)
end
assert(parser.document.end_document_called)
end

it :test_empty_doc_without_recovery do
# behavior is different between implementations
# https://github.com/sparklemotion/nokogiri/issues/1758
if Nokogiri.jruby?
parser.finish

assert_nil(parser.document.start_elements)
assert_nil(parser.document.end_elements)
assert_empty(parser.document.errors)
assert(parser.document.end_document_called)
else
e = assert_raises(Nokogiri::XML::SyntaxError) do
parser.finish
end
if Nokogiri.uses_libxml?(">= 2.12.0") # gnome/libxml2@53050b1d
assert_match(/Document is empty/, e.message)
end
end
end

it :test_finish_should_rethrow_last_error do
Expand Down

0 comments on commit c208799

Please sign in to comment.