-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Y2Packager::ProductLicense class
- Loading branch information
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |