summaryrefslogtreecommitdiff
path: root/spec/support/helpers.rb
blob: 0945b0171ae1974a54e8600e754e84c9f118c043 (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
module Spec
  module Helpers
    def reset!
      Dir["#{tmp}/{gems/*,*}"].each do |dir|
        next if %(base remote1 gems).include?(File.basename(dir))
        FileUtils.rm_rf(dir)
      end
      FileUtils.mkdir_p(tmp)
    end

    attr_reader :out

    def in_app_root(&blk)
      FileUtils.mkdir_p(bundled_app)
      Dir.chdir(bundled_app, &blk)
    end

    def run_in_context(cmd)
      env = bundled_path.join('environment.rb')
      raise "Missing environment.rb" unless env.file?
      @out = ruby "-r #{env}", cmd
    end

    def run(cmd)
      setup = "require 'rubygems' ; require 'bubble' ; Bubble.setup\n"
      @out = ruby(setup + cmd)
    end

    def ruby(opts, ruby = nil)
      ruby, opts = opts, nil unless ruby
      ruby.gsub!(/(?=")/, "\\")
      ruby.gsub!('$', '\\$')
      lib = File.join(File.dirname(__FILE__), '..', '..', 'lib')
      %x{#{Gem.ruby} -I#{lib} #{opts} -e "#{ruby}"}.strip
    end

    def gemfile(*args)
      path = bundled_app("Gemfile")
      path = args.shift if Pathname === args.first
      str  = args.shift || ""
      FileUtils.mkdir_p(path.dirname.to_s)
      File.open(path.to_s, 'w') do |f|
        f.puts str
      end
    end

    def bubble(*args)
      path = bundled_app("Gemfile")
      path = args.shift if Pathname === args.first
      str  = args.shift || ""
      FileUtils.mkdir_p(path.dirname)
      Dir.chdir(path.dirname) do
        gemfile(path, str)
        Bubble.load(path)
      end
    end

    def install_gems(*gems)
      Dir["#{gem_repo1}/**/*.gem"].each do |path|
        if gems.include?(File.basename(path, ".gem"))
          gem_command :install, "--no-rdoc --no-ri --ignore-dependencies #{path}"
        end
      end
    end

    alias install_gem install_gems

    def system_gems(*gems)
      FileUtils.mkdir_p(system_gem_path)

      Gem.clear_paths

      gem_home, gem_path = ENV['GEM_HOME'], ENV['GEM_PATH']
      ENV['GEM_HOME'], ENV['GEM_PATH'] = system_gem_path.to_s, system_gem_path.to_s

      install_gems(*gems)
      if block_given?
        yield
        ENV['GEM_HOME'], ENV['GEM_PATH'] = gem_home, gem_path
      end
    end
  end
end