Skip to content

Commit

Permalink
[Fixed] Separate lines in license text with LF when exported to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuitl committed May 18, 2020
1 parent d36a8ad commit baddb97
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/license_finder/reports/csv_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module LicenseFinder
class CsvReport < Report
COMMA_SEP = ','.freeze
NEWLINE_SEP = '\@NL'.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

Expand Down Expand Up @@ -30,11 +31,13 @@ def format_dependency(dep)
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")
dep.license_files.map { |file| file.text.split(/[\n\r]+/).join(self.class::NEWLINE_SEP) }
.join(self.class::NEWLINE_SEP).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")
dep.notice_files.map { |file| file.text.split(/[\n\r]+/).join(self.class::NEWLINE_SEP) }
.join(self.class::NEWLINE_SEP).force_encoding("ISO-8859-1").encode("UTF-8")
end

def format_name(dep)
Expand Down
2 changes: 2 additions & 0 deletions lib/license_finder/reports/json_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module LicenseFinder
class JsonReport < CsvReport
NEWLINE_SEP = "\n".freeze

def initialize(dependencies, options)
super(dependencies, options)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/license_finder/reports/json_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,21 @@ module LicenseFinder

expect(subject.to_s).to eq(expected)
end

it 'supports multiple license texts and joins lines with line feed' do
install_path = fixture_path('license_directory')
dep = Package.new('gem_a', '1.0', install_path: install_path)
subject = described_class.new([dep], columns: %w[name version texts])
expected = {
dependencies:
[
{
name: 'gem_a', version: '1.0', texts: "The MIT License\nThe MIT License"
}
]
}.to_json

expect(subject.to_s).to eq(expected)
end
end
end

0 comments on commit baddb97

Please sign in to comment.