Skip to content

Commit

Permalink
Merge pull request #173 from github/bundler-updates
Browse files Browse the repository at this point in the history
Bundler updates
  • Loading branch information
jonabc authored Aug 21, 2019
2 parents eea41bc + f4c1595 commit 48efcd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
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

0 comments on commit 48efcd0

Please sign in to comment.