-
-
Notifications
You must be signed in to change notification settings - Fork 761
/
spec_helper.rb
169 lines (144 loc) · 3.88 KB
/
spec_helper.rb
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'rubygems' if RUBY_VERSION.to_f < 1.9
begin
require 'spork'
rescue LoadError
module Spork
def self.prefork
yield
end
def self.each_run
yield
end
end
end
Spork.prefork do
require 'rspec/support/spec'
module ArubaLoader
extend RSpec::Support::WithIsolatedStdErr
with_isolated_stderr do
require 'aruba/api'
end
end
if RUBY_PLATFORM == 'java'
# Works around https://jira.codehaus.org/browse/JRUBY-5678
require 'fileutils'
ENV['TMPDIR'] = File.expand_path('../../tmp', __FILE__)
FileUtils.mkdir_p(ENV['TMPDIR'])
end
$rspec_core_without_stderr_monkey_patch = RSpec::Core::Configuration.new
class RSpec::Core::Configuration
def self.new(*args, &block)
super.tap do |config|
# We detect ruby warnings via $stderr,
# so direct our deprecations to $stdout instead.
config.deprecation_stream = $stdout
end
end
end
Dir['./spec/support/**/*.rb'].map {|f| require f}
class NullObject
private
def method_missing(method, *args, &block)
# ignore
end
end
module Sandboxing
def self.sandboxed(&block)
@orig_config = RSpec.configuration
@orig_world = RSpec.world
@orig_example = RSpec.current_example
new_config = RSpec::Core::Configuration.new
new_config.expose_dsl_globally = false
new_world = RSpec::Core::World.new(new_config)
RSpec.configuration = new_config
RSpec.world = new_world
object = Object.new
object.extend(RSpec::Core::SharedExampleGroup)
(class << RSpec::Core::ExampleGroup; self; end).class_eval do
alias_method :orig_run, :run
def run(reporter=nil)
RSpec.current_example = nil
orig_run(reporter || NullObject.new)
end
end
RSpec::Core::SandboxedMockSpace.sandboxed do
object.instance_eval(&block)
end
ensure
(class << RSpec::Core::ExampleGroup; self; end).class_eval do
remove_method :run
alias_method :run, :orig_run
remove_method :orig_run
end
RSpec.configuration = @orig_config
RSpec.world = @orig_world
RSpec.current_example = @orig_example
end
end
def in_editor?
ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM')
end
module EnvHelpers
def with_env_vars(vars)
original = ENV.to_hash
vars.each { |k, v| ENV[k] = v }
begin
yield
ensure
ENV.replace(original)
end
end
def without_env_vars(*vars)
original = ENV.to_hash
vars.each { |k| ENV.delete(k) }
begin
yield
ensure
ENV.replace(original)
end
end
end
RSpec.configure do |c|
# structural
c.alias_it_behaves_like_to 'it_has_behavior'
c.around {|example| Sandboxing.sandboxed { example.run }}
c.include(RSpecHelpers)
c.include Aruba::Api, :example_group => {
:file_path => /spec\/command_line/
}
# Use the doc formatter when running individual files.
# This is too verbose when running all spec files but
# is nice for a single file.
if c.files_to_run.one? && c.formatters.none?
c.formatter = 'doc'
end
c.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
c.mock_with :rspec do |mocks|
mocks.syntax = :expect
end
# runtime options
c.color = !in_editor?
c.filter_run :focus
c.include EnvHelpers
c.run_all_when_everything_filtered = true
c.filter_run_excluding :ruby => lambda {|version|
case version.to_s
when "!jruby"
RUBY_ENGINE == "jruby"
when /^> (.*)/
!(RUBY_VERSION.to_s > $1)
else
!(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
end
}
c.after(:suite) do
if $stderr.has_output?
raise "Ruby warnings were emitted:\n\n#{$stderr.output}"
end
end
end
end
Spork.each_run do
end