Skip to content
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

Extend unit test for existing functions #23

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions spec/OneClickInstallWorkerFunctions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,80 @@
end
end
end

describe '#InstallPackages' do
before do
allow(Yast::Pkg).to receive(:ResolvableInstallRepo).and_return(true)
end
context "when package depedencies are resolved" do
before do
allow(Yast::Pkg).to receive(:PkgSolve).and_return(true)
end
it "installs packages" do
expect(subject.InstallPackages("vim")).to be true
end
end
context "when package depedencies are not resolved" do
before do
allow(Yast::Pkg).to receive(:PkgSolve).and_return(false)
end
it "installs packages if package selection is accepted" do
allow(Yast::PackagesUI).to receive(:RunPackageSelector).and_return(:accept)
expect(subject.InstallPackages("vim")).to be true
end
it "does not install packages if package selection is not accepted" do
allow(Yast::PackagesUI).to receive(:RunPackageSelector).and_return(:cancel)
expect(subject.InstallPackages("vim")).to be false
end
end
end

describe '#InstallPatterns' do
context "when pattern depedencies are resolved" do
before do
allow(Yast::Pkg).to receive(:PkgSolve).and_return(true)
end
it "installs pattern" do
expect(subject.InstallPatterns("gnome")).to be true
end
end
context "when pattern depedencies are not resolved" do
before do
allow(Yast::Pkg).to receive(:PkgSolve).and_return(false)
end
it "installs pattern if package selection is accepted" do
allow(Yast::PackagesUI).to receive(:RunPackageSelector).and_return(:accept)
expect(subject.InstallPatterns("gnome")).to be true
end
it "does not install pattern if package selection is not accepted" do
allow(Yast::PackagesUI).to receive(:RunPackageSelector).and_return(:cancel)
expect(subject.InstallPatterns("gnome")).to be false
end
end
end

describe '#RemovePackages' do
context "when package depedensies are resolved" do
before do
allow(Yast::Pkg).to receive(:PkgSolve).and_return(true)
end
it "removes installed package" do
expect(subject.RemovePackages("vim")).to be true
end
end
context "when package depedencies are not resolved" do
before do
allow(Yast::Pkg).to receive(:PkgSolve).and_return(false)
end
it "removes installed packages if package selection is accepted" do
allow(Yast::PackagesUI).to receive(:RunPackageSelector).and_return(:accept)
expect(subject.RemovePackages("vim")).to be true
end
it "does not remove installed packages if package selection is not accepted" do
allow(Yast::PackagesUI).to receive(:RunPackageSelector).and_return(:cancel)
expect(subject.RemovePackages("vim")).to be false
end
end
end

end
176 changes: 156 additions & 20 deletions spec/OneClickInstall_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,172 @@
describe Yast::OneClickInstall do
let(:filepath) { File.join(DATA_PATH, "vim.ymp") }
let(:output) { "" }
let(:urls) { ["http://download.opensuse.org/tumbleweed/repo/oss/",
"http://download.opensuse.org/tumbleweed/repo/non-oss/"] }
let!(:repositories) { subject.instance_variable_set(:@repositories,
{ urls[0] => { "name" => "oss" , "recommended" => "true" },
urls[1] => { "name" => "non-oss" , "recommended" => "false" }}) }
let!(:software) { subject.instance_variable_set(:@software,
{ "vim" => { "recommended" => "true" , "action" => "install" , "type" => "package" },
"tftp" => { "recommended" => "false" , "action" => "install" , "type" => "package" },
"x11" => { "recommended" => "true" , "action" => "install" , "type" => "pattern" },
"sap_server" => { "recommended" => "true" , "action" => "remove" , "type" => "pattern" }}) }

describe "#xml_from_file" do
context "ymp file parsed" do
it "parses the XML file" do
doc = subject.xml_from_file(filepath)
expect(doc).to be_instance_of(REXML::Document)
doc.write(output)
expect(output).to include("vim")
end
it "parses the XML file" do
doc = subject.xml_from_file(filepath)
expect(doc).to be_instance_of(REXML::Document)
doc.write(output)
expect(output).to include("vim")
end
end

describe "#xpath_match" do
context "matching node" do
it "returns and array of elements, matching xpath" do
doc = REXML::Document.new(File.read(filepath))
expect(subject.xpath_match(doc, "metapackage//url").first.text).to eq("http://download.opensuse.org/tumbleweed/repo/oss/")
end
it "returns and array of elements, matching xpath" do
doc = REXML::Document.new(File.read(filepath))
expect(subject.xpath_match(doc, "metapackage//url").first.text).to eq(urls[0])
end
end

