2018-11-02 19:07:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-11-11 03:57:45 -05:00
|
|
|
require_relative "command_execution"
|
|
|
|
require_relative "the_bundle"
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
module Spec
|
|
|
|
module Helpers
|
|
|
|
def reset!
|
|
|
|
Dir.glob("#{tmp}/{gems/*,*}", File::FNM_DOTMATCH).each do |dir|
|
|
|
|
next if %w[base remote1 gems rubygems . ..].include?(File.basename(dir))
|
2020-05-08 01:19:04 -04:00
|
|
|
FileUtils.rm_rf(dir)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
FileUtils.mkdir_p(home)
|
|
|
|
FileUtils.mkdir_p(tmpdir)
|
|
|
|
Bundler.reset!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bang(method)
|
|
|
|
define_method("#{method}!") do |*args, &blk|
|
|
|
|
send(method, *args, &blk).tap do
|
|
|
|
unless last_command.success?
|
2019-12-14 05:49:16 -05:00
|
|
|
raise "Invoking #{method}!(#{args.map(&:inspect).join(", ")}) failed:\n#{last_command.stdboth}"
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def the_bundle(*args)
|
|
|
|
TheBundle.new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_command
|
|
|
|
@command_executions.last || raise("There is no last command")
|
|
|
|
end
|
|
|
|
|
|
|
|
def out
|
2019-04-14 02:01:35 -04:00
|
|
|
last_command.stdout
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def err
|
2019-06-01 05:49:40 -04:00
|
|
|
last_command.stderr
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
2019-04-14 02:01:35 -04:00
|
|
|
MAJOR_DEPRECATION = /^\[DEPRECATED\]\s*/.freeze
|
|
|
|
|
|
|
|
def err_without_deprecations
|
2019-06-01 05:49:40 -04:00
|
|
|
err.gsub(/#{MAJOR_DEPRECATION}.+[\n]?/, "")
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
2019-04-14 02:01:35 -04:00
|
|
|
def deprecations
|
|
|
|
err.split("\n").select {|l| l =~ MAJOR_DEPRECATION }.join("\n").split(MAJOR_DEPRECATION)
|
|
|
|
end
|
|
|
|
|
|
|
|
def exitstatus
|
|
|
|
last_command.exitstatus
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run(cmd, *args)
|
|
|
|
opts = args.last.is_a?(Hash) ? args.pop : {}
|
|
|
|
groups = args.map(&:inspect).join(", ")
|
2020-05-08 01:19:04 -04:00
|
|
|
setup = "require '#{lib_dir}/bundler' ; Bundler.ui.silence { Bundler.setup(#{groups}) }"
|
|
|
|
ruby([setup, cmd].join(" ; "), opts)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
bang :run
|
|
|
|
|
|
|
|
def load_error_run(ruby, name, *args)
|
|
|
|
cmd = <<-RUBY
|
|
|
|
begin
|
|
|
|
#{ruby}
|
|
|
|
rescue LoadError => e
|
2020-05-08 01:19:04 -04:00
|
|
|
warn "ZOMG LOAD ERROR" if e.message.include?("-- #{name}")
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
opts = args.last.is_a?(Hash) ? args.pop : {}
|
|
|
|
args += [opts]
|
|
|
|
run(cmd, *args)
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def bundle(cmd, options = {}, &block)
|
2018-11-02 19:07:56 -04:00
|
|
|
with_sudo = options.delete(:sudo)
|
2020-05-08 01:19:04 -04:00
|
|
|
sudo = with_sudo == :preserve_env ? "sudo -E --preserve-env=RUBYOPT" : "sudo" if with_sudo
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
bundle_bin = options.delete("bundle_bin") || bindir.join("bundle")
|
|
|
|
|
|
|
|
if system_bundler = options.delete(:system_bundler)
|
2019-07-26 07:37:51 -04:00
|
|
|
bundle_bin = system_gem_path.join("bin/bundler")
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
env = options.delete(:env) || {}
|
|
|
|
env["PATH"].gsub!("#{Path.root}/exe", "") if env["PATH"] && system_bundler
|
|
|
|
|
|
|
|
requires = options.delete(:requires) || []
|
|
|
|
|
|
|
|
artifice = options.delete(:artifice) do
|
|
|
|
if RSpec.current_example.metadata[:realworld]
|
|
|
|
"vcr"
|
|
|
|
else
|
|
|
|
"fail"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if artifice
|
2020-05-08 01:19:04 -04:00
|
|
|
requires << "#{Path.spec_dir}/support/artifice/#{artifice}.rb"
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
load_path = []
|
2019-11-11 03:57:45 -05:00
|
|
|
load_path << lib_dir unless system_bundler
|
|
|
|
load_path << spec_dir
|
2020-05-08 01:19:04 -04:00
|
|
|
|
|
|
|
dir = options.delete(:dir) || bundled_app
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
args = options.map do |k, v|
|
|
|
|
case v
|
|
|
|
when nil
|
|
|
|
next
|
|
|
|
when true
|
|
|
|
" --#{k}"
|
|
|
|
when false
|
|
|
|
" --no-#{k}"
|
|
|
|
else
|
|
|
|
" --#{k} #{v}"
|
|
|
|
end
|
|
|
|
end.join
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
ruby_cmd = build_ruby_cmd({ :sudo => sudo, :load_path => load_path, :requires => requires })
|
|
|
|
cmd = "#{ruby_cmd} #{bundle_bin} #{cmd}#{args}"
|
|
|
|
sys_exec(cmd, { :env => env, :dir => dir }, &block)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
bang :bundle
|
|
|
|
|
|
|
|
def forgotten_command_line_options(options)
|
2019-04-14 02:01:35 -04:00
|
|
|
remembered = Bundler::VERSION.split(".", 2).first == "2"
|
2018-11-02 19:07:56 -04:00
|
|
|
options = options.map do |k, v|
|
|
|
|
v = '""' if v && v.to_s.empty?
|
|
|
|
[k, v]
|
|
|
|
end
|
|
|
|
return Hash[options] if remembered
|
|
|
|
options.each do |k, v|
|
|
|
|
if v.nil?
|
2019-04-14 02:01:35 -04:00
|
|
|
bundle! "config unset #{k}"
|
2018-11-02 19:07:56 -04:00
|
|
|
else
|
2019-04-14 02:01:35 -04:00
|
|
|
bundle! "config set --local #{k} #{v}"
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def bundler(cmd, options = {})
|
|
|
|
options["bundle_bin"] = bindir.join("bundler")
|
|
|
|
bundle(cmd, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ruby(ruby, options = {})
|
2020-05-08 01:19:04 -04:00
|
|
|
ruby_cmd = build_ruby_cmd({ :load_path => options[:no_lib] ? [] : [lib_dir] })
|
|
|
|
escaped_ruby = RUBY_PLATFORM == "java" ? ruby.shellescape.dump : ruby.shellescape
|
|
|
|
sys_exec(%(#{ruby_cmd} -w -e #{escaped_ruby}), options)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
bang :ruby
|
|
|
|
|
|
|
|
def load_error_ruby(ruby, name, opts = {})
|
|
|
|
ruby(<<-R)
|
|
|
|
begin
|
|
|
|
#{ruby}
|
|
|
|
rescue LoadError => e
|
2020-05-08 01:19:04 -04:00
|
|
|
warn "ZOMG LOAD ERROR" if e.message.include?("-- #{name}")
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
R
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def build_ruby_cmd(options = {})
|
|
|
|
sudo = options.delete(:sudo)
|
|
|
|
|
|
|
|
libs = options.delete(:load_path) || []
|
|
|
|
lib_option = "-I#{libs.join(File::PATH_SEPARATOR)}"
|
|
|
|
|
|
|
|
requires = options.delete(:requires) || []
|
|
|
|
requires << "#{Path.spec_dir}/support/hax.rb"
|
|
|
|
require_option = requires.map {|r| "-r#{r}" }
|
|
|
|
|
|
|
|
[sudo, Gem.ruby, *lib_option, *require_option].compact.join(" ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def gembin(cmd, options = {})
|
2018-11-02 19:07:56 -04:00
|
|
|
old = ENV["RUBYOPT"]
|
2019-11-11 03:57:45 -05:00
|
|
|
ENV["RUBYOPT"] = "#{ENV["RUBYOPT"]} -I#{lib_dir}"
|
2018-11-02 19:07:56 -04:00
|
|
|
cmd = bundled_app("bin/#{cmd}") unless cmd.to_s.include?("/")
|
2020-05-08 01:19:04 -04:00
|
|
|
sys_exec(cmd.to_s, options)
|
2018-11-02 19:07:56 -04:00
|
|
|
ensure
|
|
|
|
ENV["RUBYOPT"] = old
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def gem_command(command, options = {})
|
|
|
|
sys_exec("#{Path.gem_bin} #{command}", options)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
bang :gem_command
|
|
|
|
|
|
|
|
def rake
|
|
|
|
"#{Gem.ruby} -S #{ENV["GEM_PATH"]}/bin/rake"
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def git(cmd, path)
|
|
|
|
sys_exec("git #{cmd}", :dir => path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sys_exec(cmd, options = {})
|
|
|
|
env = options[:env] || {}
|
|
|
|
env["RUBYOPT"] = opt_add("-r#{spec_dir}/support/switch_rubygems.rb", env["RUBYOPT"] || ENV["RUBYOPT"])
|
|
|
|
dir = options[:dir] || bundled_app
|
|
|
|
command_execution = CommandExecution.new(cmd.to_s, dir)
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-12-14 05:49:16 -05:00
|
|
|
require "open3"
|
2020-05-08 01:19:04 -04:00
|
|
|
require "shellwords"
|
|
|
|
Open3.popen3(env, *cmd.shellsplit, :chdir => dir) do |stdin, stdout, stderr, wait_thr|
|
2018-11-02 19:07:56 -04:00
|
|
|
yield stdin, stdout, wait_thr if block_given?
|
|
|
|
stdin.close
|
|
|
|
|
2020-01-08 02:11:52 -05:00
|
|
|
stdout_read_thread = Thread.new { stdout.read }
|
|
|
|
stderr_read_thread = Thread.new { stderr.read }
|
|
|
|
command_execution.stdout = stdout_read_thread.value.strip
|
|
|
|
command_execution.stderr = stderr_read_thread.value.strip
|
2019-04-14 02:01:35 -04:00
|
|
|
command_execution.exitstatus = wait_thr && wait_thr.value.exitstatus
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
(@command_executions ||= []) << command_execution
|
|
|
|
|
|
|
|
command_execution.stdout
|
|
|
|
end
|
|
|
|
bang :sys_exec
|
|
|
|
|
|
|
|
def config(config = nil, path = bundled_app(".bundle/config"))
|
|
|
|
return YAML.load_file(path) unless config
|
|
|
|
FileUtils.mkdir_p(File.dirname(path))
|
|
|
|
File.open(path, "w") do |f|
|
|
|
|
f.puts config.to_yaml
|
|
|
|
end
|
|
|
|
config
|
|
|
|
end
|
|
|
|
|
|
|
|
def global_config(config = nil)
|
|
|
|
config(config, home(".bundle/config"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_file(*args)
|
|
|
|
path = bundled_app(args.shift)
|
|
|
|
path = args.shift if args.first.is_a?(Pathname)
|
|
|
|
str = args.shift || ""
|
|
|
|
path.dirname.mkpath
|
|
|
|
File.open(path.to_s, "w") do |f|
|
|
|
|
f.puts strip_whitespace(str)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gemfile(*args)
|
2019-04-14 02:01:35 -04:00
|
|
|
contents = args.shift
|
|
|
|
|
|
|
|
if contents.nil?
|
2020-05-08 01:19:04 -04:00
|
|
|
File.open(bundled_app_gemfile, "r", &:read)
|
2018-11-02 19:07:56 -04:00
|
|
|
else
|
2019-04-14 02:01:35 -04:00
|
|
|
create_file("Gemfile", contents, *args)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lockfile(*args)
|
2019-04-14 02:01:35 -04:00
|
|
|
contents = args.shift
|
|
|
|
|
|
|
|
if contents.nil?
|
2020-05-08 01:19:04 -04:00
|
|
|
File.open(bundled_app_lock, "r", &:read)
|
2018-11-02 19:07:56 -04:00
|
|
|
else
|
2019-05-06 12:06:21 -04:00
|
|
|
create_file("Gemfile.lock", contents, *args)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_whitespace(str)
|
|
|
|
# Trim the leading spaces
|
|
|
|
spaces = str[/\A\s+/, 0] || ""
|
|
|
|
str.gsub(/^#{spaces}/, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
def install_gemfile(*args)
|
|
|
|
gemfile(*args)
|
|
|
|
opts = args.last.is_a?(Hash) ? args.last : {}
|
|
|
|
opts[:retry] ||= 0
|
|
|
|
bundle :install, opts
|
|
|
|
end
|
|
|
|
bang :install_gemfile
|
|
|
|
|
|
|
|
def lock_gemfile(*args)
|
|
|
|
gemfile(*args)
|
|
|
|
opts = args.last.is_a?(Hash) ? args.last : {}
|
|
|
|
opts[:retry] ||= 0
|
|
|
|
bundle :lock, opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def install_gems(*gems)
|
|
|
|
options = gems.last.is_a?(Hash) ? gems.pop : {}
|
|
|
|
gem_repo = options.fetch(:gem_repo) { gem_repo1 }
|
|
|
|
gems.each do |g|
|
2020-05-08 01:19:04 -04:00
|
|
|
gem_name = g.to_s
|
|
|
|
if gem_name.start_with?("bundler")
|
|
|
|
version = gem_name.match(/\Abundler-(?<version>.*)\z/)[:version] if gem_name != "bundler"
|
|
|
|
with_built_bundler(version) {|gem_path| install_gem(gem_path) }
|
|
|
|
elsif gem_name =~ %r{\A(?:[a-zA-Z]:)?/.*\.gem\z}
|
|
|
|
install_gem(gem_name)
|
2018-11-02 19:07:56 -04:00
|
|
|
else
|
2020-05-08 01:19:04 -04:00
|
|
|
install_gem("#{gem_repo}/gems/#{gem_name}.gem")
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-15 11:07:48 -04:00
|
|
|
def install_gem(path)
|
|
|
|
raise "OMG `#{path}` does not exist!" unless File.exist?(path)
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
gem_command! "install --no-document --ignore-dependencies '#{path}'"
|
2019-08-15 11:07:48 -04:00
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def with_built_bundler(version = nil)
|
|
|
|
version ||= Bundler::VERSION
|
|
|
|
full_name = "bundler-#{version}"
|
|
|
|
build_path = tmp + full_name
|
|
|
|
bundler_path = build_path + "#{full_name}.gem"
|
2019-08-15 12:58:07 -04:00
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
Dir.mkdir build_path
|
2019-08-15 12:58:07 -04:00
|
|
|
|
|
|
|
begin
|
2020-05-08 01:19:04 -04:00
|
|
|
shipped_files.each do |shipped_file|
|
|
|
|
target_shipped_file = build_path + shipped_file
|
|
|
|
target_shipped_dir = File.dirname(target_shipped_file)
|
|
|
|
FileUtils.mkdir_p target_shipped_dir unless File.directory?(target_shipped_dir)
|
|
|
|
FileUtils.cp shipped_file, target_shipped_file, :preserve => true
|
|
|
|
end
|
|
|
|
|
|
|
|
replace_version_file(version, dir: build_path) # rubocop:disable Style/HashSyntax
|
|
|
|
|
2020-05-11 04:26:43 -04:00
|
|
|
gem_command! "build #{shipped_gemspec}", :dir => build_path
|
2020-05-08 01:19:04 -04:00
|
|
|
|
2019-08-15 12:58:07 -04:00
|
|
|
yield(bundler_path)
|
|
|
|
ensure
|
2020-05-08 01:19:04 -04:00
|
|
|
build_path.rmtree
|
2019-08-15 12:58:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
def with_gem_path_as(path)
|
|
|
|
backup = ENV.to_hash
|
|
|
|
ENV["GEM_HOME"] = path.to_s
|
|
|
|
ENV["GEM_PATH"] = path.to_s
|
|
|
|
ENV["BUNDLER_ORIG_GEM_PATH"] = nil
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV.replace(backup)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_path_as(path)
|
|
|
|
backup = ENV.to_hash
|
|
|
|
ENV["PATH"] = path.to_s
|
|
|
|
ENV["BUNDLER_ORIG_PATH"] = nil
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV.replace(backup)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_path_added(path)
|
|
|
|
with_path_as(path.to_s + ":" + ENV["PATH"]) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def opt_add(option, options)
|
|
|
|
[option.strip, options].compact.reject(&:empty?).join(" ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def opt_remove(option, options)
|
|
|
|
return unless options
|
|
|
|
|
|
|
|
options.split(" ").reject {|opt| opt.strip == option.strip }.join(" ")
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
def break_git!
|
|
|
|
FileUtils.mkdir_p(tmp("broken_path"))
|
|
|
|
File.open(tmp("broken_path/git"), "w", 0o755) do |f|
|
|
|
|
f.puts "#!/usr/bin/env ruby\nSTDERR.puts 'This is not the git you are looking for'\nexit 1"
|
|
|
|
end
|
|
|
|
|
|
|
|
ENV["PATH"] = "#{tmp("broken_path")}:#{ENV["PATH"]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_fake_man
|
2020-05-08 01:19:04 -04:00
|
|
|
skip "fake_man is not a Windows friendly binstub" if Gem.win_platform?
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
FileUtils.mkdir_p(tmp("fake_man"))
|
|
|
|
File.open(tmp("fake_man/man"), "w", 0o755) do |f|
|
|
|
|
f.puts "#!/usr/bin/env ruby\nputs ARGV.inspect\n"
|
|
|
|
end
|
|
|
|
with_path_added(tmp("fake_man")) { yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_gems(*gems)
|
|
|
|
opts = gems.last.is_a?(Hash) ? gems.last : {}
|
|
|
|
path = opts.fetch(:path, system_gem_path)
|
|
|
|
gems = gems.flatten
|
|
|
|
|
|
|
|
unless opts[:keep_path]
|
|
|
|
FileUtils.rm_rf(path)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
Gem.clear_paths
|
|
|
|
|
|
|
|
env_backup = ENV.to_hash
|
|
|
|
ENV["GEM_HOME"] = path.to_s
|
|
|
|
ENV["GEM_PATH"] = path.to_s
|
|
|
|
ENV["BUNDLER_ORIG_GEM_PATH"] = nil
|
|
|
|
|
|
|
|
install_gems(*gems)
|
|
|
|
return unless block_given?
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV.replace(env_backup)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def realworld_system_gems(*gems)
|
|
|
|
gems = gems.flatten
|
|
|
|
|
|
|
|
FileUtils.rm_rf(system_gem_path)
|
|
|
|
FileUtils.mkdir_p(system_gem_path)
|
|
|
|
|
|
|
|
Gem.clear_paths
|
|
|
|
|
|
|
|
gem_home = ENV["GEM_HOME"]
|
|
|
|
gem_path = ENV["GEM_PATH"]
|
|
|
|
path = ENV["PATH"]
|
|
|
|
ENV["GEM_HOME"] = system_gem_path.to_s
|
|
|
|
ENV["GEM_PATH"] = system_gem_path.to_s
|
|
|
|
|
|
|
|
gems.each do |gem|
|
2020-05-08 01:19:04 -04:00
|
|
|
gem_command! "install --no-document #{gem}"
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
return unless block_given?
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV["GEM_HOME"] = gem_home
|
|
|
|
ENV["GEM_PATH"] = gem_path
|
|
|
|
ENV["PATH"] = path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_gems(*gems)
|
|
|
|
gems = gems.flatten
|
|
|
|
|
|
|
|
FileUtils.rm_rf("#{bundled_app}/vendor/cache")
|
|
|
|
FileUtils.mkdir_p("#{bundled_app}/vendor/cache")
|
|
|
|
|
|
|
|
gems.each do |g|
|
|
|
|
path = "#{gem_repo1}/gems/#{g}.gem"
|
|
|
|
raise "OMG `#{path}` does not exist!" unless File.exist?(path)
|
|
|
|
FileUtils.cp(path, "#{bundled_app}/vendor/cache")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def simulate_new_machine
|
|
|
|
system_gems []
|
|
|
|
FileUtils.rm_rf system_gem_path
|
|
|
|
FileUtils.rm_rf bundled_app(".bundle")
|
|
|
|
end
|
|
|
|
|
|
|
|
def simulate_platform(platform)
|
|
|
|
old = ENV["BUNDLER_SPEC_PLATFORM"]
|
|
|
|
ENV["BUNDLER_SPEC_PLATFORM"] = platform.to_s
|
|
|
|
yield if block_given?
|
|
|
|
ensure
|
|
|
|
ENV["BUNDLER_SPEC_PLATFORM"] = old if block_given?
|
|
|
|
end
|
|
|
|
|
|
|
|
def simulate_ruby_version(version)
|
|
|
|
return if version == RUBY_VERSION
|
|
|
|
old = ENV["BUNDLER_SPEC_RUBY_VERSION"]
|
|
|
|
ENV["BUNDLER_SPEC_RUBY_VERSION"] = version
|
|
|
|
yield if block_given?
|
|
|
|
ensure
|
|
|
|
ENV["BUNDLER_SPEC_RUBY_VERSION"] = old if block_given?
|
|
|
|
end
|
|
|
|
|
|
|
|
def simulate_windows(platform = mswin)
|
|
|
|
old = ENV["BUNDLER_SPEC_WINDOWS"]
|
|
|
|
ENV["BUNDLER_SPEC_WINDOWS"] = "true"
|
|
|
|
simulate_platform platform do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ENV["BUNDLER_SPEC_WINDOWS"] = old
|
|
|
|
end
|
|
|
|
|
|
|
|
def revision_for(path)
|
2020-05-08 01:19:04 -04:00
|
|
|
sys_exec("git rev-parse HEAD", :dir => path).strip
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_read_only(pattern)
|
|
|
|
chmod = lambda do |dirmode, filemode|
|
|
|
|
lambda do |f|
|
|
|
|
mode = File.directory?(f) ? dirmode : filemode
|
|
|
|
File.chmod(mode, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir[pattern].each(&chmod[0o555, 0o444])
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Dir[pattern].each(&chmod[0o755, 0o644])
|
|
|
|
end
|
|
|
|
|
2019-05-31 05:03:11 -04:00
|
|
|
# Simulate replacing TODOs with real values
|
|
|
|
def prepare_gemspec(pathname)
|
|
|
|
process_file(pathname) do |line|
|
|
|
|
case line
|
|
|
|
when /spec\.metadata\["(?:allowed_push_host|homepage_uri|source_code_uri|changelog_uri)"\]/, /spec\.homepage/
|
|
|
|
line.gsub(/\=.*$/, "= 'http://example.org'")
|
|
|
|
when /spec\.summary/
|
|
|
|
line.gsub(/\=.*$/, "= %q{A short summary of my new gem.}")
|
|
|
|
when /spec\.description/
|
|
|
|
line.gsub(/\=.*$/, "= %q{A longer description of my new gem.}")
|
|
|
|
else
|
|
|
|
line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
def process_file(pathname)
|
|
|
|
changed_lines = pathname.readlines.map do |line|
|
|
|
|
yield line
|
|
|
|
end
|
|
|
|
File.open(pathname, "w") {|file| file.puts(changed_lines.join) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_env_vars(env_hash, &block)
|
|
|
|
current_values = {}
|
|
|
|
env_hash.each do |k, v|
|
|
|
|
current_values[k] = ENV[k]
|
|
|
|
ENV[k] = v
|
|
|
|
end
|
|
|
|
block.call if block_given?
|
|
|
|
env_hash.each do |k, _|
|
|
|
|
ENV[k] = current_values[k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_rack
|
|
|
|
# need to hack, so we can require rack
|
|
|
|
old_gem_home = ENV["GEM_HOME"]
|
|
|
|
ENV["GEM_HOME"] = Spec::Path.base_system_gems.to_s
|
|
|
|
require "rack"
|
|
|
|
ENV["GEM_HOME"] = old_gem_home
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_server(host, port, seconds = 15)
|
|
|
|
tries = 0
|
|
|
|
sleep 0.5
|
|
|
|
TCPSocket.new(host, port)
|
2019-04-14 02:01:35 -04:00
|
|
|
rescue StandardError => e
|
2018-11-02 19:07:56 -04:00
|
|
|
raise(e) if tries > (seconds * 2)
|
|
|
|
tries += 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_unused_port
|
|
|
|
port = 21_453
|
|
|
|
begin
|
|
|
|
port += 1 while TCPSocket.new("127.0.0.1", port)
|
2019-04-14 02:01:35 -04:00
|
|
|
rescue StandardError
|
2018-11-02 19:07:56 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
port
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|