diff --git a/lib/license_finder/cli/main.rb b/lib/license_finder/cli/main.rb index a521dc8e2..bbd472652 100644 --- a/lib/license_finder/cli/main.rb +++ b/lib/license_finder/cli/main.rb @@ -192,6 +192,9 @@ def aggregate_paths end def save_report(content, file_name) + dir = File.dirname(file_name) + FileUtils.mkdir_p(dir) unless Dir.exist?(dir) + 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