describe "#SetRequiredRepository" do
context "set repository to recommended" do
it "Ensures that the repository with the specified URL is selected for addition" do
url = "http://download.opensuse.org/tumbleweed/repo/oss/"
subject.instance_variable_set(:@repositories, { url => { "name" => "vim" , "recommended" => "false" }})
subject.SetRequiredRepository(url)
repos = subject.instance_variable_get(:@repositories)
expect(repos[url]["name"]).to eql("vim")
expect(repos[url]["recommended"]).to eql("true")
it "Ensures that the repository with the specified URL is selected for addition" do
subject.instance_variable_set(:@repositories, { urls[0] => { "name" => "oss" , "recommended" => "false" }})
subject.SetRequiredRepository(urls[0])
repos = subject.instance_variable_get(:@repositories)
expect(repos[urls[0]]).to match_array( "name" => "oss" , "recommended" => "true" )
end
end

describe "#SetNonRequiredRepository" do
it "Ensures that the repository with the specified URL is not selected for addition" do
subject.instance_variable_set(:@repositories, { urls[1] => { "name" => "non-oss" , "recommended" => "true" }})
subject.SetNonRequiredRepository(urls[1])
repos = subject.instance_variable_get(:@repositories)
expect(repos[urls[1]]["name"]).to eql("non-oss")
expect(repos[urls[1]]["recommended"]).to eql("false")
end
end

describe "#SetRequiredRepositories" do
it "Ensures that the repositories with the specified URL are selected for addition" do
subject.SetRequiredRepositories(urls)
repos = subject.instance_variable_get(:@repositories)
expect(repos[urls[0]]).to match_array( "name" => "oss" , "recommended" => "true" )
expect(repos[urls[1]]).to match_array( "name" => "non-oss" , "recommended" => "true" )
end
end

describe "#GetRequiredRepositories" do
it "Returns a list of the URLs of the repositories currently selected for addition" do
repos = subject.GetRequiredRepositories
expect(repos).to match_array([urls[0]])
end
end

describe "#GetNonRequiredRepositories" do
it "Returns a list of the URLs of the repositories currently not selected for addition" do
repos = subject.GetNonRequiredRepositories
expect(repos).to match_array(urls[1])
end
end

describe "#GetRequiredSoftware" do
it "Returns a list of the names of the software currently selected for installation" do
software_to_install = subject.GetRequiredSoftware
expect(software_to_install).to match_array(["vim" , "x11"])
end
end

describe "#GetRequiredRemoveSoftware" do
it "Returns a list of the names of the software currently selected for removal" do
software_to_remove = subject.GetRequiredRemoveSoftware
expect(software_to_remove).to match_array(["sap_server"])
end
end

describe "#GetRequiredPackages" do
it "Returns a list of the names of the packages currently selected for installation" do
packages_to_install = subject.GetRequiredPackages
expect(packages_to_install).to match_array(["vim"])
end
end

describe "#GetRequiredPatterns" do
it "Returns a list of the names of the patterns currently selected for installation" do
patterns_to_install = subject.GetRequiredPatterns
expect(patterns_to_install).to match_array(["x11"])
end
end

describe "#HaveAnyRecommended" do
it "Returns boolean, depending on the existence of any recommended repositories or software" do
any_recommended = subject.HaveAnyRecommended
expect(any_recommended).to be true
end
end

describe "#makeXMLFriendly" do
it "converts map structure (eg. @repositories) to a list of maps with a key element" do
flattened = subject.makeXMLFriendly(software)
expect(flattened).to match_array([
{"recommended"=>"true", "action"=>"remove", "type"=>"pattern", "key"=>"sap_server"},
{"recommended"=>"false", "action"=>"install", "type"=>"package", "key"=>"tftp"},
{"recommended"=>"true", "action"=>"install", "type"=>"package", "key"=>"vim"},
{"recommended"=>"true", "action"=>"install", "type"=>"pattern", "key"=>"x11"}])
end
end

describe "#fromXMLFriendly" do
it "converts back from makeXMLFriendly to original structure" do
flattened = [
{"recommended"=>"true", "action"=>"remove", "type"=>"pattern", "key"=>"sap_server"},
{"recommended"=>"false", "action"=>"install", "type"=>"package", "key"=>"tftp"},
{"recommended"=>"true", "action"=>"install", "type"=>"package", "key"=>"vim"},
{"recommended"=>"true", "action"=>"install", "type"=>"pattern", "key"=>"x11"}]
unflattened = subject.fromXMLFriendly(flattened)
expect(unflattened).to match_array(software)
end
end

