Skip to content

Commit

Permalink
Read products specification
Browse files Browse the repository at this point in the history
* Add a ProductSpecReader#products method to get the products.
  • Loading branch information
imobachgs committed Oct 5, 2021
1 parent d3b91a4 commit 6f449f6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/lib/y2packager/product_spec_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2packager/product_spec_readers/full"
require "y2packager/product_spec_readers/libzypp"
require "y2packager/product_spec_readers/control"

module Y2Packager
Expand All @@ -28,7 +30,29 @@ class ProductSpecReader
#
# @return [Y2Packager::ProductSpec] List of product specifications
def products
Y2Packager::ProductSpecReaders::Control.new.products
products_from_control || products_from_offline || products_from_libzypp
end

private

def products_from_control
control_products = Y2Packager::ProductSpecReaders::Control.new.products
return nil if control_products.empty?

control_products
end

def products_from_offline
offline_products = Y2Packager::ProductSpecReaders::Full.new.products(
Yast::InstURL.installInf2Url("")
)
return nil if offline_products.empty?

offline_products
end

def products_from_libzypp
Y2Packager::ProductSpecReaders::Libzypp.new.products
end
end
end
70 changes: 70 additions & 0 deletions test/lib/product_spec_reader_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"
require "y2packager/product_spec_reader"

describe Y2Packager::ProductSpecReader do
let(:reader) { described_class.new }

let(:full_reader) do
instance_double(Y2Packager::ProductSpecReaders::Full, products: full_products)
end

let(:control_reader) do
instance_double(Y2Packager::ProductSpecReaders::Control, products: control_products)
end

let(:libzypp_reader) do
instance_double(Y2Packager::ProductSpecReaders::Libzypp, products: libzypp_products)
end

let(:full_products) { [instance_double(Y2Packager::ProductSpec, name: "SLES")] }
let(:control_products) { [instance_double(Y2Packager::ProductSpec, name: "SLED")] }
let(:libzypp_products) { [instance_double(Y2Packager::ProductSpec, name: "SLE-HA")] }

describe "#products" do
before do
allow(Y2Packager::ProductSpecReaders::Full).to receive(:new).and_return(full_reader)
allow(Y2Packager::ProductSpecReaders::Control).to receive(:new).and_return(control_reader)
allow(Y2Packager::ProductSpecReaders::Libzypp).to receive(:new).and_return(libzypp_reader)
end

it "returns products from the control file" do
expect(reader.products).to eq(control_products)
end

context "when no online products are found" do
let(:control_products) { [] }

it "returns offline products" do
expect(reader.products).to eq(full_products)
end
end

context "when no online nor offline products are found" do
let(:full_products) { [] }
let(:control_products) { [] }

it "returns the libzypp products" do
expect(reader.products).to eq(libzypp_products)
end
end
end
end

0 comments on commit 6f449f6

Please sign in to comment.