Skip to content

Commit

Permalink
handle exceptions in autoyast classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jul 22, 2020
1 parent 9430852 commit 113687b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/modules/AutoinstClass.rb
Expand Up @@ -53,10 +53,13 @@ def findPath(name, class_)
def Read
if SCR.Read(path(".target.size"), @classPath) != -1 &&
Y2Autoinstallation::XmlChecks.valid_classes?(@classPath)

# TODO: use XML module
classes_map = Convert.to_map(SCR.Read(path(".xml"), @classPath))
@Classes = (classes_map && classes_map["classes"]) || []
begin
classes_map = XML.XMLToYCPFile(@classPath)
@Classes = (classes_map && classes_map["classes"]) || []
rescue XMLDeserializationError => e
log.error "failed to parse classes xml file #{e.inspect}"
@Classes = []
end
else
@Classes = []
end
Expand All @@ -74,6 +77,8 @@ def Compat
XML.YCPToXMLFile(:class, new_classes_map, @classPath)
end
nil
rescue XMLSerializationError => e
log.error "Failed to compact object #{e.inspect}"
end

# Changes the directory and reads the class definitions
Expand Down Expand Up @@ -164,6 +169,9 @@ def Save
tmp = { "classes" => @Classes }
log.debug "saving classes: #{@classPath}"
XML.YCPToXMLFile(:class, tmp, @classPath)
rescue XMLSerializationError => e
log.error "Failed to save object #{e.inspect}"
false
end

# Imports configuration
Expand Down
20 changes: 20 additions & 0 deletions test/AutoinstClass_test.rb
Expand Up @@ -31,6 +31,20 @@
end
end

context "when class definition is not valid" do
it "sets Classes to []" do
allow(Yast::XML).to receive(:XMLToYCPFile).and_raise(Yast::XMLDeserializationError)
subject.Read
expect(subject.Classes).to eq([])
end

it "returns nil" do
allow(Yast::XML).to receive(:XMLToYCPFile).and_raise(Yast::XMLDeserializationError)
expect(subject.Read).to be_nil
end

end

context "when classes definition file does not exist" do
before(:each) do
allow(Yast::SCR).to receive(:Read).with(Yast::Path.new(".target.size"), CLASS_PATH)
Expand Down Expand Up @@ -376,6 +390,12 @@
subject.Save
end

it "does not raise exception when serialization failed" do
subject.Classes = nil
expect{subject.Save}.to_not raise_error
subject.Classes = []
end

context "when classes are marked for deletion" do
around(:each) do |example|
subject.deletedClasses = ["swap"]
Expand Down

0 comments on commit 113687b

Please sign in to comment.