summaryrefslogtreecommitdiff
path: root/spec/support/rubygems_ext.rb
blob: 5299a7b447e17f389fae40d8519445f740b59ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

require_relative "path"

module Spec
  module Rubygems
    DEV_DEPS = { # rubocop:disable Style/MutableConstant
      "automatiek" => "~> 0.3.0",
      "parallel_tests" => "~> 2.29",
      "ronn" => "~> 0.7.3",
      "rspec" => "~> 3.8",
      "rubocop" => "= 0.77.0",
      "rubocop-performance" => "= 1.5.1",
    }

    DEPS = { # rubocop:disable Style/MutableConstant
      "rack" => "2.0.8",
      "rack-test" => "~> 1.1",
      "artifice" => "~> 0.6.0",
      "compact_index" => "~> 0.11.0",
      "sinatra" => "~> 2.0",
      # Rake version has to be consistent for tests to pass
      "rake" => "13.0.1",
      "builder" => "~> 3.2",
      # ruby-graphviz is used by the viz tests
      "ruby-graphviz" => ">= 0.a",
    }

    extend self

    def dev_setup
      deps = DEV_DEPS

      # JRuby can't build ronn, so we skip that
      deps.delete("ronn") if RUBY_ENGINE == "jruby"

      install_gems(deps)
    end

    def gem_load(gem_name, bin_container)
      require_relative "rubygems_version_manager"
      RubygemsVersionManager.new(ENV["RGV"]).switch

      gem_load_and_activate(gem_name, bin_container)
    end

    def gem_require(gem_name)
      gem_activate(gem_name)
      require gem_name
    end

    def setup
      install_test_deps

      require "fileutils"

      FileUtils.mkdir_p(Path.home)
      FileUtils.mkdir_p(Path.tmpdir)

      ENV["HOME"] = Path.home.to_s
      ENV["TMPDIR"] = Path.tmpdir.to_s

      require "rubygems/user_interaction"
      Gem::DefaultUserInteraction.ui = Gem::SilentUI.new
    end

    def install_parallel_test_deps
      require "parallel"

      prev_env_test_number = ENV["TEST_ENV_NUMBER"]

      begin
        Parallel.processor_count.times do |n|
          ENV["TEST_ENV_NUMBER"] = (n + 1).to_s

          install_test_deps
        end
      ensure
        ENV["TEST_ENV_NUMBER"] = prev_env_test_number
      end
    end

    def install_test_deps
      Gem.clear_paths

      ENV["BUNDLE_PATH"] = nil
      ENV["GEM_HOME"] = ENV["GEM_PATH"] = Path.base_system_gems.to_s
      ENV["PATH"] = [Path.bindir, Path.system_gem_path.join("bin"), ENV["PATH"]].join(File::PATH_SEPARATOR)

      install_gems(DEPS)
    end

  private

    def gem_load_and_activate(gem_name, bin_container)
      gem_activate(gem_name)
      load Gem.bin_path(gem_name, bin_container)
    rescue Gem::LoadError => e
      abort "We couln't activate #{gem_name} (#{e.requirement}). Run `gem install #{gem_name}:'#{e.requirement}'`"
    end

    def gem_activate(gem_name)
      gem_requirement = DEV_DEPS[gem_name]
      gem gem_name, gem_requirement
    end

    def install_gems(gems)
      deps = gems.map {|name, req| "'#{name}:#{req}'" }.join(" ")
      gem = ENV["GEM_COMMAND"] || "#{Gem.ruby} -S gem --backtrace"
      cmd = "#{gem} install #{deps} --no-document --conservative"
      system(cmd) || raise("Installing gems #{deps} for the tests to use failed!")
    end
  end
end