Skip to content

Commit

Permalink
[Added] Specify log directory for prepare
Browse files Browse the repository at this point in the history
[finish #154254782]

Signed-off-by: Ryan Collins <rcollins@pivotal.io>
  • Loading branch information
Daniil Kouznetsov authored and Vikram Yadav committed Jan 15, 2018
1 parent 73c0b92 commit b9a5991
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AmbiguousRegexpLiteral:

# Metrics Cops
AbcSize:
Max: 30
Max: 35
BlockLength:
Enabled: false # TODO: enable and refactor long methods
# ExcludedMethods: ['describe', 'context', 'it', 'shared_examples'] # uncomment once enabled
Expand Down
3 changes: 3 additions & 0 deletions lib/license_finder/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Base < Thor
desc: 'Path to the project. Defaults to current working directory.'
class_option :decisions_file,
desc: 'Where decisions are saved. Defaults to doc/dependency_decisions.yml.'
class_option :log_directory,
desc: 'Where logs are saved. Defaults to ./lf_logs/$PROJECT/$PACKAGE_MANAGER.log'

no_commands do
def decisions
Expand Down Expand Up @@ -41,6 +43,7 @@ def license_finder_config
:save,
:prepare,
:prepare_no_fail,
:log_directory,
:format,
:columns,
:aggregate_paths,
Expand Down
5 changes: 5 additions & 0 deletions lib/license_finder/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def decisions_file_path
project_path.join(path).expand_path
end

def log_directory
path = get(:log_directory) || 'lf_logs'
project_path.join(path).expand_path
end

def project_path
Pathname(path_prefix).expand_path
end
Expand Down
6 changes: 6 additions & 0 deletions lib/license_finder/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def decisions
end

def prepare_projects
clear_logs
package_managers = @scanner.active_package_managers
package_managers.each do |manager|
logger.debug manager.class, 'Running prepare on project'
Expand All @@ -80,10 +81,15 @@ def current_packages
@scanner.active_packages
end

def clear_logs
FileUtils.rm config.log_directory if File.directory? config.log_directory
end

def options
{
logger: logger,
project_path: config.project_path,
log_directory: File.join(config.log_directory, project_name),
ignored_groups: decisions.ignored_groups,
go_full_version: config.go_full_version,
gradle_command: config.gradle_command,
Expand Down
9 changes: 4 additions & 5 deletions lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def initialize(options = {})
@prepare_no_fail = options[:prepare_no_fail]
@logger = options[:logger] || Core.default_logger
@project_path = options[:project_path]
@log_directory = options[:log_directory]
end

def active?
Expand Down Expand Up @@ -111,11 +112,9 @@ def log_errors(stderr)
end

def log_to_file(contents)
log_dir = File.join(@project_path, 'lf_logs')
log_file_path = File.join(log_dir, 'error.log')

FileUtils.mkdir_p(log_dir)
File.open(log_file_path, 'w') do |f|
FileUtils.mkdir_p @log_directory
log_file = File.join(@log_directory, "prepare_#{self.class.package_management_command || '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")
end
Expand Down
12 changes: 6 additions & 6 deletions lib/license_finder/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class Scanner
PACKAGE_MANAGERS = [GoDep, GoWorkspace, Go15VendorExperiment, Glide, Gvt, Govendor, Dep, Bundler, NPM, Pip,
Yarn, Bower, Maven, Gradle, CocoaPods, Rebar, Nuget, Carthage, Mix, Conan].freeze

def initialize(options = { project_path: Pathname.new('') })
@options = options
@project_path = options[:project_path]
@logger = options[:logger]
def initialize(config = { project_path: Pathname.new('') })
@config = config
@project_path = @config[:project_path]
@logger = @config[:logger]
end

def active_packages
Expand All @@ -20,7 +20,7 @@ def active_package_managers

active_pm_classes = []
PACKAGE_MANAGERS.each do |pm_class|
active = pm_class.new(@options).active?
active = pm_class.new(@config).active?
if active
@logger.info pm_class, 'is active', color: :green
active_pm_classes << pm_class
Expand All @@ -32,7 +32,7 @@ def active_package_managers
@logger.info 'License Finder', 'No active and installed package managers found for project.', color: :red if active_pm_classes.empty?

active_pm_classes -= active_pm_classes.map(&:takes_priority_over)
@package_managers = active_pm_classes.map { |pm_class| pm_class.new(@options) }
@package_managers = active_pm_classes.map { |pm_class| pm_class.new(@config) }
end
end
end
4 changes: 3 additions & 1 deletion spec/lib/license_finder/cli/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ module CLI
'--rebar_deps_dir=rebar_dir',
'--mix_command=surprise_me',
'--mix_deps_dir=mix_dir',
'--prepare'
'--prepare',
'--log_directory=some_logs'
]
end

Expand All @@ -83,6 +84,7 @@ module CLI
mix_command: 'surprise_me',
mix_deps_dir: 'mix_dir',
prepare: true,
log_directory: 'some_logs',
logger: { mode: LicenseFinder::Logger::MODE_INFO }
}
end
Expand Down
38 changes: 38 additions & 0 deletions spec/lib/license_finder/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ module LicenseFinder
end
end

