Skip to content

Commit

Permalink
Add license handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 4, 2018
1 parent dc7259d commit 0c0237c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
25 changes: 25 additions & 0 deletions library/packages/src/lib/y2packager/licenses_handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2018 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.
# ------------------------------------------------------------------------------

require "y2packager/licenses_handlers/rpm"

module Y2Packager
module LicensesHandlers
# Return the licenses proper fetcher for a given source
#
# @param source [Symbol] :rpm or :url
def self.for(source, product_name)
klass = const_get(source.to_s.capitalize)
klass.new(product_name)
end
end
end
46 changes: 46 additions & 0 deletions library/packages/src/lib/y2packager/licenses_handlers/rpm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2018 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.
# ------------------------------------------------------------------------------

Yast.import "Pkg"

module Y2Packager
module LicensesHandlers
class Rpm
attr_reader :product_name

# Constructor
#
# @param product_name [String] Product's name
def initialize(product_name)
@product_name = product_name
end

# Determine whether the license should be accepted or not
#
# @return [Boolean] true if the license acceptance is required
def license_confirmation_required?
Yast::Pkg.PrdNeedToAcceptLicense(product_name)
end

# Set the license confirmation for the product
#
# @param confirmed [Boolean] true if it should be accepted; false otherwise
def license_confirmation=(confirmed)
if confirmed
Yast::Pkg.PrdMarkLicenseConfirmed(product_name)
else
Yast::Pkg.PrdMarkLicenseNotConfirmed(product_name)
end
end
end
end
end
17 changes: 15 additions & 2 deletions library/packages/src/lib/y2packager/product_license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "yast"
require "forwardable"
require "y2packager/license"
require "y2packager/licenses_handlers"

module Y2Packager
class ProductLicense
Expand All @@ -29,8 +30,19 @@ class ProductLicense
# @see Y2Packager::License

extend Forwardable

def_delegators :@license, :content_for, :locales, :accept!, :reject!

# @!method license_confirmation_required?
# Determine whether the license should be accepted or not
# @return [Boolean] true if the license acceptance is required
#
# @!method license_confirmation=(confirmed)
# Set the license confirmation for the product
# @param confirmed [Boolean] true if it should be accepted; false otherwise
def_delegators :@handler, :license_confirmation_required?, :license_confirmation

# @return [License] Product's license
attr_reader :license

class << self
Expand All @@ -45,7 +57,7 @@ def find(product_name, source = :rpm)
return cache[product_name] if cache[product_name]
license = License.find(product_name, source)
return nil unless license
cache[product_name] = ProductLicense.new(product_name, license)
cache[product_name] = ProductLicense.new(product_name, license, source: source)
end

# Clear product licenses cache
Expand All @@ -62,9 +74,10 @@ def clear_cache
#
# @param product_name [String] Product name to get licenses for
# @param source [Symbol] Source to use for fetching the license from
def initialize(product_name, license)
def initialize(product_name, license, source: :rpm)
@product_name = product_name
@license = license
@handler = Y2Packager::LicensesHandlers.for(source, product_name)
end

# Determines whether the license have been accepted or not
Expand Down
45 changes: 45 additions & 0 deletions library/packages/test/y2packager/licenses_handlers/rpm_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env rspec
# ------------------------------------------------------------------------------
# Copyright (c) 2018 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.
# ------------------------------------------------------------------------------

require_relative "../../test_helper"

require "y2packager/product_license"

describe Y2Packager::LicensesHandlers::Rpm do
subject(:handler) { described_class.new(product_name) }

let(:product_name) { "SLES" }

describe "#license_confirmation_required?" do
before do
allow(Yast::Pkg).to receive(:PrdNeedToAcceptLicense)
.with(product_name).and_return(needed)
end

context "when according to libzypp the license is required to be confirmed" do
let(:needed) { true }

it "returns true" do
expect(handler.license_confirmation_required?).to eq(true)
end
end

context "when according to libzypp the license is not required to be confirmed" do
let(:needed) { false }

it "returns false" do
expect(handler.license_confirmation_required?).to eq(false)
end
end
end
end

0 comments on commit 0c0237c

Please sign in to comment.