lib/bundler/fixture.rb in bundler-fixture-1.4.0 vs lib/bundler/fixture.rb in bundler-fixture-1.5.0
- old
+ new
@@ -3,19 +3,27 @@
require 'tmpdir'
class BundlerFixture
attr_reader :dir
+ def self.bundler_version_or_higher(version)
+ Gem::Version.new(Bundler::VERSION) >= Gem::Version.new(version)
+ end
+
def initialize(dir: File.join(Dir.tmpdir, 'fake_project_root'), gemfile: 'Gemfile')
@dir = dir
@gemfile = gemfile
FileUtils.makedirs @dir
@sources = Bundler::SourceList.new
@sources.add_rubygems_remote('https://rubygems.org')
end
+ def bundler_version_or_higher(version)
+ self.class.bundler_version_or_higher(version)
+ end
+
def clean_up
FileUtils.rmtree @dir
end
# @param [Gem::Specification] gem_dependencies This simulates gem requirements in Gemfile.
@@ -26,37 +34,82 @@
# Default is empty Array.
def create_lockfile(gem_dependencies:,
source_specs: [],
ensure_sources: true,
update_gems: [],
- gemfile: nil)
+ gemfile: nil,
+ ruby_version: nil)
@gemfile = gemfile if gemfile
- defn = create_definition(gem_dependencies: gem_dependencies,
- source_specs: source_specs,
- ensure_sources: ensure_sources,
- update_gems: update_gems)
- defn.lock(lockfile_filename)
+ dfn = create_definition(gem_dependencies: gem_dependencies,
+ source_specs: source_specs,
+ ensure_sources: ensure_sources,
+ update_gems: update_gems,
+ ruby_version: ruby_version)
+ dfn.lock(lockfile_filename)
end
- def create_definition(gem_dependencies:, source_specs:, ensure_sources:, update_gems:)
+ def create_definition(gem_dependencies:, source_specs:, ensure_sources:, update_gems:, ruby_version: nil)
index = Bundler::Index.new
Array(source_specs).flatten.each { |s| index << s }
- if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new('1.14.0')
+ if bundler_version_or_higher('1.14.0')
index << Gem::Specification.new("ruby\0", Bundler::RubyVersion.system.to_gem_version_with_patchlevel)
index << Gem::Specification.new("rubygems\0", Gem::VERSION)
end
Array(gem_dependencies).each do |dep|
index << create_spec(dep.name, dep.requirement.requirements.first.last)
end if ensure_sources
update_hash = update_gems === true ? true : {gems: Array(update_gems)}
- defn = Bundler::Definition.new(lockfile_filename, Array(gem_dependencies), @sources, update_hash)
- defn.instance_variable_set('@index', index)
+ ruby_version_obj = Bundler::RubyVersion.new(ruby_version, nil, nil, nil)
+ dfn = Bundler::Definition.new(lockfile_filename, Array(gem_dependencies), @sources, update_hash, ruby_version_obj)
+ dfn.instance_variable_set('@index', index)
# reading an existing lockfile in will overwrite the hacked up sources with detected
# ones from lockfile, so this needs to go here after the constructor is called.
source.instance_variable_set('@specs', index)
- defn
+ dfn
+ end
+
+ def create_gemfile(gem_dependencies:, ruby_version: nil)
+ lines = []
+ lines << "source 'https://rubygems.org'"
+ Array(gem_dependencies).each do |spec|
+ name, requirement = case spec
+ when Array
+ spec
+ when Gem::Dependency, Bundler::Dependency
+ [spec.name, spec.requirement]
+ end
+ line = "gem '#{name}'"
+ if requirement
+ req_output = requirement_to_s(requirement)
+ line << ", #{req_output}" unless req_output.empty?
+ end
+ lines << line
+ end
+ lines << "ruby '#{ruby_version}'" if ruby_version
+ File.open(gemfile_filename, 'w') { |f| f.puts lines }
+ end
+
+ def requirement_to_s(req)
+ case req
+ when Gem::Requirement
+ req.as_list.delete_if do |r|
+ r == '>= 0'
+ end.map do |r|
+ "'#{r.gsub(/^= /, '')}'"
+ end.join(', ')
+ when String
+ "'#{req}'"
+ when Array
+ req.map { |r| "'#{r}'" }.join(', ')
+ else
+ req
+ end
+ end
+
+ def gemfile_filename
+ File.join(@dir, "#{@gemfile}")
end
def lockfile_filename
File.join(@dir, "#{@gemfile}.lock")
end