Skip to content

Commit

Permalink
Add a Y2Packager::ProductLicense class
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 4, 2018
1 parent 16404fc commit 10c8867
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
77 changes: 77 additions & 0 deletions library/packages/src/lib/y2packager/product_license.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ------------------------------------------------------------------------------
# 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 "yast"
require "forwardable"
require "y2packager/license"

module Y2Packager
class ProductLicense
# This class holds the license stuff for a given product
#
# Why a separate ProductLicense class? First of all, we wanted to extract
# the license handling from Y2Packager::Product and moving this logic to
# Y2Packager::License was not a good idea because different products could
# share the same license. Additionally, this class offers an API to work
# with licenses with a proper Product or Addon object is not available
# (backward compatibility reasons).
#
# @see Y2Packager::Product
# @see Y2Packager::License

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

attr_reader :license

class << self
# Find license for a given product
#
# @param product_name [String] Product's name
# @param source [:rpm,:url] Source to get the license from. For the time being,
# only :rpm is really supported.
# @return [ProductLicense]

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)
end

# Clear product licenses cache
def clear_cache
@cache = nil
end

private def cache
@cache ||= {}
end
end

# Constructor
#
# @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)
@product_name = product_name
@license = license
end

# Determines whether the license have been accepted or not
#
# @return [Boolean] true if the license has been accepted; false otherwise.
def accepted?
license.accepted?
end
end
end
92 changes: 92 additions & 0 deletions library/packages/test/y2packager/product_license_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/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::ProductLicense do
subject(:product_license) { Y2Packager::ProductLicense.new(product_name, license) }

let(:license) { instance_double(Y2Packager::License) }
let(:product_name) { "SLES" }

before do
described_class.clear_cache
end

describe ".find" do
before do
allow(Y2Packager::License).to receive(:find).and_return(license)
end

it "returns a product license for the given product" do
expect(Y2Packager::License).to receive(:find).with("SLES", :rpm).and_return(license)
product_license = described_class.find("SLES", :rpm)
expect(product_license).to be_a(Y2Packager::ProductLicense)
expect(product_license.license).to eq(license)
end

context "when the product license was already built" do
it "returns the already built instance" do
cached_product_license = described_class.find("SLES", :rpm)
product_license = described_class.find("SLES", :rpm)
expect(product_license).to be(cached_product_license)
end
end

context "when license does not have an id" do
it "returns nil"
end
end

describe "#content_for" do
it "delegates to License#content_for" do
expect(license).to receive(:content_for).with("es_ES").and_return("contenido")
expect(product_license.content_for("es_ES")).to eq("contenido")
end
end

describe "#locales" do
it "delegates to License#locales" do
expect(license).to receive(:locales).and_return(["en_US", "de_DE"])
expect(product_license.locales).to eq(["en_US", "de_DE"])
end
end

describe "#accept!" do
it "delegates to License#accept!" do
expect(license).to receive(:accept!)
product_license.accept!
end
end

describe "#reject!" do
it "delegates to License#reject!" do
expect(license).to receive(:reject!)
product_license.reject!
end
end

describe "#accepted?" do
context "if the product license has been accepted" do
it "returns true"
end

context "if the product license has been accepted" do
it "returns false"
end

context "synchronizes the license status"
end
end

0 comments on commit 10c8867

Please sign in to comment.