summaryrefslogtreecommitdiff
path: root/spec/spec_helper.rb
blob: 597553a5bec5c7fee8c76b5d4a138b564fa3495a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
$:.unshift File.expand_path('..', __FILE__)
$:.unshift File.expand_path('../../lib', __FILE__)

require 'fileutils'
require 'rubygems'
require 'bundler'
require 'rspec'

Dir["#{File.expand_path('../support', __FILE__)}/*.rb"].each do |file|
  require file
end

$debug    = false
$show_err = true

Spec::Rubygems.setup
FileUtils.rm_rf(Spec::Path.gem_repo1)
ENV['RUBYOPT'] = "-I#{Spec::Path.root}/spec/support/rubygems_hax"

RSpec.configure do |config|
  config.include Spec::Builders
  config.include Spec::Helpers
  config.include Spec::Indexes
  config.include Spec::Matchers
  config.include Spec::Path
  config.include Spec::Rubygems
  config.include Spec::Platforms
  config.include Spec::Sudo

  original_wd       = Dir.pwd
  original_path     = ENV['PATH']
  original_gem_home = ENV['GEM_HOME']

  def pending_jruby_shebang_fix
    pending "JRuby executables do not have a proper shebang" if RUBY_PLATFORM == "java"
  end

  def check(*args)
    # suppresses ruby warnings about "useless use of == in void context"
    # e.g. check foo.should == bar
  end

  config.before :all do
    @__current_dir__ = Dir.pwd
    build_repo1
  end

  config.before :each do
    reset!
    system_gems []
    chdir bundled_app.to_s
  end

  config.after :each do
    # clean up open pipes
    @in_p.close  if @in_p
    @out_p.close if @out_p
    @err_p.close if @err_p
    Dir.chdir(original_wd)
    # Reset ENV
    env.clear
  end

  Thread.abort_on_exception = true

  class Queue
    def initialize
      @pipes = []
    end

    def push(pipe)
      @pipes << pipe
    end

    def read
      Thread.new do
        loop do
          begin
            sleep 0.1 until @pipes[0]
            pipe = @pipes.shift
            break if pipe == :EOF

            puts "READING: #{pipe.inspect}"
            while line = pipe.gets
              puts line
            end

            pipe.close
          rescue Exception => e
            puts "OMGOMGOMG ERRORZ: #{e.message} - #{e.class}"
            puts e.backtrace
            exit!
          end
        end
      end
    end
  end

  module ForkingRunner
    @queue = Queue.new

    def self.queue
      @queue
    end

    def self.pipe
      Thread.current[:pipe]
    end

    def self.pipe=(pipe)
      Thread.current[:pipe] = pipe
    end

    def run(reporter)
      if ForkingRunner.pipe
        super
      else
        read, write = IO.pipe
        ForkingRunner.queue.push read
        t = Thread.new do
          ForkingRunner.pipe = write

          def reporter.output
            ForkingRunner.pipe || super
          end

          puts "PUSHING A PIPE YO"
          super
          puts "CLOSING A PIPE YO"
          write.close
        end
      end
    end
  end

  # config.extend ForkingRunner
end

# module RSpec::Core::CommandLine::ExampleGroups
#   alias old_run_examples run_examples
# 
#   def run_examples(reporter)
#     t = ForkingRunner.queue.read
#     old_run_examples(reporter)
#     ForkingRunner.queue.push :EOF
# 
#     t.join
#   end
# end