Skip to content

Commit

Permalink
Merge bfd42a9 into 86774ec
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jun 18, 2020
2 parents 86774ec + bfd42a9 commit 1076d7c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
11 changes: 6 additions & 5 deletions library/xml/src/modules/XML.rb
Expand Up @@ -23,6 +23,7 @@
require "yast"

require "nokogiri"
require "pathname"

module Yast
# Exception used when trying to serialize a Ruby object that we cannot
Expand Down Expand Up @@ -191,19 +192,19 @@ def XMLToYCPString(xml_string)

# Validates a XML document against a given schema.
#
# @param xml [String] path or content of XML (if the string contains a new
# @param xml [Pathname, String] path or content of XML (if the string contains a new
# line character it is considered as a XML string, otherwise as a file name)
# @param schema [String] path or content of relax ng schema
# @param schema [Pathname, String] path or content of relax ng schema
# @return [Array<String>] array of strings with errors or empty array if
# well formed and valid
# @raise [Yast::XMLDeserializationError] if the document is not well formed
# (there are syntax errors)
def validate(xml, schema)
xml = SCR.Read(path(".target.string"), xml) unless xml.include?("\n")
if schema.include?("\n") # content, not path
xml = SCR.Read(path(".target.string"), xml.to_s) if xml.is_a?(Pathname)
if schema.is_a?(::String)
validator = Nokogiri::XML::RelaxNG(schema)
else
schema_content = SCR.Read(path(".target.string"), schema)
schema_content = SCR.Read(path(".target.string"), schema.to_s)
schema_path = File.dirname(schema)
# change directory so relative include works
Dir.chdir(schema_path) { validator = Nokogiri::XML::RelaxNG(schema_content) }
Expand Down
47 changes: 34 additions & 13 deletions library/xml/test/xml_test.rb
@@ -1,4 +1,5 @@
require_relative "test_helper"
require "tempfile"

Yast.import "XML"

Expand Down Expand Up @@ -524,20 +525,22 @@
</element>'
end

it "returns empty array for valid xml" do
xml = '<?xml version="1.0"?>
<test>
<person>
<name>
clark
</name>
<voice>
nice
</voice>
</person>
</test>'
let(:valid_xml) do
'<?xml version="1.0"?>
<test>
<person>
<name>
clark
</name>
<voice>
nice
</voice>
</person>
</test>'
end

expect(Yast::XML.validate(xml, schema)).to be_empty
it "returns empty array for valid xml" do
expect(Yast::XML.validate(valid_xml, schema)).to be_empty
end

it "returns error string in array when xml is not valid for given schema" do
Expand Down Expand Up @@ -569,5 +572,23 @@

expect { Yast::XML.validate(xml, schema) }.to raise_error(Yast::XMLDeserializationError)
end

it "can read the input from files" do
# create temporary files with the testing content
xml_file = Tempfile.new
schema_file = Tempfile.new
begin
xml_file.write(valid_xml)
xml_file.close
schema_file.write(schema)
schema_file.close

errors = Yast::XML.validate(Pathname.new(xml_file.path), Pathname.new(schema_file.path))
expect(errors).to be_empty
ensure
xml_file.unlink
schema_file.unlink
end
end
end
end
9 changes: 9 additions & 0 deletions package/yast2.changes
@@ -1,3 +1,12 @@
-------------------------------------------------------------------
Thu Jun 18 15:23:54 UTC 2020 - Ladislav Slezák <lslezak@suse.cz>

- Updated Yast::XML.validate arguments
- Distinguish between a String argument (containing a XML
document/schema) and Pathname (path to a file)
- Related to bsc#1170886
- 4.3.8

-------------------------------------------------------------------
Tue Jun 16 14:01:51 UTC 2020 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2
Version: 4.3.7
Version: 4.3.8
Release: 0
Summary: YaST2 Main Package
License: GPL-2.0-only
Expand Down

0 comments on commit 1076d7c

Please sign in to comment.