Skip to content

Commit

Permalink
Fixed license? method and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator authored and imobachgs committed Apr 5, 2018
1 parent 47f8b5a commit 5c3fb71
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion library/packages/src/lib/y2packager/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Y2Packager
# This class stores the license ID and the traslated content of the license
# for different languages.
class License
DEFAULT_LANG = "en_US"
DEFAULT_LANG = "en_US".freeze
# @return [String] License unique identifier
attr_reader :id

Expand Down
2 changes: 1 addition & 1 deletion library/packages/src/lib/y2packager/license_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def license
content = fetcher.license_content(License::DEFAULT_LANG)
return unless content

store.add_license_for(product_name, License.new(:content => content))
store.add_license_for(product_name, License.new(content: content))
end

# Return the license text
Expand Down
22 changes: 21 additions & 1 deletion library/packages/src/lib/y2packager/license_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,49 @@
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require 'singleton'
require "singleton"

module Y2Packager
# This class is responsible for storing the relation between products and
# licenses. A license could be the same for multiple products and it is
# determined by the license id.
class LicenseStore
include Singleton

attr_reader :product_licenses

# Constructor
def initialize
@product_licenses = {}
end

# Return the license for the given product name
#
# @return [License,nil]
def license_for(product_name)
@product_licenses[product_name]
end

# Stores the given license for the given product name if there is not
# already a license with the same id. In case that there is already a
# license with the same id that license is used and returned instead.
#
# @param product_name [String]
# @param license [License]
# @return [License] The given license or the already stored one with the
# same id.
def add_license_for(product_name, license)
stored_license = license(license.id)

@product_licenses[product_name] = stored_license || license
end

private

# Looks for a license with the given id
#
# @param id [String] of the license to be found
# @return [License, nil] a license with the given id or nil if not found
def license(id)
@product_licenses.values.find { |l| l.id == id }
end
Expand Down
5 changes: 5 additions & 0 deletions library/packages/src/lib/y2packager/license_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
require "yast"

module Y2Packager
# This class is responsible for storing the different license translations.
class LicenseTranslation
# @return [String] Content Language
attr_reader :lang
attr_reader :content

# Constructor
#
# @param language [String]
# @param content [String] of the license for the given language
def initialize(language:, content:)
@lang = language
@content = content
Expand Down
4 changes: 4 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers/rpm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

module Y2Packager
module LicensesFetchers
# This class is responsible for obtaining the license and license content
# of a given product from libzypp.
#
# FIXME: Finish implementation
class Rpm < Base
# Return the license text to be confirmed
#
Expand Down
5 changes: 3 additions & 2 deletions library/packages/src/lib/y2packager/licenses_fetchers/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

module Y2Packager
module LicensesFetchers
# This class reads the licenses from the eula_url product property
#
# FIXME: Finish implementation
class Url < Base

def license_content(lang)
def license_content(_lang)
"Fetching product license"
end

Expand Down
6 changes: 2 additions & 4 deletions library/packages/src/lib/y2packager/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,8 @@ def license_content(lang)
#
# @param lang [String] Language
# @return [Boolean] true if the product has a license
def license?(lang)
content = license(lang)
return false unless content
content != ""
def license?
license ? true : false
end

# Determine whether the license should be accepted or not
Expand Down
40 changes: 13 additions & 27 deletions library/packages/test/y2packager/product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,72 +184,58 @@
end
end

describe "#license" do
let(:license) { "license content" }
describe "#license_content" do
let(:license_content) { "license content" }
let(:lang) { "en_US" }

before do
allow(Yast::Pkg).to receive(:PrdGetLicenseToConfirm).with(product.name, lang)
.and_return(license)
.and_return(license_content)
allow(product.license_reader).to receive(:license_content).and_return(license_content)
end

it "return the license" do
expect(product.license(lang)).to eq(license)
it "return the license content" do
expect(product.license_content(lang)).to eq(license_content)
end

context "when the no license to confirm was found" do
let(:license) { "" }

it "return the empty string" do
expect(product.license(lang)).to eq("")
expect(product.license_content(lang)).to eq("")
end
end

context "when the product does not exist" do
let(:license) { nil }

it "return nil" do
expect(product.license(lang)).to be_nil
expect(product.license_content(lang)).to be_nil
end
end
end

describe "#license?" do
let(:lang) { "en_US" }
let(:license) { "" }
let(:license) { instance_double("Y2Packager::License") }

before do
allow(product).to receive(:license).and_return(license)
end

context "when product has a license" do
let(:license) { "license content" }

it "returns the license content" do
expect(product.license?(lang)).to eq(true)
it "returns true" do
expect(product.license?).to eq(true)
end
end

context "when product does not have a license" do
let(:license) { "" }

it "returns false" do
expect(product.license?(lang)).to eq(false)
end
end

context "when product is not found" do
let(:license) { nil }

it "returns nil" do
expect(product.license?(lang)).to eq(false)
it "returns false" do
expect(product.license?).to eq(false)
end
end

it "asks for the license in the given language" do
expect(product).to receive(:license).with(lang)
product.license?(lang)
end
end

describe "#license_locales" do
Expand Down

0 comments on commit 5c3fb71

Please sign in to comment.