describe "#ToXML" do
it "serializes to XML file, according to OneClickInstall data structure" do
output_file = File.join(DATA_PATH, "serialized_result.xml")
subject.ToXML(output_file)
result = File.read(output_file)
expected_file = File.join(DATA_PATH, "serialized_expected.xml")
expected = File.read(expected_file)
expect(result).to eql(expected)
File.delete(output_file)
end
end

describe "#FromXML" do
it "deserializes according to OneClickInstall data structure to XML" do
filename = File.join(DATA_PATH, "to_deserialize.xml")
subject.FromXML(filename)
expect(subject.instance_variable_get(:@software)).to match_array({"zdoom"=>{"action"=>"install", "recommended"=>"false", "type"=>"package"}})
expect(subject.instance_variable_get(:@repositories)).to match_array({"https://ftp.gwdg.de/pub/linux/misc/packman/suse/openSUSE_Tumbleweed/"=>{"name"=>"packman", "recommended"=>"false"}})
expect(subject.instance_variable_get(:@remainSubscribed)).to be true
expect(subject.instance_variable_get(:@summary)).to be_empty
expect(subject.instance_variable_get(:@description)).to be_empty
expect(subject.instance_variable_get(:@name)).to be_empty
end
end

describe "#Load" do
context "when loading YMP file" do
it "loads the Metapackage data from the YML file supplied for further processing" do
subject.Load(filepath)
expect(subject.instance_variable_get(:@repositories)[urls[0]]).to include("name"=>"openSUSE:Factory",
"summary"=>"The next openSUSE distribution",
"recommended"=>"true")
expect(subject.instance_variable_get(:@software)["vim"]).to include("summary"=>"Vi IMproved",
"recommended"=>"true",
"action"=>"install",
"type"=>"package")
expect(subject.instance_variable_get(:@name)).to eql("vim")
expect(subject.instance_variable_get(:@remainSubscribed)).to be true
expect(subject.instance_variable_get(:@description)).to include("Vim (Vi IMproved) is an almost compatible version of the UNIX editor")
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions spec/data/serialized_expected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<!DOCTYPE OneClickInstall SYSTEM "/un/defined">
<OneClickInstall xmlns="http://www.suse.com/1.0/yast2ns" xmlns:config="http://www.suse.com/1.0/configns">
<description/>
<name/>
<remainSubscribed t="boolean">true</remainSubscribed>
<repositories t="list">
<repository t="map">
<key>http://download.opensuse.org/tumbleweed/repo/non-oss/</key>
<name>non-oss</name>
<recommended>false</recommended>
</repository>
<repository t="map">
<key>http://download.opensuse.org/tumbleweed/repo/oss/</key>
<name>oss</name>
<recommended>true</recommended>
</repository>
</repositories>
<software t="list">
<listentry t="map">
<action>remove</action>
<key>sap_server</key>
<recommended>true</recommended>
<type>pattern</type>
</listentry>
<listentry t="map">
<action>install</action>
<key>tftp</key>
<recommended>false</recommended>
<type>package</type>
</listentry>
<listentry t="map">
<action>install</action>
<key>vim</key>
<recommended>true</recommended>
<type>package</type>
</listentry>
<listentry t="map">
<action>install</action>
<key>x11</key>
<recommended>true</recommended>
<type>pattern</type>
</listentry>
</software>
<summary/>
</OneClickInstall>
23 changes: 23 additions & 0 deletions spec/data/to_deserialize.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!DOCTYPE OneClickInstall SYSTEM "/un/defined">
<OneClickInstall xmlns="http://www.suse.com/1.0/yast2ns" xmlns:config="http://www.suse.com/1.0/configns">
<description/>
<name/>
<remainSubscribed t="boolean">true</remainSubscribed>
<repositories t="list">
<repository t="map">
<key>https://ftp.gwdg.de/pub/linux/misc/packman/suse/openSUSE_Tumbleweed/</key>
<name>packman</name>
<recommended>false</recommended>
</repository>
</repositories>
<software t="list">
<listentry t="map">
<action>install</action>
<key>zdoom</key>
<recommended>false</recommended>
<type>package</type>
</listentry>
</software>
<summary/>
</OneClickInstall>