/
api.rb
256 lines (210 loc) · 5.88 KB
/
api.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
require 'tempfile'
require 'rbconfig'
require 'aruba/process'
module Aruba
module Api
def in_current_dir(&block)
_mkdir(current_dir)
Dir.chdir(current_dir, &block)
end
def current_dir
File.join(*dirs)
end
def cd(dir)
dirs << dir
raise "#{current_dir} is not a directory." unless File.directory?(current_dir)
end
def dirs
@dirs ||= ['tmp/aruba']
end
def create_file(file_name, file_content, check_presence = false)
in_current_dir do
raise "expected #{file_name} to be present" if check_presence && !File.file?(file_name)
_mkdir(File.dirname(file_name))
File.open(file_name, 'w') { |f| f << file_content }
end
end
def append_to_file(file_name, file_content)
in_current_dir do
File.open(file_name, 'a') { |f| f << file_content }
end
end
def create_dir(dir_name)
in_current_dir do
_mkdir(dir_name)
end
end
def check_file_presence(paths, expect_presence)
in_current_dir do
paths.each do |path|
if expect_presence
File.should be_file(path)
else
File.should_not be_file(path)
end
end
end
end
def check_file_content(file, partial_content, expect_match)
regexp = regexp(partial_content)
in_current_dir do
content = IO.read(file)
if expect_match
content.should =~ regexp
else
content.should_not =~ regexp
end
end
end
def check_exact_file_content(file, exact_content)
in_current_dir do
IO.read(file).should == exact_content
end
end
def check_directory_presence(paths, expect_presence)
stop_processes!
in_current_dir do
paths.each do |path|
if expect_presence
File.should be_directory(path)
else
File.should_not be_directory(path)
end
end
end
end
def _mkdir(dir_name)
FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
end
def unescape(string)
eval(%{"#{string}"})
end
def regexp(string_or_regexp)
Regexp === string_or_regexp ? string_or_regexp : Regexp.compile(Regexp.escape(string_or_regexp))
end
def output_from(cmd)
cmd = detect_ruby(cmd)
@processes[cmd].output
end
def all_stdout
@processes.values.inject("") { |out, ps| out << ps.stdout }
end
def all_stderr
@processes.values.inject("") { |out, ps| out << ps.stderr }
end
def all_output
all_stdout << all_stderr
end
def assert_partial_output(partial_output)
all_output.should include(unescape(partial_output))
end
def assert_passing_with(partial_output)
assert_exit_status_and_partial_output(true, partial_output)
end
def assert_failing_with(partial_output)
assert_exit_status_and_partial_output(false, partial_output)
end
def assert_exit_status_and_partial_output(expect_to_pass, partial_output)
assert_partial_output(partial_output)
if expect_to_pass
@last_exit_status.should == 0
else
@last_exit_status.should_not == 0
end
end
def install_gems(gemfile)
create_file("Gemfile", gemfile)
if ENV['GOTGEMS'].nil?
run("gem install bundler")
run("bundle --no-color install")
end
end
def processes
@processes ||= {}
end
def stop_processes!
processes.each do |_, process|
process.stop
end
end
def run(cmd)
cmd = detect_ruby(cmd)
in_current_dir do
announce_or_puts("$ cd #{Dir.pwd}") if @announce_dir
announce_or_puts("$ #{cmd}") if @announce_cmd
process = processes[cmd] = Process.new(cmd)
process.run!
block_given? ? yield(process) : process
end
end
def run_simple(cmd, fail_on_error=true)
@last_exit_status = run(cmd) do |process|
announce_or_puts(process.stdout) if @announce_stdout
announce_or_puts(process.stderr) if @announce_stderr
process.stop
end
if(@last_exit_status != 0 && fail_on_error)
fail("Exit status was #{@last_exit_status}. Output:\n#{all_output}")
end
end
def run_interactive(cmd)
@interactive = run(cmd)
end
def write_interactive(input)
@interactive.stdin.write(input)
end
def announce_or_puts(msg)
if(@puts)
puts(msg)
else
announce(msg)
end
end
def detect_ruby(cmd)
if cmd =~ /^ruby\s/
cmd.gsub(/^ruby\s/, "#{current_ruby} ")
else
cmd
end
end
def current_ruby
File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
end
def use_clean_gemset(gemset)
run(%{rvm gemset create "#{gemset}"}, true)
if @last_stdout =~ /'#{gemset}' gemset created \((.*)\)\./
gem_home = $1
set_env('GEM_HOME', gem_home)
set_env('GEM_PATH', gem_home)
set_env('BUNDLE_PATH', gem_home)
paths = (ENV['PATH'] || "").split(File::PATH_SEPARATOR)
paths.unshift(File.join(gem_home, 'bin'))
set_env('PATH', paths.uniq.join(File::PATH_SEPARATOR))
run("gem install bundler", true)
else
raise "I didn't understand rvm's output: #{@last_stdout}"
end
end
def unset_bundler_env_vars
%w[RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE].each do |key|
set_env(key, nil)
end
end
def set_env(key, value)
announce_or_puts(%{$ export #{key}="#{value}"}) if @announce_env
original_env[key] = ENV.delete(key)
ENV[key] = value
end
def restore_env
original_env.each do |key, value|
ENV[key] = value
end
end
def original_env
@original_env ||= {}
end
def ensure_newline(str)
str.chomp << "\n"
end
end
end