-
Notifications
You must be signed in to change notification settings - Fork 437
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
Kiwi image editor: Improve #to_xml to decrease the resulting diff #4002
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e7f494
[webui] Only replace default package group
ChrisBr 71195a3
[webui] Add repositories after the first repository
ChrisBr 88d18be
[webui] Parse and use correct indentation for kiwi files
ChrisBr 97542fa
[webui] Move Kiwi::Image#to_xml to dedicated class
ChrisBr a12a5be
[ci] Add specs for Kiwi::Image#to_xml
ChrisBr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# TODO: Please overwrite this comment with something explaining the model target | ||
module Kiwi | ||
class Image | ||
class Xml | ||
def initialize(image) | ||
@image = image | ||
end | ||
|
||
def to_xml | ||
doc = Nokogiri::XML::DocumentFragment.parse(kiwi_body) | ||
image = doc.at_css('image') | ||
|
||
return nil unless image && image.first_element_child | ||
|
||
doc = update_packages(doc) | ||
doc = update_repositories(doc) | ||
Nokogiri::XML(doc.to_xml, &:noblanks).to_xml(indent: kiwi_indentation(doc)) | ||
end | ||
|
||
private | ||
|
||
def update_packages(document) | ||
# for now we only write the default package group | ||
xml_packages = @image.default_package_group.to_xml | ||
packages = document.xpath('image/packages[@type="image"]').first | ||
if packages | ||
packages.replace(xml_packages) | ||
else | ||
document.at_css('image').last_element_child.after(xml_packages) | ||
end | ||
document | ||
end | ||
|
||
def update_repositories(document) | ||
repository_position = document.xpath("image/repository").first.try(:previous) || document.at_css('image').last_element_child | ||
document.xpath("image/repository").remove | ||
xml_repos = repositories_for_xml.map(&:to_xml).join("\n") | ||
repository_position.after(xml_repos) | ||
document | ||
end | ||
|
||
def repositories_for_xml | ||
if @image.use_project_repositories? | ||
[Kiwi::Repository.new(source_path: 'obsrepositories:/', repo_type: 'rpm-md')] | ||
else | ||
@image.repositories | ||
end | ||
end | ||
|
||
def kiwi_body | ||
if @image.package | ||
kiwi_file = @image.package.kiwi_image_file | ||
return nil unless kiwi_file | ||
@image.package.source_file(kiwi_file) | ||
else | ||
Kiwi::Image::DEFAULT_KIWI_BODY | ||
end | ||
end | ||
|
||
def kiwi_indentation(xml) | ||
content = xml.xpath('image').children.first.try(:content) | ||
content ? content.delete("\n").length : 2 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/api/spec/cassettes/Kiwi_Image/_to_xml/with_kiwi_image_file/1_3_3_6.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,7 +169,7 @@ | |
context 'with some repositories and packages' do | ||
before do | ||
kiwi_image.repositories << create(:kiwi_repository) | ||
kiwi_image.package_groups << create(:kiwi_package_group_non_empty) | ||
kiwi_image.package_groups << create(:kiwi_package_group_non_empty, kiwi_type: 'image') | ||
end | ||
|
||
subject { Nokogiri::XML::DocumentFragment.parse(kiwi_image.to_xml) } | ||
|
@@ -211,7 +211,8 @@ | |
it { expect(subject.errors).to be_empty } | ||
it { expect(subject.xpath('.//image').length).to be(1) } | ||
it { expect(subject.xpath('.//image/description').length).to be(1) } | ||
it { expect(subject.xpath('.//image/packages/package').length).to be(0) } | ||
it { expect(subject.xpath(".//image/packages[@type='image']/package").length).to be(0) } | ||
it { expect(subject.xpath(".//image/packages[@type='delete']/package").length).to be(0) } | ||
it { expect(subject.xpath('.//image/repository').length).to be(0) } | ||
end | ||
|
||
|
@@ -241,6 +242,44 @@ | |
|
||
it { expect(subject.to_xml).to be_nil } | ||
end | ||
|
||
context 'with a kiwi file with packages and repositories' do | ||
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) } | ||
|
||
before do | ||
allow(package).to receive(:kiwi_image_file).and_return('config.kiwi') | ||
allow(package).to receive(:source_file).and_return(kiwi_xml) | ||
kiwi_image.package = package | ||
kiwi_image.save | ||
end | ||
|
||
it { expect(subject.children[2].children[3].name).to eq('packages') } | ||
it { expect(subject.children[2].children[3].attributes['type'].value).to eq('image') } | ||
it { expect(subject.children[2].children[7].name).to eq('repository') } | ||
it { expect(subject.children[2].children[9].name).to eq('repository') } | ||
it { expect(subject.children[2].children[11].name).to eq('repository') } | ||
it { expect(subject.children[2].children[13].name).to eq('repository') } | ||
end | ||
|
||
context 'with a kiwi file without packages and repositories' do | ||
let(:package) { create(:package) } | ||
let(:kiwi_image) { Kiwi::Image.build_from_xml(Kiwi::Image::DEFAULT_KIWI_BODY, 'some_md5') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to have a factory for it? |
||
subject { Nokogiri::XML::DocumentFragment.parse(kiwi_image.to_xml) } | ||
|
||
before do | ||
allow(package).to receive(:kiwi_image_file).and_return('config.kiwi') | ||
allow(package).to receive(:source_file).and_return(Kiwi::Image::DEFAULT_KIWI_BODY) | ||
kiwi_image.package = package | ||
kiwi_image.package_groups << create(:kiwi_package_group_non_empty, kiwi_type: 'image') | ||
kiwi_image.repositories << create(:kiwi_repository) | ||
kiwi_image.save | ||
end | ||
|
||
it { expect(subject.children[2].children[5].name).to eq('packages') } | ||
it { expect(subject.children[2].children[7].name).to eq('repository') } | ||
end | ||
end | ||
|
||
describe '.write_to_backend' do | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to have a factory for it?