1
0
mirror of https://github.com/github/choosealicense.com synced 2024-11-05 19:28:50 +01:00
choosealicense.com/spec/spec_helper.rb

135 lines
2.9 KiB
Ruby
Raw Normal View History

2015-03-07 18:47:24 +01:00
require 'jekyll'
2015-03-07 19:38:52 +01:00
require 'open-uri'
require 'json'
2016-01-15 23:40:32 +01:00
require 'open-uri'
require 'nokogiri'
2015-03-07 18:47:24 +01:00
2016-02-08 21:37:19 +01:00
module SpecHelper
class << self
attr_accessor :config, :licenses, :site, :spdx
attr_accessor :osi_approved_licenses, :fsf_approved_licenses, :od_approved_licenses
end
end
2015-03-07 18:47:24 +01:00
def config_file
2016-02-08 21:37:19 +01:00
File.expand_path './_config.yml', source
2015-03-07 18:47:24 +01:00
end
def source
2016-02-08 21:37:19 +01:00
File.expand_path('../', File.dirname(__FILE__))
2015-03-07 18:47:24 +01:00
end
2016-01-29 18:44:28 +01:00
def licenses_path
2016-02-08 21:37:19 +01:00
File.expand_path '_licenses', source
2016-01-29 18:44:28 +01:00
end
2015-03-07 18:47:24 +01:00
def config
2016-02-08 21:37:19 +01:00
SpecHelper.config ||= begin
config = Jekyll::Configuration.new.read_config_file config_file
config = Jekyll::Utils.deep_merge_hashes(config, source: source)
Jekyll::Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, config)
end
2015-03-07 18:47:24 +01:00
end
def licenses
2016-02-08 21:37:19 +01:00
SpecHelper.licenses ||= begin
site.collections['licenses'].docs.map do |license|
spdx_lcase = File.basename(license.basename, '.txt')
license.to_liquid.merge('spdx-lcase' => spdx_lcase)
2016-01-20 16:37:44 +01:00
end
2016-01-18 23:05:02 +01:00
end
end
def shown_licenses
licenses.select { |l| !l['hidden'] }
end
2015-03-07 18:47:24 +01:00
def site
2016-02-08 21:37:19 +01:00
SpecHelper.site ||= begin
2015-03-07 18:47:24 +01:00
site = Jekyll::Site.new(config)
site.reset
site.read
site
end
end
def rules
2016-02-08 21:37:19 +01:00
site.data['rules']
2015-03-07 18:47:24 +01:00
end
2015-08-22 21:25:16 +02:00
def fields
2016-02-08 21:37:19 +01:00
site.data['fields']
2015-08-22 21:25:16 +02:00
end
def meta
2016-02-08 21:37:19 +01:00
site.data['meta']
2015-08-22 21:25:16 +02:00
end
2015-03-07 18:47:24 +01:00
def rule?(tag, group)
2016-02-08 21:37:19 +01:00
rules[group].any? { |r| r['tag'] == tag }
2015-03-07 18:47:24 +01:00
end
2015-03-07 19:38:52 +01:00
def spdx_list
2016-02-08 21:37:19 +01:00
url = 'https://raw.githubusercontent.com/sindresorhus/spdx-license-list/master/spdx.json'
SpecHelper.spdx ||= JSON.parse(open(url).read)
2015-03-07 19:38:52 +01:00
end
2016-01-15 23:40:32 +01:00
def spdx_ids
spdx_list.map { |name, _properties| name }
2016-01-15 23:40:32 +01:00
end
2015-03-07 19:38:52 +01:00
def find_spdx(license)
spdx_list.find { |name, _properties| name == license }
2015-03-07 19:38:52 +01:00
end
2016-01-15 23:40:32 +01:00
2016-01-16 00:01:07 +01:00
def osi_approved_licenses
2016-02-08 21:37:19 +01:00
SpecHelper.osi_approved_licenses ||= begin
2016-01-16 00:01:07 +01:00
licenses = {}
2016-02-08 21:37:19 +01:00
list = spdx_list.select { |_id, meta| meta['osiApproved'] }
2016-01-16 00:01:07 +01:00
list.each do |id, meta|
2016-02-08 21:37:19 +01:00
licenses[id.downcase] = meta['name']
2016-01-16 00:01:07 +01:00
end
licenses
end
2016-01-15 23:40:32 +01:00
end
def fsf_approved_licenses
2016-02-08 21:37:19 +01:00
SpecHelper.fsf_approved_licenses ||= begin
url = 'https://www.gnu.org/licenses/license-list.en.html'
2016-01-15 23:40:32 +01:00
doc = Nokogiri::HTML(open(url).read)
2016-02-08 21:37:19 +01:00
list = doc.css('.green dt')
2016-01-15 23:40:32 +01:00
licenses = {}
list.each do |license|
2016-02-08 21:37:19 +01:00
a = license.css('a').find { |link| !link.text.nil? && !link.text.empty? && link.attr('id') }
2016-01-15 23:40:32 +01:00
next if a.nil?
2016-02-08 21:37:19 +01:00
id = a.attr('id').downcase
2016-01-15 23:40:32 +01:00
name = a.text.strip
licenses[id] = name
end
# FSF approved the Clear BSD, but doesn't use its SPDX ID or Name
2016-02-08 21:37:19 +01:00
if licenses.keys.include? 'clearbsd'
licenses['bsd-3-clause-clear'] = licenses['clearbsd']
end
2016-01-15 23:40:32 +01:00
licenses
end
end
def od_approved_licenses
2016-02-08 21:37:19 +01:00
SpecHelper.od_approved_licenses ||= begin
url = 'http://licenses.opendefinition.org/licenses/groups/od.json'
2016-01-15 23:40:32 +01:00
data = open(url).read
data = JSON.parse(data)
licenses = {}
data.each do |id, meta|
2016-02-08 21:37:19 +01:00
licenses[id.downcase] = meta['title']
2016-01-15 23:40:32 +01:00
end
licenses
end
end
2016-01-16 00:01:07 +01:00
def approved_licenses
(osi_approved_licenses.keys + fsf_approved_licenses.keys + od_approved_licenses.keys).flatten.uniq.sort
2016-01-15 23:40:32 +01:00
end