From 97ab9a0030760de6a5ecdde8803b5b6beaf95107 Mon Sep 17 00:00:00 2001 From: Lutz Lengemann Date: Thu, 24 Oct 2019 11:16:03 +0200 Subject: [PATCH 1/2] Create output dir if not existant --- lib/license_finder/cli/main.rb | 6 ++++++ spec/lib/license_finder/cli/main_spec.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/license_finder/cli/main.rb b/lib/license_finder/cli/main.rb index a521dc8e2..d71d54eaa 100644 --- a/lib/license_finder/cli/main.rb +++ b/lib/license_finder/cli/main.rb @@ -192,6 +192,12 @@ def aggregate_paths end def save_report(content, file_name) + require 'fileutils' + dir = File.dirname(file_name) + unless Dir.exist?(dir) + FileUtils.mkdir_p(dir) + end + File.open(file_name, 'w') do |f| f.write(content) end diff --git a/spec/lib/license_finder/cli/main_spec.rb b/spec/lib/license_finder/cli/main_spec.rb index 5ecf1ad2c..c20f9b47b 100644 --- a/spec/lib/license_finder/cli/main_spec.rb +++ b/spec/lib/license_finder/cli/main_spec.rb @@ -196,6 +196,22 @@ def report subject.report end end + + context 'when folder does not exist, it is being created' do + let(:save) { 'output/subfolder/license_report.txt' } + + after do + # cleanup to remove the created directory again + FileUtils.rm_rf('output') + end + + it 'calls `FileUtils` which creates the directory' do + subject.options = { 'save' => save } + expect(FileUtils).to receive(:mkdir_p).with('output/subfolder').and_call_original + + subject.report + end + end end context 'when the --save option is not passed' do From d45be01405dde1aef48fae7b32acbe742e9a2f32 Mon Sep 17 00:00:00 2001 From: Shane Lattanzio Date: Thu, 24 Oct 2019 10:27:19 -0400 Subject: [PATCH 2/2] Update main.rb --- lib/license_finder/cli/main.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/license_finder/cli/main.rb b/lib/license_finder/cli/main.rb index d71d54eaa..bbd472652 100644 --- a/lib/license_finder/cli/main.rb +++ b/lib/license_finder/cli/main.rb @@ -192,11 +192,8 @@ def aggregate_paths end def save_report(content, file_name) - require 'fileutils' dir = File.dirname(file_name) - unless Dir.exist?(dir) - FileUtils.mkdir_p(dir) - end + FileUtils.mkdir_p(dir) unless Dir.exist?(dir) File.open(file_name, 'w') do |f| f.write(content)