Skip to content

Commit

Permalink
whitelist with any known variant of a license name
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Kane Parker committed Sep 27, 2012
1 parent 64df005 commit 522327d
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 25 deletions.
25 changes: 15 additions & 10 deletions features/step_definitions/steps.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 24 additions & 0 deletions features/whitelist.feature
@@ -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 changes: 11 additions & 0 deletions lib/license_finder/configuration.rb
@@ -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 changes: 1 addition & 1 deletion lib/license_finder/dependency.rb
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/license_finder/license.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/license_finder/license_url.rb
Expand Up @@ -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 changes: 1 addition & 1 deletion lib/license_finder/persistence/yaml/configuration.rb
@@ -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)
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/license_finder/configuration_spec.rb
@@ -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 changes: 17 additions & 5 deletions spec/lib/license_finder/dependency_spec.rb
Expand Up @@ -17,24 +17,36 @@ 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
dependency = Dependency.new('license' => 'GPL')
dependency.approved.should == false
end



it "should be overridable" do
dependency = Dependency.new
dependency.approved = true
Expand Down
31 changes: 31 additions & 0 deletions spec/lib/license_finder/license_spec.rb
@@ -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
Expand Down

This file was deleted.

0 comments on commit 522327d

Please sign in to comment.