diff --git a/lib/license_finder/package_managers/bundler.rb b/lib/license_finder/package_managers/bundler.rb index 047d27f14..89eadcedd 100644 --- a/lib/license_finder/package_managers/bundler.rb +++ b/lib/license_finder/package_managers/bundler.rb @@ -34,7 +34,9 @@ def prepare_command end def possible_package_paths - [project_path.join(gemfile)] + ::Bundler.with_original_env do + [project_path.join(gemfile)] + end end private @@ -42,9 +44,11 @@ def possible_package_paths attr_reader :ignored_groups def definition - ENV['BUNDLE_GEMFILE'] = "#{project_path}/#{gemfile}" + return @definition if @definition + + ENV['BUNDLE_GEMFILE'] = File.join(project_path, gemfile) - @definition ||= ::Bundler::Definition.build(detected_package_path, lockfile_path, nil) + @definition = ::Bundler::Definition.build(detected_package_path, lockfile_path, nil) if detected_package_path end def details @@ -63,15 +67,19 @@ def gem_details ::Bundler.reset! ::Bundler.configure end - @gem_details = definition.specs_for(included_groups) + @gem_details = definition&.specs_for(included_groups) || [] end def bundler_details - @bundler_details ||= definition.dependencies + @bundler_details ||= definition&.dependencies || [] end def included_groups - definition.groups - ignored_groups.map(&:to_sym) + definition_groups - ignored_groups.map(&:to_sym) + end + + def definition_groups + definition&.groups || [] end def lockfile_path @@ -100,7 +108,7 @@ def log_package_dependencies(package) end def gemfile - File.basename(ENV['BUNDLE_GEMFILE'] || 'Gemfile') + @gemfile ||= ENV['BUNDLE_GEMFILE'] || 'Gemfile' end def lockfile diff --git a/spec/fixtures/custom_gemfile/gemfiles/nested.gemfile b/spec/fixtures/custom_gemfile/gemfiles/nested.gemfile new file mode 100644 index 000000000..e69de29bb diff --git a/spec/fixtures/custom_gemfile/gemfiles/nested.gemfile.lock b/spec/fixtures/custom_gemfile/gemfiles/nested.gemfile.lock new file mode 100644 index 000000000..e69de29bb diff --git a/spec/lib/license_finder/package_managers/bundler_spec.rb b/spec/lib/license_finder/package_managers/bundler_spec.rb index 431a98a5d..9fc419649 100644 --- a/spec/lib/license_finder/package_managers/bundler_spec.rb +++ b/spec/lib/license_finder/package_managers/bundler_spec.rb @@ -78,9 +78,18 @@ def build_gemspec(name, version, dependency = nil) Bundler.new(project_path: custom_gemfile, ignored_groups: Set.new(%w[dev test])) end - it 'defaults to Gemfile/Gemfile.lock' do - expect(::Bundler::Definition).to receive(:build).with(custom_gemfile.join('Gemfile'), custom_gemfile.join('Gemfile.lock'), nil).and_return(definition) - expect(subject.current_packages).to_not be_empty + context 'when actually not specifying' do + around do |example| + old_var = ENV['BUNDLE_GEMFILE'] + ENV.delete 'BUNDLE_GEMFILE' + example.run + ENV['BUNDLE_GEMFILE'] = old_var + end + + it 'defaults to Gemfile/Gemfile.lock' do + expect(::Bundler::Definition).to receive(:build).with(custom_gemfile.join('Gemfile'), custom_gemfile.join('Gemfile.lock'), nil).and_return(definition) + expect(subject.current_packages).to_not be_empty + end end context 'with the BUNDLE_GEMFILE environment variable set' do @@ -96,6 +105,20 @@ def build_gemspec(name, version, dependency = nil) expect(subject.current_packages).to_not be_empty end end + + context 'with the BUNDLE_GEMFILE environment variable set to a project nested directory' do + around do |example| + old_var = ENV['BUNDLE_GEMFILE'] + ENV['BUNDLE_GEMFILE'] = 'gemfiles/nested.gemfile' + example.run + ENV['BUNDLE_GEMFILE'] = old_var + end + + it 'uses the BUNDLE_GEMFILE variable to identify the gemfile' do + expect(::Bundler::Definition).to receive(:build).with(custom_gemfile.join('gemfiles/nested.gemfile'), custom_gemfile.join('gemfiles/nested.gemfile.lock'), nil).and_return(definition) + expect(subject.current_packages).to_not be_empty + end + end end end end