Skip to content

Commit

Permalink
Merge pull request #515 from MrBerg/add-full-license-text
Browse files Browse the repository at this point in the history
Add full license texts and notice files as possible report columns
  • Loading branch information
cf-osl-bot committed Oct 22, 2018
2 parents 4a2b4d2 + c6ff50c commit ab535bd
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 215 deletions.
5 changes: 5 additions & 0 deletions lib/license_finder/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'license_finder/package_utils/licensing'
require 'license_finder/package_utils/license_files'
require 'license_finder/package_utils/notice_files'

module LicenseFinder
# Super-class that adapts data from different package management
Expand Down Expand Up @@ -149,6 +150,10 @@ def license_files
LicenseFiles.find(install_path, logger: logger)
end

def notice_files
NoticeFiles.find(install_path, logger: logger)
end

def package_manager
'unknown'
end
Expand Down
40 changes: 40 additions & 0 deletions lib/license_finder/package_utils/notice_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'license_finder/package_utils/possible_license_file'

module LicenseFinder
class NoticeFiles
CANDIDATE_FILE_NAMES = %w[NOTICE Notice].freeze
CANDIDATE_PATH_WILDCARD = "*{#{CANDIDATE_FILE_NAMES.join(',')}}*"

def self.find(install_path, options = {})
new(install_path).find(options)
end

def initialize(install_path)
@install_path = install_path ? Pathname(install_path) : nil
end

def find(options = {})
paths_of_candidate_files
.map { |path| PossibleLicenseFile.new(path, options) } # Not really possible license files, but that class has all we need.
end

private

attr_reader :install_path

def paths_of_candidate_files
candidate_files_and_dirs
.flat_map { |path| path.directory? ? path.children : path }
.reject(&:directory?)
.uniq
end

def candidate_files_and_dirs
return [] if install_path.nil?

Pathname.glob(install_path.join('**', CANDIDATE_PATH_WILDCARD))
end
end
end
2 changes: 1 addition & 1 deletion lib/license_finder/packages/merged_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(package, aggregate_paths)
:groups, :whitelisted, :blacklisted, :manual_approval, :install_path, :licenses, :approved_manually?,
:approved_manually!, :approved?, :whitelisted!, :whitelisted?, :blacklisted!, :blacklisted?, :hash,
:activations, :missing, :license_names_from_spec, :decided_licenses, :licensing, :decide_on_license,
:license_files, :package_manager, :missing?, :log_activation
:license_files, :package_manager, :missing?, :log_activation, :notice_files

def aggregate_paths
@aggregate_paths.map { |p| p.expand_path.to_s }
Expand Down
10 changes: 9 additions & 1 deletion lib/license_finder/reports/csv_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module LicenseFinder
class CsvReport < Report
COMMA_SEP = ','.freeze
AVAILABLE_COLUMNS = %w[name version authors licenses license_links approved summary description homepage install_path package_manager groups].freeze
AVAILABLE_COLUMNS = %w[name version authors licenses license_links approved summary description homepage install_path package_manager groups texts notice].freeze
MISSING_DEPENDENCY_TEXT = 'This package is not installed. Please install to determine licenses.'.freeze

def initialize(dependencies, options)
Expand All @@ -28,6 +28,14 @@ def format_dependency(dep)
end
end

def format_texts(dep)
dep.license_files.map { |file| file.text.split(/[\n\r]+/).join("\\@NL") }.join("\\@NL").force_encoding("ISO-8859-1").encode("UTF-8")
end

def format_notice(dep)
dep.notice_files.map { |file| file.text.split(/[\n\r]+/).join("\\@NL") }.join("\\@NL").force_encoding("ISO-8859-1").encode("UTF-8")
end

def format_name(dep)
dep.name
end
Expand Down
Binary file added spec/.DS_Store
Binary file not shown.
Binary file added spec/fixtures/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions spec/fixtures/nested_gem/vendor/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a notice.
1 change: 1 addition & 0 deletions spec/fixtures/non_utf_8_gem/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I�t�rn�ti�n�l
1 change: 1 addition & 0 deletions spec/fixtures/non_utf_8_gem/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I�t�rn�ti�n�l
1 change: 1 addition & 0 deletions spec/fixtures/notice_names/Notice
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a notice.
210 changes: 0 additions & 210 deletions spec/fixtures/utf8_gem/README

This file was deleted.

12 changes: 10 additions & 2 deletions spec/lib/license_finder/package_managers/merged_package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module LicenseFinder
Package.new(
'foo', '1.0.0',
spec_licenses: ['MIT'],
install_path: '/tmp/foo',
install_path: fixture_path('nested_gem'),
authors: 'An author',
description: 'A description',
summary: 'A summary',
Expand Down Expand Up @@ -39,7 +39,15 @@ module LicenseFinder
end

it 'returns the install path' do
expect(subject.install_path).to eq('/tmp/foo')
expect(subject.install_path).to eq(package.install_path)
end

it 'returns the license files' do
expect(subject.license_files.map(&:path)).to eq(package.license_files.map(&:path))
end

it 'returns the notice files' do
expect(subject.notice_files.map(&:path)).to eq(package.notice_files.map(&:path))
end

it 'returns the homepage' do
Expand Down
21 changes: 20 additions & 1 deletion spec/lib/license_finder/packages/license_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

module LicenseFinder
describe LicenseFiles do
describe '#initialize' do
it 'handles a nil install path' do
subject = described_class.new nil
expect(subject.send(:install_path)).to eq(nil)
end

it 'handles a non-existant install path' do
root_path = fixture_path('not/a/dir')
subject = described_class.new root_path
expect(subject.send(:install_path)).to eq(root_path)
end

it 'handles an existing install path' do
root_path = fixture_path('license_names')
subject = described_class.new root_path
expect(subject.send(:install_path)).to eq(root_path)
end
end

describe '#find' do
def files_in(fixture)
root_path = fixture_path(fixture)
Expand Down Expand Up @@ -41,7 +60,7 @@ def files_in(fixture)
end

it 'handles non UTF8 encodings' do
expect { files_in('utf8_gem') }.not_to raise_error
expect { files_in('non_utf8_gem') }.not_to raise_error
end
end
end
Expand Down
Loading

0 comments on commit ab535bd

Please sign in to comment.