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

Handle Bundler DSL errors #8

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
4 changes: 3 additions & 1 deletion lib/polariscope/scanner/dependency_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def gem_versions
@gem_versions ||= GemVersions.new(dependencies.map(&:name), spec_type: spec_type)
end

def bundle_definition
def bundle_definition # rubocop:disable Metrics/MethodLength
@bundle_definition ||=
::Tempfile.create do |gemfile|
::Tempfile.create do |gemfile_lock|
Expand All @@ -64,6 +64,8 @@ def bundle_definition
Bundler::Definition.build(gemfile.path, gemfile_lock.path, false)
end
end
rescue Bundler::Dsl::DSLError => e
raise Polariscope::Error, "Unable to parse the provided Gemfile/Gemfile.lock: #{e.message}"
end

def current_dependency_version(dependency)
Expand Down
11 changes: 11 additions & 0 deletions spec/files/gemfile.lock_unparseable
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GEM
remote: https://rubygems.org/
specs:

PLATFORMS
x86_64-linux

DEPENDENCIES

BUNDLED WITH
2.2.16
3 changes: 3 additions & 0 deletions spec/files/gemfile_unparseable
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

eval File.read('unavailable_file')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some Gemfiles load other files; this is something Polariscope can't handle in certain modes (e.g. when you use Polariscope.scan with non-local Gemfile).

15 changes: 15 additions & 0 deletions spec/lib/polariscope/scanner/dependency_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
'rspec-rails', 'ruby')
end
end

context 'when gemfile has unparseable content' do
let(:opts) do
{
gemfile_content: File.read('spec/files/gemfile_unparseable'),
gemfile_lock_ontent: File.read('spec/files/gemfile.lock_unparseable')
}
end

it 'raises an error' do
expect do
dependency_context.dependencies
end.to raise_error(Polariscope::Error, %r{^Unable to parse the provided Gemfile/Gemfile.lock:})
end
end
end

describe '#dependency_versions' do
Expand Down