diff --git a/.gitignore b/.gitignore index ffefd5b..58d4d12 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ assets/vendor/qtip2/basic Gemfile.lock /.sass-cache .ruby-version +/tmp diff --git a/Gemfile b/Gemfile index bf1cd2b..5df1765 100644 --- a/Gemfile +++ b/Gemfile @@ -7,4 +7,5 @@ group :test do gem "html-proofer" gem "rake" gem "rspec" + gem "nokogiri" end diff --git a/spec/license_spec.rb b/spec/license_spec.rb index fe4b73b..f09ad3d 100644 --- a/spec/license_spec.rb +++ b/spec/license_spec.rb @@ -1,36 +1,31 @@ require 'spec_helper' -# List of licenses that need not be OSI approved -LICENSE_WHITELIST = %w[ - unlicense - cc0-1.0 - wtfpl - bsd-3-clause-clear -] - licenses.each do |license| + + # "No license" isn't really a license, so no need to test + next if license["id"] == "no-license" + describe "The #{license["title"]} license" do - describe "SPDX compliance" do - # "No license" isn't really a license, so no need to test - unless license["id"] == "no-license" - it "#{license["id"]} should be a valid SPDX ID" do - expect(find_spdx(license["id"])).to_not be_nil - end - it "should be the proper SPDX name" do - spdx = find_spdx(license["id"]) - expect(spdx[1]["name"].gsub(/ only$/,"")).to eql(license["title"]) - end + let(:id) { license["id"] } - # CC0 and Unlicense are not OSI approved, but that's okay - unless LICENSE_WHITELIST.include? license["id"] - it "should be OSI approved" do - spdx = find_spdx(license["id"]) - approved = spdx[1]["osiApproved"] - expect(approved).to eql(true) - end - end - end + it "has an SPDX ID" do + expect(spdx_ids).to include(id) + end + + it "uses its SPDX name" do + spdx = find_spdx(id) + expect(spdx[1]["name"].gsub(/ only$/,"")).to eql(license["title"]) + end + + it "should be approved by OSI or FSF or OD" do + osi = osi_approved?(id) + fsf = fsf_approved?(id) + od = od_approved?(id) + + approved = osi || fsf || od + msg = "The license must be approved by OSI, FSF, or OD. See https://git.io/vzCTV." + expect(approved).to eql(true), msg end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 49dd589..ceefde9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,8 @@ require 'jekyll' require 'open-uri' require 'json' +require 'open-uri' +require 'nokogiri' def config_file File.expand_path "./_config.yml", source @@ -54,6 +56,54 @@ def spdx_list $spdx ||= JSON.parse(open(url).read) end +def spdx_ids + spdx_list.map { |name, properties| name.downcase } +end + def find_spdx(license) spdx_list.find { |name, properties| name.downcase == license } end + +def osi_approved?(id) + spdx = find_spdx(id) + return false unless spdx + spdx[1]["osiApproved"] +end + +def fsf_approved_licenses + $fsf_approved_licenses ||= begin + url = "https://www.gnu.org/licenses/license-list.en.html" + doc = Nokogiri::HTML(open(url).read) + list = doc.css(".green dt") + licenses = {} + list.each do |license| + a = license.css("a").find { |link| !link.text.nil? && !link.text.empty? && link.attr("id") } + next if a.nil? + id = a.attr("id").downcase + name = a.text.strip + licenses[id] = name + end + licenses + end +end + +def fsf_approved?(id) + fsf_approved_licenses.keys.include?(id) +end + +def od_approved_licenses + $od_approved_licenses ||= begin + url = "http://licenses.opendefinition.org/licenses/groups/od.json" + data = open(url).read + data = JSON.parse(data) + licenses = {} + data.each do |id, meta| + licenses[id.downcase] = meta["title"] + end + licenses + end +end + +def od_approved?(id) + od_approved_licenses.keys.include?(id) +end