Module: Cuken::Api::Aruba::Api
Constant Summary collapse
- DEFAULT_TIMEOUT_SECONDS =
3
- DEFAULT_IO_WAIT_SECONDS =
0.1
Instance Method Summary collapse
- #_create_file(file_name, file_content, check_presence) ⇒ Object
- #_ensure_newline(str) ⇒ Object
- #_mkdir(dir_name) ⇒ Object
- #_write_interactive(input) ⇒ Object
- #all_output ⇒ Object
- #all_stderr ⇒ Object
- #all_stdout ⇒ Object
- #announce_or_puts(msg) ⇒ Object
- #append_to_file(file_name, file_content) ⇒ Object
- #assert_exact_output(exact_output) ⇒ Object
- #assert_exit_status_and_output(expect_to_pass, output, expect_exact_output) ⇒ Object
- #assert_exit_status_and_partial_output(expect_to_pass, partial_output) ⇒ Object
- #assert_exiting_with(expect_to_pass) ⇒ Object
- #assert_failing_with(partial_output) ⇒ Object
- #assert_partial_output(partial_output) ⇒ Object
- #assert_passing_with(partial_output) ⇒ Object
- #cd(dir) ⇒ Object
- #check_directory_presence(paths, expect_presence) ⇒ Object
- #check_exact_file_content(file, exact_content) ⇒ Object
-
#check_file_content(file, partial_content, expect_match, times = 1) ⇒ Object
def check_file_content(file, partial_content, expect_match) regexp = regexp(partial_content) prep_for_fs_check do content = IO.read(file) if expect_match content.should =~ regexp else content.should_not =~ regexp end end end.
- #check_file_presence(paths, expect_presence) ⇒ Object
- #create_dir(dir_name) ⇒ Object
- #current_dir ⇒ Object
- #current_ruby ⇒ Object
- #detect_ruby(cmd) ⇒ Object
- #dirs ⇒ Object
- #exit_timeout ⇒ Object
- #get_process(wanted) ⇒ Object
- #in_dir(dir = current_dir, &block) ⇒ Object
- #io_wait ⇒ Object
- #only_processes ⇒ Object
- #original_env ⇒ Object
- #output_from(cmd) ⇒ Object
- #overwrite_file(file_name, file_content) ⇒ Object
- #prep_for_fs_check(&block) ⇒ Object
- #processes ⇒ Object
- #regexp(string_or_regexp) ⇒ Object
- #register_process(name, process) ⇒ Object
- #remove_file(file_name) ⇒ Object
- #restore_env ⇒ Object
- #run(cmd, dir = current_dir) ⇒ Object
- #run_interactive(cmd, dir = current_dir) ⇒ Object
- #run_simple(cmd, dir = current_dir, fail_on_error = true) ⇒ Object
- #set_env(key, value) ⇒ Object
- #stderr_from(cmd) ⇒ Object
- #stdout_from(cmd) ⇒ Object
- #stop_processes! ⇒ Object
- #type(input) ⇒ Object
- #unescape(string) ⇒ Object
- #unset_bundler_env_vars ⇒ Object
- #use_clean_gemset(gemset) ⇒ Object
- #write_file(file_name, file_content) ⇒ Object
Instance Method Details
#_create_file(file_name, file_content, check_presence) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/cuken/api/aruba/api.rb', line 35 def _create_file(file_name, file_content, check_presence) in_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 |
#_ensure_newline(str) ⇒ Object
279 280 281 |
# File 'lib/cuken/api/aruba/api.rb', line 279 def _ensure_newline(str) str.chomp << "\n" end |
#_mkdir(dir_name) ⇒ Object
126 127 128 |
# File 'lib/cuken/api/aruba/api.rb', line 126 def _mkdir(dir_name) ::FileUtils.mkdir_p(dir_name) unless ::File.directory?(dir_name) end |
#_write_interactive(input) ⇒ Object
275 276 277 |
# File 'lib/cuken/api/aruba/api.rb', line 275 def _write_interactive(input) @interactive.stdin.write(input) end |
#all_output ⇒ Object
161 162 163 |
# File 'lib/cuken/api/aruba/api.rb', line 161 def all_output all_stdout << all_stderr end |
#all_stderr ⇒ Object
157 158 159 |
# File 'lib/cuken/api/aruba/api.rb', line 157 def all_stderr only_processes.inject("") { |out, ps| out << ps.stderr } end |
#all_stdout ⇒ Object
153 154 155 |
# File 'lib/cuken/api/aruba/api.rb', line 153 def all_stdout only_processes.inject("") { |out, ps| out << ps.stdout } end |
#announce_or_puts(msg) ⇒ Object
283 284 285 |
# File 'lib/cuken/api/aruba/api.rb', line 283 def announce_or_puts(msg) puts(msg) end |
#append_to_file(file_name, file_content) ⇒ Object
49 50 51 52 53 |
# File 'lib/cuken/api/aruba/api.rb', line 49 def append_to_file(file_name, file_content) in_dir do ::File.open(file_name, 'a') { |f| f << file_content } end end |
#assert_exact_output(exact_output) ⇒ Object
165 166 167 |
# File 'lib/cuken/api/aruba/api.rb', line 165 def assert_exact_output(exact_output) all_output.should == exact_output end |
#assert_exit_status_and_output(expect_to_pass, output, expect_exact_output) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/cuken/api/aruba/api.rb', line 186 def assert_exit_status_and_output(expect_to_pass, output, expect_exact_output) if expect_exact_output assert_exact_output(output) else assert_partial_output(output) end assert_exiting_with(expect_to_pass) end |
#assert_exit_status_and_partial_output(expect_to_pass, partial_output) ⇒ Object
181 182 183 184 |
# File 'lib/cuken/api/aruba/api.rb', line 181 def assert_exit_status_and_partial_output(expect_to_pass, partial_output) assert_partial_output(partial_output) assert_exiting_with(expect_to_pass) end |
#assert_exiting_with(expect_to_pass) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/cuken/api/aruba/api.rb', line 195 def assert_exiting_with(expect_to_pass) if expect_to_pass @last_exit_status.should == 0 else @last_exit_status.should_not == 0 end end |
#assert_failing_with(partial_output) ⇒ Object
177 178 179 |
# File 'lib/cuken/api/aruba/api.rb', line 177 def assert_failing_with(partial_output) assert_exit_status_and_partial_output(false, partial_output) end |
#assert_partial_output(partial_output) ⇒ Object
169 170 171 |
# File 'lib/cuken/api/aruba/api.rb', line 169 def assert_partial_output(partial_output) all_output.should include(unescape(partial_output)) end |
#assert_passing_with(partial_output) ⇒ Object
173 174 175 |
# File 'lib/cuken/api/aruba/api.rb', line 173 def assert_passing_with(partial_output) assert_exit_status_and_partial_output(true, partial_output) end |
#cd(dir) ⇒ Object
18 19 20 21 |
# File 'lib/cuken/api/aruba/api.rb', line 18 def cd(dir) dirs << dir raise "#{current_dir} is not a directory." unless ::File.directory?(current_dir) end |
#check_directory_presence(paths, expect_presence) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cuken/api/aruba/api.rb', line 109 def check_directory_presence(paths, expect_presence) prep_for_fs_check do paths.each do |path| if expect_presence ::File.should be_directory(path) else ::File.should_not be_directory(path) end end end end |
#check_exact_file_content(file, exact_content) ⇒ Object
105 106 107 |
# File 'lib/cuken/api/aruba/api.rb', line 105 def check_exact_file_content(file, exact_content) prep_for_fs_check { IO.read(file).should == exact_content } end |
#check_file_content(file, partial_content, expect_match, times = 1) ⇒ Object
def check_file_content(file, partial_content, expect_match)
regexp = regexp(partial_content)
prep_for_fs_check do
content = IO.read(file)
if expect_match
content.should =~ regexp
else
content.should_not =~ regexp
end
end
end
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cuken/api/aruba/api.rb', line 85 def check_file_content(file, partial_content, expect_match, times = 1) regexp = regexp(partial_content) seen_count = 0 prep_for_fs_check do content = IO.read(file) while (seen_count < times.to_i || content =~ regexp)do if content =~ regexp content = content.sub(regexp,'') seen_count+=1 end end if expect_match seen_count.should == times.to_i else seen_count.should_not == times.to_i end end end |
#check_file_presence(paths, expect_presence) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cuken/api/aruba/api.rb', line 61 def check_file_presence(paths, expect_presence) prep_for_fs_check do paths.each do |path| if expect_presence ::File.should be_file(path) else ::File.should_not be_file(path) end end end end |
#create_dir(dir_name) ⇒ Object
55 56 57 58 59 |
# File 'lib/cuken/api/aruba/api.rb', line 55 def create_dir(dir_name) in_dir do _mkdir(dir_name) end end |
#current_dir ⇒ Object
14 15 16 |
# File 'lib/cuken/api/aruba/api.rb', line 14 def current_dir ::File.join(*dirs) end |
#current_ruby ⇒ Object
295 296 297 |
# File 'lib/cuken/api/aruba/api.rb', line 295 def current_ruby ::File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']) end |
#detect_ruby(cmd) ⇒ Object
287 288 289 290 291 292 293 |
# File 'lib/cuken/api/aruba/api.rb', line 287 def detect_ruby(cmd) if cmd =~ /^ruby\s/ cmd.gsub(/^ruby\s/, "#{current_ruby} ") else cmd end end |
#dirs ⇒ Object
23 24 25 |
# File 'lib/cuken/api/aruba/api.rb', line 23 def dirs @dirs ||= ['tmp/aruba'] end |
#exit_timeout ⇒ Object
242 243 244 |
# File 'lib/cuken/api/aruba/api.rb', line 242 def exit_timeout @aruba_timeout_seconds || DEFAULT_TIMEOUT_SECONDS end |
#get_process(wanted) ⇒ Object
217 218 219 |
# File 'lib/cuken/api/aruba/api.rb', line 217 def get_process(wanted) processes.find{ |name, _| name == wanted }[-1] end |
#in_dir(dir = current_dir, &block) ⇒ Object
8 9 10 11 12 |
# File 'lib/cuken/api/aruba/api.rb', line 8 def in_dir(dir=current_dir, &block) exp_dir = ::File.(dir) _mkdir(exp_dir) Dir.chdir(exp_dir, &block) end |
#io_wait ⇒ Object
248 249 250 |
# File 'lib/cuken/api/aruba/api.rb', line 248 def io_wait @aruba_io_wait_seconds || DEFAULT_IO_WAIT_SECONDS end |
#only_processes ⇒ Object
221 222 223 |
# File 'lib/cuken/api/aruba/api.rb', line 221 def only_processes processes.collect{ |_, process| process } end |
#original_env ⇒ Object
335 336 337 |
# File 'lib/cuken/api/aruba/api.rb', line 335 def original_env @original_env ||= {} end |
#output_from(cmd) ⇒ Object
138 139 140 141 |
# File 'lib/cuken/api/aruba/api.rb', line 138 def output_from(cmd) cmd = detect_ruby(cmd) get_process(cmd).output end |
#overwrite_file(file_name, file_content) ⇒ Object
31 32 33 |
# File 'lib/cuken/api/aruba/api.rb', line 31 def overwrite_file(file_name, file_content) _create_file(file_name, file_content, true) end |
#prep_for_fs_check(&block) ⇒ Object
121 122 123 124 |
# File 'lib/cuken/api/aruba/api.rb', line 121 def prep_for_fs_check(&block) stop_processes! in_dir{ block.call } end |
#processes ⇒ Object
203 204 205 |
# File 'lib/cuken/api/aruba/api.rb', line 203 def processes @processes ||= [] end |
#regexp(string_or_regexp) ⇒ Object
134 135 136 |
# File 'lib/cuken/api/aruba/api.rb', line 134 def regexp(string_or_regexp) Regexp === string_or_regexp ? string_or_regexp : Regexp.compile(Regexp.escape(string_or_regexp)) end |
#register_process(name, process) ⇒ Object
213 214 215 |
# File 'lib/cuken/api/aruba/api.rb', line 213 def register_process(name, process) processes << [name, process] end |
#remove_file(file_name) ⇒ Object
43 44 45 46 47 |
# File 'lib/cuken/api/aruba/api.rb', line 43 def remove_file(file_name) in_dir do ::FileUtils.rm(file_name) end end |
#restore_env ⇒ Object
329 330 331 332 333 |
# File 'lib/cuken/api/aruba/api.rb', line 329 def restore_env original_env.each do |key, value| ENV[key] = value end end |
#run(cmd, dir = current_dir) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/cuken/api/aruba/api.rb', line 225 def run(cmd, dir=current_dir) cmd = detect_ruby(cmd) in_dir(dir) do announce_or_puts("$ cd #{Dir.pwd}") if @announce_dir announce_or_puts("$ #{cmd}") if @announce_cmd process = Process.new(cmd, exit_timeout, io_wait) register_process(cmd, process) process.run! block_given? ? yield(process) : process end end |
#run_interactive(cmd, dir = current_dir) ⇒ Object
267 268 269 |
# File 'lib/cuken/api/aruba/api.rb', line 267 def run_interactive(cmd, dir=current_dir) @interactive = run(cmd, dir) end |
#run_simple(cmd, dir = current_dir, fail_on_error = true) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/cuken/api/aruba/api.rb', line 252 def run_simple(cmd, dir=current_dir, fail_on_error=true) @last_exit_status = run(cmd, dir) do |process| process.stop announce_or_puts(process.stdout) if @announce_stdout announce_or_puts(process.stderr) if @announce_stderr # need to replace with process.exit_code or similar, or remove the block entirely... it doesn't add as much as I thought it would process.stop end @timed_out = @last_exit_status.nil? if(@last_exit_status != 0 && fail_on_error) fail("Exit status was #{@last_exit_status}. Output:\n#{all_output}") end end |
#set_env(key, value) ⇒ Object
323 324 325 326 327 |
# File 'lib/cuken/api/aruba/api.rb', line 323 def set_env(key, value) announce_or_puts(%{$ export #{key}="#{value}"}) if @announce_env original_env[key] = ENV.delete(key) ENV[key] = value end |
#stderr_from(cmd) ⇒ Object
148 149 150 151 |
# File 'lib/cuken/api/aruba/api.rb', line 148 def stderr_from(cmd) cmd = detect_ruby(cmd) get_process(cmd).stderr end |
#stdout_from(cmd) ⇒ Object
143 144 145 146 |
# File 'lib/cuken/api/aruba/api.rb', line 143 def stdout_from(cmd) cmd = detect_ruby(cmd) get_process(cmd).stdout end |
#stop_processes! ⇒ Object
207 208 209 210 211 |
# File 'lib/cuken/api/aruba/api.rb', line 207 def stop_processes! processes.each do |_, process| process.stop end end |
#type(input) ⇒ Object
271 272 273 |
# File 'lib/cuken/api/aruba/api.rb', line 271 def type(input) _write_interactive(_ensure_newline(input)) end |
#unescape(string) ⇒ Object
130 131 132 |
# File 'lib/cuken/api/aruba/api.rb', line 130 def unescape(string) string.gsub('\n', "\n").gsub('\"', '"') end |
#unset_bundler_env_vars ⇒ Object
317 318 319 320 321 |
# File 'lib/cuken/api/aruba/api.rb', line 317 def unset_bundler_env_vars %w[RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE].each do |key| set_env(key, nil) end end |
#use_clean_gemset(gemset) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/cuken/api/aruba/api.rb', line 299 def use_clean_gemset(gemset) run_simple(%{rvm gemset create "#{gemset}"}, true) if all_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_simple("gem install bundler", true) else raise "I didn't understand rvm's output: #{all_stdout}" end end |
#write_file(file_name, file_content) ⇒ Object
27 28 29 |
# File 'lib/cuken/api/aruba/api.rb', line 27 def write_file(file_name, file_content) _create_file(file_name, file_content, false) end |