Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

whitelist with any known variant of a license name

  • Loading branch information...
commit 522327d184efb9edce6ab02c7305c5180d689f3d 1 parent 64df005
Matthew Kane Parker authored
25 features/step_definitions/steps.rb
View
@@ -69,6 +69,21 @@
)
end
+When /^the text "([^"]*)" should link to "([^"]*)"$/ do |text, link|
+ html = Capybara.string File.read(@user.dependencies_html_path)
+ html.find(:xpath, "//a[@href='#{link}']").text.should == text
+end
+
+When /^I have a truncated dependencies.yml file$/ do
+ File.open(@user.dependencies_file_path, 'w+') do |f|
+ f.puts ""
+ end
+end
+
+When /^"([^"]*)" is an alternative name for the "MIT" license$/ do |alternative_name|
+ # this step is simply for readability
+end
+
Then /^I should see "(.*?)" in its output$/ do |gem_name|
@output.should include gem_name
end
@@ -291,13 +306,3 @@ def root_path
end
end
-
-When /^the text "([^"]*)" should link to "([^"]*)"$/ do |text, link|
- html = Capybara.string File.read(@user.dependencies_html_path)
- html.find(:xpath, "//a[@href='#{link}']").text.should == text
-end
-When /^I have a truncated dependencies.yml file$/ do
- File.open(@user.dependencies_file_path, 'w+') do |f|
- f.puts ""
- end
-end
24 features/whitelist.feature
View
@@ -0,0 +1,24 @@
+Feature: Whitelist licenses
+ As a developer
+ I want to whitelist certain OSS licenses that my business has pre-approved
+ So that any dependencies with those licenses do not show up as action items
+
+ Scenario: Auditing an application with whitelisted licenses
+ Given I have an app with license finder
+ And my app depends on a gem "mit_licensed_gem" licensed with "MIT"
+ When I run "license_finder"
+ Then I should see "mit_licensed_gem" in its output
+ When I whitelist the following licenses: "MIT, other"
+ And I run "license_finder"
+ Then I should see "All gems are approved for use" in its output
+ And it should exit with status code 0
+
+ Scenario: Whitelist with MIT License alternative name "Expat" should whitelist "MIT" licenses
+ Given I have an app with license finder
+ And "Expat" is an alternative name for the "MIT" license
+ And my app depends on a gem "mit_licensed_gem" licensed with "MIT"
+ When I run "license_finder"
+ Then I should see "mit_licensed_gem" in its output
+ When I whitelist the "Expat" license
+ And I run "license_finder"
+ Then I should not see "mit_licensed_gem" in its output
11 lib/license_finder/configuration.rb
View
@@ -1,4 +1,15 @@
module LicenseFinder
class Configuration < LicenseFinder::Persistence::Configuration
+ def whitelisted?(license_name)
+ license = License.find_by_name(license_name) || license_name
+ whitelisted_licenses.include? license
+ end
+
+ private
+ def whitelisted_licenses
+ whitelist.map do |license_name|
+ LicenseFinder::License.find_by_name(license_name) || license_name
+ end.compact
+ end
end
end
2  lib/license_finder/dependency.rb
View
@@ -1,7 +1,7 @@
module LicenseFinder
class Dependency < LicenseFinder::Persistence::Dependency
def approved
- self.approved = !!(config.whitelist.include?(license) || super)
+ self.approved = !!(config.whitelisted?(license) || super)
end
def license_files
6 lib/license_finder/license.rb
View
@@ -3,6 +3,10 @@ class << self
def all
@all ||= []
end
+
+ def find_by_name(license_name)
+ all.detect { |l| l.names.map(&:downcase).include? license_name.to_s.downcase }
+ end
end
class Text
@@ -30,7 +34,7 @@ def inherited(descendant)
end
def names
- [demodulized_name] + self.alternative_names
+ ([demodulized_name, pretty_name] + self.alternative_names).uniq
end
def alternative_names
4 lib/license_finder/license_url.rb
View
@@ -2,9 +2,9 @@ module LicenseFinder::LicenseUrl
extend self
def find_by_name(name)
- return unless name.respond_to?(:downcase)
+ name = name.to_s
- license = LicenseFinder::License.all.detect {|l| l.names.map(&:downcase).include? name.downcase }
+ license = LicenseFinder::License.find_by_name(name)
license.license_url if license
end
end
2  lib/license_finder/persistence/yaml/configuration.rb
View
@@ -1,7 +1,7 @@
module LicenseFinder
module Persistence
class Configuration
- attr_reader :whitelist, :ignore_groups, :dependencies_dir
+ attr_accessor :whitelist, :ignore_groups, :dependencies_dir
def initialize(config={})
if File.exists?(config_file_path)
27 spec/lib/license_finder/configuration_spec.rb
View
@@ -0,0 +1,27 @@
+require "spec_helper"
+
+describe LicenseFinder::Configuration do
+ it_behaves_like "a persistable configuration"
+
+ describe "whitelisted?" do
+ let(:config) { LicenseFinder::Configuration.new }
+
+ context "canonical name whitelisted" do
+ before { config.whitelist = [LicenseFinder::License::Apache2.names[rand(0...LicenseFinder::License::Apache2.names.count)]]}
+
+ let(:possible_license_names) { LicenseFinder::License::Apache2.names }
+
+ it "should return true if if the license is the canonical name, pretty name, or alternative name of the license" do
+ possible_license_names.each do |name|
+ config.whitelisted?(name).should be_true, "expected #{name} to be whitelisted, but wasn't."
+ end
+ end
+
+ it "should be case-insensitive" do
+ possible_license_names.map(&:downcase).each do |name|
+ config.whitelisted?(name).should be_true, "expected #{name} to be whitelisted, but wasn't"
+ end
+ end
+ end
+ end
+end
22 spec/lib/license_finder/dependency_spec.rb
View
@@ -17,17 +17,27 @@ module LicenseFinder
}
end
+ let(:config) { LicenseFinder::Configuration.new }
+
before do
- LicenseFinder.stub(:config).and_return(double('config', {
- :whitelist => %w(MIT),
- :dependencies_yaml => 'dependencies.yml'
- }))
+ LicenseFinder.stub(:config).and_return config
+ config.whitelist = ["MIT", "other"]
end
describe "#approved" do
it "should return true when the license is whitelisted" do
dependency = Dependency.new('license' => 'MIT')
- dependency.approved.should == true
+ dependency.should be_approved
+ end
+
+ it "should return true when the license is an alternative name of a whitelisted license" do
+ dependency = Dependency.new('license' => 'Expat')
+ dependency.should be_approved
+ end
+
+ it "should return true when the license has no matching license class, but is whitelisted anyways" do
+ dependency = Dependency.new('license' => 'other')
+ dependency.should be_approved
end
it "should return false when the license is not whitelisted" do
@@ -35,6 +45,8 @@ module LicenseFinder
dependency.approved.should == false
end
+
+
it "should be overridable" do
dependency = Dependency.new
dependency.approved = true
31 spec/lib/license_finder/license_spec.rb
View
@@ -1,5 +1,36 @@
require 'spec_helper'
+class FooLicense < LicenseFinder::License::Base
+ self.alternative_names = ["the foo license"]
+ self.license_url = "http://foo.license.com"
+
+ def self.pretty_name
+ "Ye Ole Foo License"
+ end
+end
+
+module LicenseFinder
+ describe License do
+ describe ".find_by_name" do
+ it "should match on demodulized names" do
+ License.find_by_name("FooLicense").should == FooLicense
+ end
+
+ it "should match on pretty names" do
+ License.find_by_name("Ye Ole Foo License").should == FooLicense
+ end
+
+ it "should match on alternative names" do
+ License.find_by_name("the foo license").should == FooLicense
+ end
+
+ it "should return nil if no match" do
+ License.find_by_name(:unknown).should be_nil
+ end
+ end
+ end
+end
+
describe LicenseFinder::License::Base do
describe ".names" do
subject do
5 spec/lib/license_finder/persistence/yaml/configuration_spec.rb
View
@@ -1,5 +0,0 @@
-require "spec_helper"
-
-describe LicenseFinder::Persistence::Configuration do
- it_behaves_like "a persistable configuration"
-end
Please sign in to comment.
Something went wrong with that request. Please try again.