Skip to content

Commit

Permalink
[FIXED] Handle log file names created with whitespaces and slashes
Browse files Browse the repository at this point in the history
- Replace whitespace characters with underscore and remove slashes for log file name generation

[finish #161925871]

Signed-off-by: Li Tai <[email protected]>
  • Loading branch information
Vikram Yadav authored and xtreme-lisheng-tai committed Nov 14, 2018
1 parent a51db63 commit 7d6f9da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ def log_errors(stderr)

def log_to_file(contents)
FileUtils.mkdir_p @log_directory
log_file = File.join(@log_directory, "prepare_#{self.class.package_management_command || 'errors'}.log")

# replace whitespace with underscores and remove slashes
log_file_name = self.class.package_management_command&.gsub(/\s/, '_')&.gsub(/\//,'')
log_file = File.join(@log_directory, "prepare_#{log_file_name || 'errors'}.log")

File.open(log_file, 'w') do |f|
f.write("Prepare command \"#{self.class.prepare_command}\" failed with:\n")
f.write("#{contents}\n\n")
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/license_finder/package_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ module LicenseFinder
expect { subject.prepare }.to raise_error(prepare_error)
expect(File.exist?(File.join(log_directory, 'prepare_foobar.log'))).to be_truthy
end

context 'when prepare command has whitespaces and slashes in it' do
before do
allow(described_class).to receive(:package_management_command)
.and_return('mono /usr/local/bin/nuget.exe')
end

let(:log_file_path) { File.join(log_directory, 'prepare_mono_usrlocalbinnuget.exe.log') }

it 'creates log files with whitespaces replaced by underscores and slashes removed' do
expect { subject.prepare }.to raise_error(prepare_error)
expect(File.read(log_file_path)).to eq error_msg
end
end
end

context 'with prepare_no_fail' do
Expand Down

0 comments on commit 7d6f9da

Please sign in to comment.