Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nested gemfiles like Appraisals #1041

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/license_finder/package_managers/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ def prepare_command
end

def possible_package_paths
[project_path.join(gemfile)]
::Bundler.with_original_env do
[project_path.join(gemfile)]
end
end

private

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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Empty file.
Empty file.
29 changes: 26 additions & 3 deletions spec/lib/license_finder/package_managers/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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