From c1fbc22cc656111f5f03af22b4da087fe58f09b4 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Mon, 13 May 2024 16:18:25 -0300 Subject: [PATCH 1/3] Allow nested gemfiles like Appraisals --- lib/license_finder/package_managers/bundler.rb | 6 +++--- .../custom_gemfile/gemfiles/nested.gemfile | 0 .../custom_gemfile/gemfiles/nested.gemfile.lock | 0 .../package_managers/bundler_spec.rb | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/custom_gemfile/gemfiles/nested.gemfile create mode 100644 spec/fixtures/custom_gemfile/gemfiles/nested.gemfile.lock diff --git a/lib/license_finder/package_managers/bundler.rb b/lib/license_finder/package_managers/bundler.rb index 047d27f14..c1e9c49f5 100644 --- a/lib/license_finder/package_managers/bundler.rb +++ b/lib/license_finder/package_managers/bundler.rb @@ -34,7 +34,7 @@ def prepare_command end def possible_package_paths - [project_path.join(gemfile)] + [gemfile] end private @@ -42,7 +42,7 @@ def possible_package_paths attr_reader :ignored_groups def definition - ENV['BUNDLE_GEMFILE'] = "#{project_path}/#{gemfile}" + ENV['BUNDLE_GEMFILE'] = gemfile @definition ||= ::Bundler::Definition.build(detected_package_path, lockfile_path, nil) end @@ -100,7 +100,7 @@ def log_package_dependencies(package) end def gemfile - File.basename(ENV['BUNDLE_GEMFILE'] || 'Gemfile') + ENV.fetch('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..29315b248 100644 --- a/spec/lib/license_finder/package_managers/bundler_spec.rb +++ b/spec/lib/license_finder/package_managers/bundler_spec.rb @@ -96,6 +96,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 From 6aaf24240337ba03accfead0eae16a70f5594b44 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Mon, 13 May 2024 16:27:48 -0300 Subject: [PATCH 2/3] Find full path from project path --- lib/license_finder/package_managers/bundler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/license_finder/package_managers/bundler.rb b/lib/license_finder/package_managers/bundler.rb index c1e9c49f5..9c9ce011a 100644 --- a/lib/license_finder/package_managers/bundler.rb +++ b/lib/license_finder/package_managers/bundler.rb @@ -100,7 +100,7 @@ def log_package_dependencies(package) end def gemfile - ENV.fetch('BUNDLE_GEMFILE') { "Gemfile" } + ENV.expand_path(ENV['BUNDLE_GEMFILE'] || "Gemfile", project_path) end def lockfile From 307eaf12991c7ddf13a69bc8f059c06536476424 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Mon, 13 May 2024 22:30:57 -0300 Subject: [PATCH 3/3] Allow relative BUNDLE_GEMFILE to be nested without overriding it --- .../package_managers/bundler.rb | 22 +++++++++++++------ .../package_managers/bundler_spec.rb | 15 ++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/license_finder/package_managers/bundler.rb b/lib/license_finder/package_managers/bundler.rb index 9c9ce011a..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 - [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'] = 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 - ENV.expand_path(ENV['BUNDLE_GEMFILE'] || "Gemfile", project_path) + @gemfile ||= ENV['BUNDLE_GEMFILE'] || 'Gemfile' end def lockfile diff --git a/spec/lib/license_finder/package_managers/bundler_spec.rb b/spec/lib/license_finder/package_managers/bundler_spec.rb index 29315b248..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