Skip to content

Commit

Permalink
Fix broken specs in Kiwi::Image model
Browse files Browse the repository at this point in the history
Two specs were failing when checking if the subject didn't have errors:

```
Failure/Error: it { expect(subject.errors).to be_empty }
  expected `[#<Nokogiri::XML::SyntaxError: 1:12: FATAL: XML declaration allowed only at the start of the document>].empty?` to be truthy, got false
./spec/models/kiwi/image_spec.rb:226:in `block (5 levels) in <top (required)>'

Failure/Error: expect(subject.errors).to be_empty
expected `[#<Nokogiri::XML::SyntaxError: 1:12: FATAL: XML declaration allowed only at the start of the document>].empty?` to be truthy, got false
./spec/models/kiwi/image_spec.rb:279:in `block (4 levels) in <top (required)>'
```

Those errors weren't reported before nokogiri _1.13.0_, as noted in the
changelog upstream:

> XML::DocumentFragment#errors now correctly contains any parsing errors
> encountered. Previously this was always empty. (Note that
> HTML::DocumentFragment#errors already did this.

Relying on XML::DocumentFragment was wrong since the XML generated by
the Kiwi::Image's to_xml and kiwi_body methods is not a document
fragment, but a document. They contain a XML declaration (`<?xml ...?>`),
so changing the class solves the issue.
  • Loading branch information
Dany Marcoux committed Mar 21, 2022
1 parent 76f3666 commit db6bda1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api/app/models/kiwi/image/xml_builder.rb
Expand Up @@ -6,7 +6,7 @@ def initialize(image)
end

def build
doc = Nokogiri::XML::DocumentFragment.parse(@image.kiwi_body)
doc = Nokogiri::XML::Document.parse(@image.kiwi_body)
image = doc.at_css('image')

return nil unless image && image.first_element_child
Expand Down
8 changes: 4 additions & 4 deletions src/api/spec/models/kiwi/image_spec.rb
Expand Up @@ -221,7 +221,7 @@
kiwi_image.save
end

subject { Nokogiri::XML::DocumentFragment.parse(kiwi_image.to_xml) }
subject { Nokogiri::XML::Document.parse(kiwi_image.to_xml) }

it { expect(subject.errors).to be_empty }
it { expect(subject.xpath('.//image').length).to be(1) }
Expand Down Expand Up @@ -273,7 +273,7 @@
logout
end

subject { Nokogiri::XML::DocumentFragment.parse(kiwi_image.to_xml) }
subject { Nokogiri::XML::Document.parse(kiwi_image.to_xml) }

it 'returns the xml for the kiwi image correctly' do
expect(subject.errors).to be_empty
Expand Down Expand Up @@ -315,7 +315,7 @@
let(:package) { create(:package) }
let(:kiwi_image) { Kiwi::Image.build_from_xml(kiwi_xml, 'some_md5') }

subject { Nokogiri::XML::DocumentFragment.parse(kiwi_image.to_xml) }
subject { Nokogiri::XML::Document.parse(kiwi_image.to_xml) }

before do
allow(package).to receive(:kiwi_image_file).and_return('config.kiwi')
Expand All @@ -335,7 +335,7 @@
let(:package) { create(:package) }
let(:kiwi_image) { Kiwi::Image.build_from_xml(Kiwi::Image::DEFAULT_KIWI_BODY, 'some_md5') }

subject { Nokogiri::XML::DocumentFragment.parse(kiwi_image.to_xml) }
subject { Nokogiri::XML::Document.parse(kiwi_image.to_xml) }

before do
allow(package).to receive(:kiwi_image_file).and_return('config.kiwi')
Expand Down

0 comments on commit db6bda1

Please sign in to comment.