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

Bundler updates #173

Merged
merged 3 commits into from
Aug 21, 2019
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
13 changes: 7 additions & 6 deletions lib/licensed/sources/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def gem_spec(dependency)
spec = definition.resolve.find { |s| s.satisfies?(dependency) }

# a nil spec should be rare, generally only seen from bundler
return matching_spec(dependency) || bundle_exec_gem_spec(dependency.name) if spec.nil?
return matching_spec(dependency) || bundle_exec_gem_spec(dependency.name, dependency.requirement) if spec.nil?

# try to find a non-lazy specification that matches `spec`
# spec.source.specs gives access to specifications with more
Expand All @@ -166,7 +166,7 @@ def gem_spec(dependency)

# if the specification file doesn't exist, get the specification using
# the bundler and gem CLI
bundle_exec_gem_spec(dependency.name)
bundle_exec_gem_spec(dependency.name, dependency.requirement)
end

# Returns whether a dependency should be included in the final
Expand Down Expand Up @@ -200,7 +200,7 @@ def exclude_development_dependencies?

# Load a gem specification from the YAML returned from `gem specification`
# This is a last resort when licensed can't obtain a specification from other means
def bundle_exec_gem_spec(name)
def bundle_exec_gem_spec(name, requirement)
# `gem` must be available to run `gem specification`
return unless Licensed::Shell.tool_available?("gem")

Expand All @@ -209,11 +209,12 @@ def bundle_exec_gem_spec(name)
begin
::Bundler.with_original_env do
::Bundler.rubygems.clear_paths
yaml = Licensed::Shell.execute(*ruby_command_args("gem", "specification", name))
yaml = Licensed::Shell.execute(*ruby_command_args("gem", "specification", name, "-v", requirement.to_s))
spec = Gem::Specification.from_yaml(yaml)
# this is horrible, but it will cache the gem_dir using the clean env
# so that it can be used outside of this block
spec.gem_dir
# so that it can be used outside of this block when running from
# the ruby packer executable environment
spec.gem_dir if ruby_packer?
spec
end
rescue Licensed::Shell::Error
Expand Down
23 changes: 23 additions & 0 deletions test/sources/bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,28 @@
end
end
end

describe "bundle_exec_gem_spec" do
it "gets a gem specification for a version" do
Dir.chdir(fixtures) do
version = source.dependencies.find { |d| d.name == "bundler" }.version
assert source.bundle_exec_gem_spec("bundler", version)
end
end

it "gets a gem specification for a requirement" do
Dir.chdir(fixtures) do
version = source.dependencies.find { |d| d.name == "bundler" }.version
assert source.bundle_exec_gem_spec("bundler", Gem::Requirement.new(">= #{version}"))
end
end

it "returns nil if a gem specification isn't found" do
Dir.chdir(fixtures) do
version = source.dependencies.find { |d| d.name == "bundler" }.version
refute source.bundle_exec_gem_spec("bundler", version.to_f - 1)
end
end
end
end
end