Module: PickledAruba::Api
- Defined in:
- lib/pickled_aruba/api.rb
Instance Method Summary collapse
- #_mkdir(dir_name) ⇒ Object
- #announce_or_puts(msg) ⇒ Object
- #append_to_file(file_name, file_content) ⇒ Object
- #assert_exit_status_and_partial_output(expect_to_pass, partial_output, case_insensitive = :case_sensitive) ⇒ Object
- #assert_failing_with(partial_output, case_insensitive = :case_sensitive) ⇒ Object
- #assert_partial_output(partial_output, case_insensitive = :case_sensitive) ⇒ Object
- #assert_passing_with(partial_output, case_insensitive = :case_sensitive) ⇒ 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) ⇒ Object
- #check_file_presence(paths, expect_presence) ⇒ Object
- #combined_output ⇒ Object
- #create_dir(dir_name) ⇒ Object
- #create_file(file_name, file_content, check_presence = false) ⇒ Object
- #current_dir ⇒ Object
- #current_ruby ⇒ Object
- #detect_ruby(cmd) ⇒ Object
- #dirs ⇒ Object
- #ensure_newline(str) ⇒ Object
- #in_current_dir(&block) ⇒ Object
- #install_gems(gemfile) ⇒ Object
- #interactive_output ⇒ Object
- #original_env ⇒ Object
- #regexp(string_or_regexp, case_insensitive = :case_sensitive) ⇒ Object
- #restore_env ⇒ Object
- #run(cmd, fail_on_error = true) ⇒ Object
- #run_interactive(cmd) ⇒ Object
- #set_env(key, value) ⇒ Object
- #unescape(string) ⇒ Object
- #unset_bundler_env_vars ⇒ Object
- #use_clean_gemset(gemset) ⇒ Object
- #write_interactive(input) ⇒ Object
Instance Method Details
#_mkdir(dir_name) ⇒ Object
87 88 89 |
# File 'lib/pickled_aruba/api.rb', line 87 def _mkdir(dir_name) FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name) end |
#announce_or_puts(msg) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/pickled_aruba/api.rb', line 178 def announce_or_puts(msg) if(@puts) puts(msg) else announce(msg) end end |
#append_to_file(file_name, file_content) ⇒ Object
33 34 35 36 37 |
# File 'lib/pickled_aruba/api.rb', line 33 def append_to_file(file_name, file_content) in_current_dir do File.open(file_name, 'a') { |f| f << file_content } end end |
#assert_exit_status_and_partial_output(expect_to_pass, partial_output, case_insensitive = :case_sensitive) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/pickled_aruba/api.rb', line 119 def assert_exit_status_and_partial_output(expect_to_pass, partial_output, case_insensitive = :case_sensitive) assert_partial_output(partial_output, case_insensitive) if expect_to_pass @last_exit_status.should == 0 else @last_exit_status.should_not == 0 end end |
#assert_failing_with(partial_output, case_insensitive = :case_sensitive) ⇒ Object
115 116 117 |
# File 'lib/pickled_aruba/api.rb', line 115 def assert_failing_with(partial_output, case_insensitive = :case_sensitive) assert_exit_status_and_partial_output(false, partial_output, case_insensitive) end |
#assert_partial_output(partial_output, case_insensitive = :case_sensitive) ⇒ Object
107 108 109 |
# File 'lib/pickled_aruba/api.rb', line 107 def assert_partial_output(partial_output, case_insensitive = :case_sensitive) combined_output.should =~ regexp(partial_output, case_insensitive) end |
#assert_passing_with(partial_output, case_insensitive = :case_sensitive) ⇒ Object
111 112 113 |
# File 'lib/pickled_aruba/api.rb', line 111 def assert_passing_with(partial_output, case_insensitive = :case_sensitive) assert_exit_status_and_partial_output(true, partial_output, case_insensitive) end |
#cd(dir) ⇒ Object
16 17 18 19 |
# File 'lib/pickled_aruba/api.rb', line 16 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
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pickled_aruba/api.rb', line 75 def check_directory_presence(paths, expect_presence) 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 |
#check_exact_file_content(file, exact_content) ⇒ Object
69 70 71 72 73 |
# File 'lib/pickled_aruba/api.rb', line 69 def check_exact_file_content(file, exact_content) in_current_dir do IO.read(file).should == exact_content end end |
#check_file_content(file, partial_content, expect_match) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pickled_aruba/api.rb', line 57 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 |
#check_file_presence(paths, expect_presence) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pickled_aruba/api.rb', line 45 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 |
#combined_output ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/pickled_aruba/api.rb', line 99 def combined_output if @interactive interactive_output else @last_stdout + @last_stderr end end |
#create_dir(dir_name) ⇒ Object
39 40 41 42 43 |
# File 'lib/pickled_aruba/api.rb', line 39 def create_dir(dir_name) in_current_dir do _mkdir(dir_name) end end |
#create_file(file_name, file_content, check_presence = false) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/pickled_aruba/api.rb', line 25 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 |
#current_dir ⇒ Object
12 13 14 |
# File 'lib/pickled_aruba/api.rb', line 12 def current_dir File.join(*dirs) end |
#current_ruby ⇒ Object
194 195 196 |
# File 'lib/pickled_aruba/api.rb', line 194 def current_ruby File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']) end |
#detect_ruby(cmd) ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/pickled_aruba/api.rb', line 186 def detect_ruby(cmd) if cmd =~ /^ruby\s/ cmd.gsub(/^ruby\s/, "#{current_ruby} ") else cmd end end |
#dirs ⇒ Object
21 22 23 |
# File 'lib/pickled_aruba/api.rb', line 21 def dirs @dirs ||= ['tmp/pickled_aruba'] end |
#ensure_newline(str) ⇒ Object
238 239 240 |
# File 'lib/pickled_aruba/api.rb', line 238 def ensure_newline(str) str.chomp << "\n" end |
#in_current_dir(&block) ⇒ Object
7 8 9 10 |
# File 'lib/pickled_aruba/api.rb', line 7 def in_current_dir(&block) _mkdir(current_dir) Dir.chdir(current_dir, &block) end |
#install_gems(gemfile) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/pickled_aruba/api.rb', line 128 def install_gems(gemfile) create_file("Gemfile", gemfile) if ENV['GOTGEMS'].nil? run("gem install bundler") run("bundle --no-color install") end end |
#interactive_output ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/pickled_aruba/api.rb', line 165 def interactive_output if @interactive @interactive.wait(1) || @interactive.kill('TERM') @interactive.stdout.read else "" end end |
#original_env ⇒ Object
234 235 236 |
# File 'lib/pickled_aruba/api.rb', line 234 def original_env @original_env ||= {} end |
#regexp(string_or_regexp, case_insensitive = :case_sensitive) ⇒ Object
95 96 97 |
# File 'lib/pickled_aruba/api.rb', line 95 def regexp(string_or_regexp, case_insensitive = :case_sensitive) Regexp === string_or_regexp ? string_or_regexp : Regexp.compile(Regexp.escape(string_or_regexp), (case_insensitive == :case_insensitive)) end |
#restore_env ⇒ Object
228 229 230 231 232 |
# File 'lib/pickled_aruba/api.rb', line 228 def restore_env original_env.each do |key, value| ENV[key] = value end end |
#run(cmd, fail_on_error = true) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/pickled_aruba/api.rb', line 136 def run(cmd, fail_on_error=true) cmd = detect_ruby(cmd) in_current_dir do announce_or_puts("$ cd #{Dir.pwd}") if @announce_dir announce_or_puts("$ #{cmd}") if @announce_cmd ps = BackgroundProcess.run(cmd) @last_exit_status = ps.exitstatus # waits for the process to finish @last_stdout = ps.stdout.read announce_or_puts(@last_stdout) if @announce_stdout @last_stderr = ps.stderr.read announce_or_puts(@last_stderr) if @announce_stderr end if(@last_exit_status != 0 && fail_on_error) fail("Exit status was #{@last_exit_status}. Output:\n#{combined_output}") end @last_stderr end |
#run_interactive(cmd) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/pickled_aruba/api.rb', line 157 def run_interactive(cmd) cmd = detect_ruby(cmd) in_current_dir do @interactive = BackgroundProcess.run(cmd) end end |
#set_env(key, value) ⇒ Object
222 223 224 225 226 |
# File 'lib/pickled_aruba/api.rb', line 222 def set_env(key, value) announce_or_puts(%{$ export #{key}="#{value}"}) if @announce_env original_env[key] = ENV.delete(key) ENV[key] = value end |
#unescape(string) ⇒ Object
91 92 93 |
# File 'lib/pickled_aruba/api.rb', line 91 def unescape(string) eval(%{"#{string}"}) end |
#unset_bundler_env_vars ⇒ Object
216 217 218 219 220 |
# File 'lib/pickled_aruba/api.rb', line 216 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
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/pickled_aruba/api.rb', line 198 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 |
#write_interactive(input) ⇒ Object
174 175 176 |
# File 'lib/pickled_aruba/api.rb', line 174 def write_interactive(input) @interactive.stdin.write(input) end |