describe 'log_directory' do
it 'prefers primary value' do
subject = described_class.new(
{ log_directory: 'primary' },
'log_directory' => 'secondary'
)
expect(subject.log_directory.to_s).to end_with 'primary'
end

it 'accepts saved value' do
subject = described_class.new(
{ log_directory: nil },
'log_directory' => 'secondary'
)
expect(subject.log_directory.to_s).to end_with 'secondary'
end

it 'has default' do
subject = described_class.new(
{ log_directory: nil },
'log_directory' => nil
)
expect(subject.log_directory.to_s).to end_with 'lf_logs'
end

it 'prepends project path to default path if project_path option is set' do
subject = described_class.new({ project_path: 'magic_path' }, {})
expect(subject.log_directory.to_s).to end_with 'magic_path/lf_logs'
end

it 'prepends project path to provided value' do
subject = described_class.new({ log_directory: 'primary',
project_path: 'magic_path' },
'log_directory' => 'secondary')
expect(subject.log_directory.to_s).to end_with 'magic_path/primary'
end
end

describe 'rebar_deps_dir' do
it 'has default' do
subject = described_class.new(
Expand Down
45 changes: 28 additions & 17 deletions spec/lib/license_finder/package_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,59 +73,70 @@ module LicenseFinder

describe '#prepare' do
context 'when there is a prepare_command' do
let(:prepare_error) { /Prepare command .* failed/ }
let(:project_path) { '/path/to/project' }
let(:log_directory) { '/path/to/project/logs/project' }
let(:log_file_path) { File.join(log_directory, 'prepare_errors.log') }
let(:quiet_logger) { double(:logger)
let(:subject) { described_class.new logger: quiet_logger, project_path: project_path, log_directory: log_directory }

before do
FakeFS.activate!
allow(described_class).to receive(:prepare_command).and_return('sh commands')
end

after do
FakeFS.clear!
FakeFS.deactivate!
end

let(:prepare_error) { /Prepare command .* failed/ }
let(:project_path) { '/path/to/project' }

it 'succeeds when prepare command runs successfully' do
expect(SharedHelpers::Cmd).to receive(:run).with('sh commands').and_return(['output', nil, cmd_success])

expect { subject.prepare }.to_not raise_error
end

it 'logs warning and exception when prepare command runs into failure' do
logger = double(:logger)
expect(SharedHelpers::Cmd).to receive(:run).with('sh commands').and_return(['output', 'failure error msg', cmd_failure])
expect(logger).to receive(:info).with('sh commands', 'did not succeed.', color: :red)
expect(logger).to receive(:info).with('sh commands', 'failure error msg', color: :red)
subject = described_class.new logger: logger, project_path: project_path
expect(quiet_logger).to receive(:info).with('sh commands', 'did not succeed.', color: :red)
expect(quiet_logger).to receive(:info).with('sh commands', 'failure error msg', color: :red)
expect { subject.prepare }.to raise_error(prepare_error)
end

describe 'logging prepare errors into log file' do
let(:subject) { described_class.new logger: logger, project_path: project_path }
let(:subject) { described_class.new logger: logger, project_path: project_path, log_directory: log_directory }
let(:error_msg) { "Prepare command \"sh commands\" failed with:\nfailure error msg\n\n" }

let(:log_dir_path) { File.join(project_path, 'lf_logs') }
let(:log_path) { File.join(log_dir_path, 'error.log') }

before do
expect(SharedHelpers::Cmd).to receive(:run).with('sh commands').and_return(['output', 'failure error msg', cmd_failure])
end

it 'logs package manager failure messages in lf_logs/error.log inside project path' do
it 'logs package manager failure messages in log file for package manager' do
expect { subject.prepare }.to raise_error(prepare_error)
expect(File.read(log_path)).to eq error_msg
expect(File.read(log_file_path)).to eq error_msg
end

it 'discards previous logs and starts logging new logs' do
FileUtils.mkdir_p log_dir_path
File.open(log_path, 'w') { |f| f.write('previous logs') }
FileUtils.mkdir_p log_directory
File.open(log_file_path, 'w') { |f| f.write('previous logs') }
expect { subject.prepare }.to raise_error(prepare_error)
expect(File.read(log_file_path)).to eq error_msg
end

it 'uses default package_management_command as the file name' do
expect { subject.prepare }.to raise_error(prepare_error)
expect(File.exists? log_file_path)
end

it 'uses defined package_management_command as the file name' do
allow(LicenseFinder::PackageManager).to receive(:package_management_command).and_return('foobar')
expect { subject.prepare }.to raise_error(prepare_error)
expect(File.read(log_path)).to eq error_msg
expect(File.exists? File.join(log_directory, 'prepare_foobar.log')).to be_truthy
end
end

context 'with prepare_no_fail' do
let(:subject) { described_class.new logger: logger, prepare_no_fail: true, project_path: project_path }
let(:subject) { described_class.new logger: logger, prepare_no_fail: true, project_path: project_path, log_directory: log_directory }

it 'should not throw an error when prepare_command fails' do
expect(SharedHelpers::Cmd).to receive(:run).with('sh commands')
Expand Down

0 comments on commit b9a5991

Please sign in to comment.