pickled_aruba 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ When /^I do pickled_aruba (.*)$/ do |aruba_step|
2
+ begin
3
+ When(aruba_step)
4
+ rescue => e
5
+ @aruba_exception = e
6
+ end
7
+ end
8
+
9
+ Then /^pickled_aruba should fail with "([^"]*)"$/ do |error_message|
10
+ @aruba_exception.message.should =~ regexp(error_message)
11
+ end
12
+
13
+ Then /^the following step should fail with Spec::Expectations::ExpectationNotMetError:$/ do |multiline_step|
14
+ proc {steps multiline_step}.should raise_error(RSpec::Expectations::ExpectationNotMetError)
15
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'pickled_aruba'
3
+ require 'fileutils'
4
+ require 'rspec'
5
+
6
+ begin
7
+ # rspec-2
8
+ require 'rspec/expectations'
9
+ rescue LoadError
10
+ # rspec-1
11
+ require 'spec/expectations'
12
+ end
13
+
14
+ Before do
15
+ FileUtils.rm(Dir['config/*.yml'])
16
+ end
@@ -0,0 +1,2 @@
1
+ require 'pickled_aruba/version'
2
+ require 'pickled_aruba/cucumber'
@@ -0,0 +1,242 @@
1
+ require 'tempfile'
2
+ require 'rbconfig'
3
+ require 'background_process'
4
+
5
+ module PickledAruba
6
+ module Api
7
+ def in_current_dir(&block)
8
+ _mkdir(current_dir)
9
+ Dir.chdir(current_dir, &block)
10
+ end
11
+
12
+ def current_dir
13
+ File.join(*dirs)
14
+ end
15
+
16
+ def cd(dir)
17
+ dirs << dir
18
+ raise "#{current_dir} is not a directory." unless File.directory?(current_dir)
19
+ end
20
+
21
+ def dirs
22
+ @dirs ||= ['tmp/pickled_aruba']
23
+ end
24
+
25
+ def create_file(file_name, file_content, check_presence = false)
26
+ in_current_dir do
27
+ raise "expected #{file_name} to be present" if check_presence && !File.file?(file_name)
28
+ _mkdir(File.dirname(file_name))
29
+ File.open(file_name, 'w') { |f| f << file_content }
30
+ end
31
+ end
32
+
33
+ def append_to_file(file_name, file_content)
34
+ in_current_dir do
35
+ File.open(file_name, 'a') { |f| f << file_content }
36
+ end
37
+ end
38
+
39
+ def create_dir(dir_name)
40
+ in_current_dir do
41
+ _mkdir(dir_name)
42
+ end
43
+ end
44
+
45
+ def check_file_presence(paths, expect_presence)
46
+ in_current_dir do
47
+ paths.each do |path|
48
+ if expect_presence
49
+ File.should be_file(path)
50
+ else
51
+ File.should_not be_file(path)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def check_file_content(file, partial_content, expect_match)
58
+ regexp = regexp(partial_content)
59
+ in_current_dir do
60
+ content = IO.read(file)
61
+ if expect_match
62
+ content.should =~ regexp
63
+ else
64
+ content.should_not =~ regexp
65
+ end
66
+ end
67
+ end
68
+
69
+ def check_exact_file_content(file, exact_content)
70
+ in_current_dir do
71
+ IO.read(file).should == exact_content
72
+ end
73
+ end
74
+
75
+ def check_directory_presence(paths, expect_presence)
76
+ in_current_dir do
77
+ paths.each do |path|
78
+ if expect_presence
79
+ File.should be_directory(path)
80
+ else
81
+ File.should_not be_directory(path)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def _mkdir(dir_name)
88
+ FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
89
+ end
90
+
91
+ def unescape(string)
92
+ eval(%{"#{string}"})
93
+ end
94
+
95
+ def regexp(string_or_regexp, case_insensitive = :case_sensitive)
96
+ Regexp === string_or_regexp ? string_or_regexp : Regexp.compile(Regexp.escape(string_or_regexp), (case_insensitive == :case_insensitive))
97
+ end
98
+
99
+ def combined_output
100
+ if @interactive
101
+ interactive_output
102
+ else
103
+ @last_stdout + @last_stderr
104
+ end
105
+ end
106
+
107
+ def assert_partial_output(partial_output, case_insensitive = :case_sensitive)
108
+ combined_output.should =~ regexp(partial_output, case_insensitive)
109
+ end
110
+
111
+ def assert_passing_with(partial_output, case_insensitive = :case_sensitive)
112
+ assert_exit_status_and_partial_output(true, partial_output, case_insensitive)
113
+ end
114
+
115
+ def assert_failing_with(partial_output, case_insensitive = :case_sensitive)
116
+ assert_exit_status_and_partial_output(false, partial_output, case_insensitive)
117
+ end
118
+
119
+ def assert_exit_status_and_partial_output(expect_to_pass, partial_output, case_insensitive = :case_sensitive)
120
+ assert_partial_output(partial_output, case_insensitive)
121
+ if expect_to_pass
122
+ @last_exit_status.should == 0
123
+ else
124
+ @last_exit_status.should_not == 0
125
+ end
126
+ end
127
+
128
+ def install_gems(gemfile)
129
+ create_file("Gemfile", gemfile)
130
+ if ENV['GOTGEMS'].nil?
131
+ run("gem install bundler")
132
+ run("bundle --no-color install")
133
+ end
134
+ end
135
+
136
+ def run(cmd, fail_on_error=true)
137
+ cmd = detect_ruby(cmd)
138
+
139
+ in_current_dir do
140
+ announce_or_puts("$ cd #{Dir.pwd}") if @announce_dir
141
+ announce_or_puts("$ #{cmd}") if @announce_cmd
142
+ ps = BackgroundProcess.run(cmd)
143
+ @last_exit_status = ps.exitstatus # waits for the process to finish
144
+ @last_stdout = ps.stdout.read
145
+ announce_or_puts(@last_stdout) if @announce_stdout
146
+ @last_stderr = ps.stderr.read
147
+ announce_or_puts(@last_stderr) if @announce_stderr
148
+ end
149
+
150
+ if(@last_exit_status != 0 && fail_on_error)
151
+ fail("Exit status was #{@last_exit_status}. Output:\n#{combined_output}")
152
+ end
153
+
154
+ @last_stderr
155
+ end
156
+
157
+ def run_interactive(cmd)
158
+ cmd = detect_ruby(cmd)
159
+
160
+ in_current_dir do
161
+ @interactive = BackgroundProcess.run(cmd)
162
+ end
163
+ end
164
+
165
+ def interactive_output
166
+ if @interactive
167
+ @interactive.wait(1) || @interactive.kill('TERM')
168
+ @interactive.stdout.read
169
+ else
170
+ ""
171
+ end
172
+ end
173
+
174
+ def write_interactive(input)
175
+ @interactive.stdin.write(input)
176
+ end
177
+
178
+ def announce_or_puts(msg)
179
+ if(@puts)
180
+ puts(msg)
181
+ else
182
+ announce(msg)
183
+ end
184
+ end
185
+
186
+ def detect_ruby(cmd)
187
+ if cmd =~ /^ruby\s/
188
+ cmd.gsub(/^ruby\s/, "#{current_ruby} ")
189
+ else
190
+ cmd
191
+ end
192
+ end
193
+
194
+ def current_ruby
195
+ File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
196
+ end
197
+
198
+ def use_clean_gemset(gemset)
199
+ run(%{rvm gemset create "#{gemset}"}, true)
200
+ if @last_stdout =~ /'#{gemset}' gemset created \((.*)\)\./
201
+ gem_home = $1
202
+ set_env('GEM_HOME', gem_home)
203
+ set_env('GEM_PATH', gem_home)
204
+ set_env('BUNDLE_PATH', gem_home)
205
+
206
+ paths = (ENV['PATH'] || "").split(File::PATH_SEPARATOR)
207
+ paths.unshift(File.join(gem_home, 'bin'))
208
+ set_env('PATH', paths.uniq.join(File::PATH_SEPARATOR))
209
+
210
+ run("gem install bundler", true)
211
+ else
212
+ raise "I didn't understand rvm's output: #{@last_stdout}"
213
+ end
214
+ end
215
+
216
+ def unset_bundler_env_vars
217
+ %w[RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE].each do |key|
218
+ set_env(key, nil)
219
+ end
220
+ end
221
+
222
+ def set_env(key, value)
223
+ announce_or_puts(%{$ export #{key}="#{value}"}) if @announce_env
224
+ original_env[key] = ENV.delete(key)
225
+ ENV[key] = value
226
+ end
227
+
228
+ def restore_env
229
+ original_env.each do |key, value|
230
+ ENV[key] = value
231
+ end
232
+ end
233
+
234
+ def original_env
235
+ @original_env ||= {}
236
+ end
237
+
238
+ def ensure_newline(str)
239
+ str.chomp << "\n"
240
+ end
241
+ end
242
+ end
@@ -0,0 +1,236 @@
1
+ require 'pickled_aruba/api'
2
+
3
+ World(PickledAruba::Api)
4
+
5
+ Before('@disable-bundler') do
6
+ unset_bundler_env_vars
7
+ end
8
+
9
+ Before do
10
+ @__pickled_aruba_original_paths = (ENV['PATH'] || '').split(File::PATH_SEPARATOR)
11
+ ENV['PATH'] = ([File.expand_path('bin')] + @__pickled_aruba_original_paths).join(File::PATH_SEPARATOR)
12
+ end
13
+
14
+ After do
15
+ ENV['PATH'] = @__pickled_aruba_original_paths.join(File::PATH_SEPARATOR)
16
+ end
17
+
18
+ Before do
19
+ FileUtils.rm_rf(current_dir)
20
+ end
21
+
22
+ Before('@puts') do
23
+ @puts = true
24
+ end
25
+
26
+ Before('@announce-cmd') do
27
+ @announce_cmd = true
28
+ end
29
+
30
+ Before('@announce-stdout') do
31
+ @announce_stdout = true
32
+ end
33
+
34
+ Before('@announce-stderr') do
35
+ @announce_stderr = true
36
+ end
37
+
38
+ Before('@announce-dir') do
39
+ @announce_dir = true
40
+ end
41
+
42
+ Before('@announce-env') do
43
+ @announce_env = true
44
+ end
45
+
46
+ Before('@announce') do
47
+ @announce_stdout = true
48
+ @announce_stderr = true
49
+ @announce_cmd = true
50
+ @announce_dir = true
51
+ @announce_env = true
52
+ end
53
+
54
+ After do
55
+ restore_env
56
+ end
57
+
58
+ Given /^I'm using a clean gemset "([^"]*)"$/ do |gemset|
59
+ use_clean_gemset(gemset)
60
+ end
61
+
62
+ Given /^a directory named "([^"]*)"$/ do |dir_name|
63
+ create_dir(dir_name)
64
+ end
65
+
66
+ Given /^a file named "([^"]*)" with:$/ do |file_name, file_content|
67
+ create_file(file_name, file_content)
68
+ end
69
+
70
+ Given /^an empty file named "([^"]*)"$/ do |file_name|
71
+ create_file(file_name, "")
72
+ end
73
+
74
+ When /^I write to "([^"]*)" with:$/ do |file_name, file_content|
75
+ create_file(file_name, file_content, false)
76
+ end
77
+
78
+ When /^I overwrite "([^"]*)" with:$/ do |file_name, file_content|
79
+ create_file(file_name, file_content, true)
80
+ end
81
+
82
+ When /^I append to "([^"]*)" with:$/ do |file_name, file_content|
83
+ append_to_file(file_name, file_content)
84
+ end
85
+
86
+ When /^I cd to "([^"]*)"$/ do |dir|
87
+ cd(dir)
88
+ end
89
+
90
+ When /^I run "(.*)"$/ do |cmd|
91
+ run(unescape(cmd), false)
92
+ end
93
+
94
+ When /^I successfully run "(.*)"$/ do |cmd|
95
+ run(unescape(cmd))
96
+ end
97
+
98
+ When /^I run "([^"]*)" interactively$/ do |cmd|
99
+ run_interactive(unescape(cmd))
100
+ end
101
+
102
+ When /^I type "([^"]*)"$/ do |input|
103
+ write_interactive(ensure_newline(input))
104
+ end
105
+
106
+ #(\w+ |\".*?\"|'.*?')
107
+ #(\w+)|(?:')(.+)(?:')|(?: )(\w+)|(\w+)(?: )
108
+
109
+ Then /^the output should(, regardless of case,|) contain "([^"]*)"( each of these|)$/ do |case_insensitive, partial_output, is_array|
110
+ (is_array == nil ? [partial_output] : partial_output.scan(/(\w+)|(?:')(.+)(?:')/).flatten.compact).each do |partial_output_each|
111
+ assert_partial_output(partial_output_each, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
112
+ end
113
+ end
114
+
115
+ Then /^the output should not(, regardless of case,|) contain "([^"]*)"$/ do |case_insensitive, partial_output|
116
+ combined_output.should_not =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
117
+ end
118
+
119
+ Then /^the output should(, regardless of case,|) contain:$/ do |case_insensitive, partial_output|
120
+ combined_output.should =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
121
+ end
122
+
123
+ Then /^the output should not(, regardless of case,|) contain:$/ do |case_insensitive, partial_output|
124
+ combined_output.should_not =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
125
+ end
126
+
127
+ Then /^the output should(, regardless of case,|) contain exactly "([^"]*)"$/ do |case_insensitive, exact_output|
128
+ combined_output.should == unescape(exact_output) if case_insensitive == ""
129
+ combined_output.downcase.should == unescape(exact_output).downcase if case_insensitive != nil
130
+ end
131
+
132
+ Then /^the output should(, regardless of case,|) contain exactly:$/ do |case_insensitive, exact_output|
133
+ combined_output.should == exact_output if case_insensitive == ""
134
+ combined_output.downcase.should == exact_output.downcase if case_insensitive == ""
135
+ end
136
+
137
+ # "the output should match" allows regex in the partial_output, if
138
+ # you don't need regex, use "the output should contain" instead since
139
+ # that way, you don't have to escape regex characters that
140
+ # appear naturally in the output
141
+ Then /^the output should match \/([^\/]*)\/$/ do |partial_output|
142
+ combined_output.should =~ /#{partial_output}/
143
+ end
144
+
145
+ Then /^the output should match \/([^\/]*)\/i$/ do |partial_output|
146
+ combined_output.should =~ /#{partial_output}/i
147
+ end
148
+
149
+ Then /^the output should not match \/([^\/]*)\/$/ do |partial_output|
150
+ combined_output.should_not =~ /#{partial_output}/
151
+ end
152
+
153
+ Then /^the output should not match \/([^\/]*)\/i$/ do |partial_output|
154
+ combined_output.should_not =~ /#{partial_output}/i
155
+ end
156
+
157
+ Then /^the output should(, regardless of case,|) match:$/ do |case_insensitive, partial_output|
158
+ combined_output.should =~ (case_insensitive == "" ? /#{partial_output}/m : /#{partial_output}/mi)
159
+ end
160
+
161
+ Then /^the output should not(, regardless of case,|) match:$/ do |case_insensitive, partial_output|
162
+ combined_output.should_not =~ (case_insensitive == "" ? /#{partial_output}/m : /#{partial_output}/mi)
163
+ end
164
+
165
+ Then /^the exit status should be (\d+)$/ do |exit_status|
166
+ @last_exit_status.should == exit_status.to_i
167
+ end
168
+
169
+ Then /^the exit status should not be (\d+)$/ do |exit_status|
170
+ @last_exit_status.should_not == exit_status.to_i
171
+ end
172
+
173
+ Then /^it should(, regardless of case,|) (pass|fail) with:$/ do |case_insensitive, pass_fail, partial_output|
174
+ self.__send__("assert_#{pass_fail}ing_with", partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
175
+ end
176
+
177
+ Then /^it should(, regardless of case,|) (pass|fail) with regexp?:$/ do |case_insensitive, pass_fail, partial_output|
178
+ Then "the output should#{case_insensitive} match:", partial_output
179
+ if pass_fail == 'pass'
180
+ @last_exit_status.should == 0
181
+ else
182
+ @last_exit_status.should_not == 0
183
+ end
184
+ end
185
+
186
+ Then /^the stderr should(, regardless of case,|) contain "([^"]*)"$/ do |case_insensitive, partial_output|
187
+ @last_stderr.should =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
188
+ end
189
+
190
+ Then /^the stdout should(, regardless of case,|) contain "([^"]*)"$/ do |case_insensitive, partial_output|
191
+ @last_stdout.should =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
192
+ end
193
+
194
+ Then /^the stderr should not(, regardless of case,|) contain "([^"]*)"$/ do |case_insensitive, partial_output|
195
+ @last_stderr.should_not =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
196
+ end
197
+
198
+ Then /^the stdout should not(, regardless of case,|) contain "([^"]*)"$/ do |case_insensitive, partial_output|
199
+ @last_stdout.should_not =~ regexp(partial_output, (case_insensitive == "" ? :case_sensitive : :case_insensitive))
200
+ end
201
+
202
+ Then /^the following files should exist:$/ do |files|
203
+ check_file_presence(files.raw.map{|file_row| file_row[0]}, true)
204
+ end
205
+
206
+ Then /^the following files should not exist:$/ do |files|
207
+ check_file_presence(files.raw.map{|file_row| file_row[0]}, false)
208
+ end
209
+
210
+ Then /^the following directories should exist:$/ do |directories|
211
+ check_directory_presence(directories.raw.map{|directory_row| directory_row[0]}, true)
212
+ end
213
+
214
+ Then /^the following directories should not exist:$/ do |directories|
215
+ check_directory_presence(directories.raw.map{|directory_row| directory_row[0]}, false)
216
+ end
217
+
218
+ Then /^the file "([^"]*)" should contain "([^"]*)"$/ do |file, partial_content|
219
+ check_file_content(file, partial_content, true)
220
+ end
221
+
222
+ Then /^the file "([^"]*)" should not contain "([^"]*)"$/ do |file, partial_content|
223
+ check_file_content(file, partial_content, false)
224
+ end
225
+
226
+ Then /^the file "([^"]*)" should contain exactly:$/ do |file, exact_content|
227
+ check_exact_file_content(file, exact_content)
228
+ end
229
+
230
+ Then /^the file "([^"]*)" should match \/([^\/]*)\/$/ do |file, partial_content|
231
+ check_file_content(file, /#{partial_content}/, true)
232
+ end
233
+
234
+ Then /^the file "([^"]*)" should not match \/([^\/]*)\/$/ do |file, partial_content|
235
+ check_file_content(file, /#{partial_content}/, false)
236
+ end