Sha256: ce143155d5f2b72140a644d1fbc2494bdb498d75881d57b634f5f53d20f76dd8

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'bundler/fixture/version'

class BundlerFixture
  attr_reader :dir

  def initialize(dir: File.join(Dir.tmpdir, 'fake_project_root'))
    @dir = dir
    FileUtils.makedirs @dir

    @sources = Bundler::SourceList.new
    @sources.add_rubygems_remote('https://rubygems.org')
  end

  def clean_up
    FileUtils.rmtree @dir
  end

  # @param [Gem::Specification] gem_dependencies This simulates gem requirements in Gemfile.
  # @param [Gem::Specification] source_specs This simulates gems in the source index.
  # @param [boolean] ensure_sources Default is true, makes sure a source exists for each gem_dependency.
  #                                 Set this to false to require sending in sources in @param source_specs.
  def create_lockfile(gem_dependencies:, source_specs: [], ensure_sources: true)
    index = Bundler::Index.new
    Array(source_specs).each { |s| index << s }
    source.instance_variable_set('@specs', index)

    Array(gem_dependencies).each do |dep|
      index << create_spec(dep.name, dep.requirement.requirements.first.last)
    end if ensure_sources

    defn = Bundler::Definition.new(lockfile_filename, Array(gem_dependencies), @sources, {})
    defn.instance_variable_set('@index', index)
    defn.lock(lockfile_filename)
  end

  def lockfile_filename
    File.join(@dir, 'Gemfile.lock')
  end

  def lockfile_contents
    File.read(lockfile_filename)
  end

  def create_dependency(name, *requirements)
    Bundler::Dependency.new(name, requirements, {'source' => source})
  end

  def source
    @sources.all_sources.first
  end

  def create_spec(name, version, dependencies={})
    Gem::Specification.new do |s|
      s.name = name
      s.version = Gem::Version.new(version)
      s.platform = 'ruby'
      s.source = source
      dependencies.each do |name, requirement|
        s.add_dependency name, requirement
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bundler-fixture-1.1.0 lib/bundler/fixture.rb