Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5b7f69566a
Fetching contributors…

Cannot retrieve contributors at this time

253 lines (205 sloc) 6.78 kb
module Spec
module Helpers
def reset!
@in_p, @out_p, @err_p = nil, nil, nil
Dir["#{tmp}/{gems/*,*}"].each do |dir|
next if %(base remote1 gems rubygems_1_3_5 rubygems_1_3_6 rubygems_master).include?(File.basename(dir))
unless env['BUNDLER_SUDO_TESTS']
FileUtils.rm_rf(dir)
else
`sudo rm -rf #{dir}`
end
end
FileUtils.mkdir_p(tmp)
FileUtils.mkdir_p(home)
Gem.sources = ["file://#{gem_repo1}/"]
Gem.configuration.write
end
attr_reader :out, :err, :exitstatus
def in_app_root(&blk)
Dir.chdir(bundled_app, &blk)
end
def in_app_root2(&blk)
Dir.chdir(bundled_app2, &blk)
end
def chdir(dir)
old_dir, @__current_dir__ = @__current_dir__, dir
yield if block_given?
ensure
@__current_dir__ = old_dir if block_given?
end
def pwd
@__current_dir__
end
def env
@__env__ ||= {}
end
def rm(file)
FileUtils.rm("#{@__current_dir__}/#{file}")
end
def run(cmd, *args)
opts = args.last.is_a?(Hash) ? args.pop : {}
expect_err = opts.delete(:expect_err)
groups = args.map {|a| a.inspect }.join(", ")
if opts[:lite_runtime]
setup = "require 'rubygems' ; require 'bundler/setup' ; Bundler.setup(#{groups})\n"
else
setup = "require 'rubygems' ; require 'bundler' ; Bundler.setup(#{groups})\n"
end
@out = ruby(setup + cmd, :expect_err => expect_err)
end
def lib
File.expand_path('../../../lib', __FILE__)
end
def bundle(cmd, options = {})
expect_err = options.delete(:expect_err)
exit_status = options.delete(:exit_status)
options["no-color"] = true unless options.key?("no-color") || cmd.to_s[0..3] == "exec"
env = (options.delete(:env) || {}).map{|k,v| "#{k}='#{v}' "}.join
args = options.map do |k,v|
v == true ? " --#{k}" : " --#{k} #{v}" if v
end.join
gemfile = File.expand_path('../../../bin/bundle', __FILE__)
cmd = "#{env}#{Gem.ruby} -I#{lib} #{gemfile} #{cmd}#{args}"
if exit_status
sys_status(cmd)
else
sys_exec(cmd, expect_err){|i| yield i if block_given? }
end
end
def ruby(ruby, options = {})
expect_err = options.delete(:expect_err)
ruby.gsub!(/["`\$]/) {|m| "\\#{m}" }
sys_exec(%'#{Gem.ruby} -I#{lib} -e "#{ruby}"', expect_err)
end
def gembin(cmd)
lib = File.expand_path("../../../lib", __FILE__)
old, env['RUBYOPT'] = env['RUBYOPT'], "#{env['RUBYOPT']} -I#{lib}"
cmd = bundled_app("bin/#{cmd}") unless cmd.to_s.include?("/")
sys_exec(cmd.to_s)
ensure
env['RUBYOPT'] = old
end
def sys_exec(cmd, expect_err = false)
require "open3"
@in_p, @out_p, @err_p = Open3.popen3(command(cmd))
yield @in_p if block_given?
@err = @err_p.read_available_bytes.strip
@out = @out_p.read_available_bytes.strip
puts @err unless expect_err || @err.empty? || !$show_err
@out
end
def sys_status(cmd)
@err = nil
@out = %x{#{command(cmd)}}.strip
@exitstatus = $?.exitstatus
end
def command(cmd)
env_string = self.env.map { |k,v| "#{k}=#{v}" }.join(" ")
"cd #{@__current_dir__} && #{env_string} #{cmd}"
end
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 gemfile(*args)
path = bundled_app("Gemfile")
path = args.shift if Pathname === args.first
str = args.shift || ""
path.dirname.mkpath
File.open(path.to_s, 'w') do |f|
f.puts str
end
end
def lockfile(*args)
path = bundled_app("Gemfile.lock")
path = args.shift if Pathname === args.first
str = args.shift || ""
# Trim the leading spaces
spaces = str[/\A\s+/, 0] || ""
str.gsub!(/^#{spaces}/, '')
File.open(path.to_s, 'w') do |f|
f.puts str
end
end
def install_gemfile(*args)
gemfile(*args)
opts = args.last.is_a?(Hash) ? args.last : {}
bundle :install, opts
end
def install_gems(*gems)
gems.each do |g|
path = "#{gem_repo1}/gems/#{g}.gem"
raise "OMG `#{path}` does not exist!" unless File.exist?(path)
gem_command :install, "--no-rdoc --no-ri --ignore-dependencies #{path}"
end
end
alias install_gem install_gems
def with_gem_path_as(path)
gem_home, gem_path = env['GEM_HOME'], env['GEM_PATH']
env['GEM_HOME'], env['GEM_PATH'] = path.to_s, path.to_s
yield
ensure
env['GEM_HOME'], env['GEM_PATH'] = gem_home, gem_path
end
def break_git!
FileUtils.mkdir_p(tmp("broken_path"))
File.open(tmp("broken_path/git"), "w", 0755) 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 system_gems(*gems)
gems = gems.flatten
FileUtils.rm_rf(system_gem_path)
FileUtils.mkdir_p(system_gem_path)
Gem.clear_paths
gem_home, gem_path, path = env['GEM_HOME'], env['GEM_PATH'], env['PATH']
env['GEM_HOME'], env['GEM_PATH'] = system_gem_path.to_s, system_gem_path.to_s
install_gems(*gems)
if block_given?
begin
yield
ensure
env['GEM_HOME'], env['GEM_PATH'] = gem_home, gem_path
env['PATH'] = path
end
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 default_bundle_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_bundler_version(version)
old, env['BUNDLER_SPEC_VERSION'] = env['BUNDLER_SPEC_VERSION'], version.to_s
yield if block_given?
ensure
env['BUNDLER_SPEC_VERSION'] = old if block_given?
end
def revision_for(path)
Dir.chdir(path) { `git rev-parse HEAD`.strip }
end
end
end
Jump to Line
Something went wrong with that request. Please try again.