Skip to content

Commit

Permalink
Avoid duplicate error reports
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jul 29, 2020
1 parent e846106 commit b659289
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/lib/autoinstall/xml_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def self.valid_classes?(file)
# @return [Boolean] true if valid, false otherwise
def self.check(file, schema, msg)
validator = XmlValidator.new(file, schema)
return true if validator.valid?
return true if validator.valid? || validator.errors_stored?

if ENV["YAST_SKIP_XML_VALIDATION"] == "1"
log.warn "Skipping invalid XML!"
Expand All @@ -94,7 +94,10 @@ def self.check(file, schema, msg)
buttons: :continue_cancel,
focus: :cancel) == :continue

log.warn "Skipping invalid XML on user request!" if ret
if ret
log.warn "Skipping invalid XML on user request and storing validation errors!"
validator.store_errors
end

ret
end
Expand Down
19 changes: 19 additions & 0 deletions src/lib/autoinstall/xml_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

require "yast"
require "pathname"
require "yaml"

Yast.import "XML"

Expand All @@ -27,6 +28,8 @@ module Y2Autoinstallation
class XmlValidator
include Yast::Logger

STORE_DIR = "/var/lib/YaST2/"

attr_reader :xml, :schema

# Constructor
Expand Down Expand Up @@ -56,8 +59,24 @@ def valid?
errors.empty?
end

def store_errors
File.write(errors_file_path, errors.to_yaml)
end

def errors_stored?
File.exist?(errors_file_path)
end

private

def id
Digest::MD5.hexdigest(errors.to_yaml)
end

def errors_file_path
File.join(STORE_DIR, "xml_validation_#{id}.yml")
end

def validate
log.info "Validating #{xml} against #{schema}..."
@errors = Yast::XML.validate(Pathname.new(xml), Pathname.new(schema))
Expand Down
7 changes: 7 additions & 0 deletions test/lib/xml_checks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@

context "valid profile" do
let(:valid) { true }

it "returns true" do
described_class.valid_profile?
end
end

context "invalid profile" do
let(:valid) { false }

it "returns false" do
described_class.valid_profile?
end
end
end
end
34 changes: 34 additions & 0 deletions test/lib/xml_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,38 @@
expect(subject.errors).to eq(["error"])
end
end

describe "#store_errors" do
it "writes current errors to the filesystem in YAML format" do
stub_const("Y2Autoinstallation::XmlValidator::STORE_DIR", FIXTURES_PATH)
errors_file_path = subject.send(:errors_file_path)
expect(File).to receive(:write).with(errors_file_path, subject.errors.to_yaml)

subject.store_errors
end
end

describe "#errors_stored?" do
before do
stub_const("Y2Autoinstallation::XmlValidator::STORE_DIR", FIXTURES_PATH)
end

after do
errors_file_path = subject.send(:errors_file_path)
File.delete(errors_file_path) if File.exist?(errors_file_path)
end

context "when there is already a file for the errors reported" do
it "returns true" do
subject.store_errors
expect(subject.errors_stored?).to eq(true)
end
end

context "when there is no file with the reported errors" do
it "returns false" do
expect(subject.errors_stored?).to eq(false)
end
end
end
end

0 comments on commit b659289

Please sign in to comment.