aruba-jbb 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,76 @@
1
+ @announce
2
+ Feature: Running ruby
3
+ In order to test a Ruby-based CLI app
4
+ I want to have control over what Ruby version is used,
5
+ possibly using a different Ruby than the one I launch Cucumber with.
6
+
7
+ Scenario: Run with ruby 1.9.1
8
+ Given I am using rvm "1.9.1"
9
+ When I run "ruby -e 'puts RUBY_VERSION'"
10
+ Then the output should contain "ruby-1.9.1-p378"
11
+ And the output should not contain "rvm usage"
12
+
13
+ Scenario: Run with ruby JRuby
14
+ Given I am using rvm "jruby"
15
+ When I run "ruby -e 'puts JRUBY_VERSION'"
16
+ Then the output should contain "1.5.1"
17
+ And the output should not contain "rvm usage"
18
+
19
+ Scenario: Install gems with bundler
20
+ Given I am using rvm "1.9.1"
21
+ And I am using rvm gemset "a-new-gemset-where-no-gems-are-installed" with Gemfile:
22
+ """
23
+ source :gemcutter
24
+ gem 'diff-lcs', '1.1.2'
25
+ """
26
+ When I run "gem list"
27
+ Then the output should match:
28
+ """
29
+ bundler \(\d+\.+\d+\.+\d+\)
30
+ diff-lcs \(\d+\.+\d+\.+\d+\)
31
+ """
32
+
33
+ Scenario: Find the version of ruby 1.9.1
34
+ Given I am using rvm "1.9.1"
35
+ When I run "ruby --version"
36
+ Then the output should contain "1.9.1"
37
+
38
+ Scenario: Find the version of cucumber on ruby 1.9.1
39
+ Given I am using rvm "1.9.1"
40
+ When I run "cucumber --version"
41
+ Then the output should match /\d+\.+\d+\.+\d+/
42
+
43
+ Scenario: Use current ruby
44
+ When I run "ruby --version"
45
+ Then the output should contain the current Ruby version
46
+
47
+ Scenario: Use current ruby and a gem bin file
48
+ When I run "rake --version"
49
+ Then the output should contain "rake, version"
50
+
51
+ # I have trouble running rvm 1.8.7 in OS X Leopard, which is
52
+ # ruby-1.8.7-p249 (It segfaults rather often). However, a previous
53
+ # patchlevel of 1.8.7 runs fine for me: ruby-1.8.7-tv1_8_7_174
54
+ #
55
+ # In order to provide some level of flexibility (and readability)
56
+ # it should be possible to set up a mapping from a version specified
57
+ # in Gherkin to an *actual* version.
58
+ #
59
+ # In order to make this scenario pass you therefore need to install
60
+ # ruby-1.8.7-tv1_8_7_174 with rvm.
61
+ Scenario: Specify an alias for an rvm
62
+ Given I do have a file named "config/aruba-rvm.yml" with:
63
+ """
64
+ 1.8.7: ruby-1.8.7-tv1_8_7_174
65
+ """
66
+ And I am using rvm "1.8.7"
67
+ When I run "ruby --version"
68
+ Then the output should contain "patchlevel 174"
69
+
70
+ # To verify this, make sure current rvm is *not* JRuby and run:
71
+ #
72
+ # ~/.rvm/rubies/jruby-1.4.0/bin/jruby -S cucumber features/running_ruby.feature -n "launched Cucumber"
73
+ @jruby
74
+ Scenario: Don't use rvm, but default to same Ruby as the one that launched Cucumber
75
+ When I run "ruby -e 'puts JRUBY_VERSION if defined?(JRUBY_VERSION)'"
76
+ Then the output should contain the JRuby version
@@ -0,0 +1,74 @@
1
+ Before('@rebase-test') do
2
+ @rebase_test_dir = 'testdata'
3
+ @soft_link_target = File.join(current_dir.to_s, @rebase_test_dir)
4
+ # puts @soft_link_target
5
+ FileUtils.rm_rf(@rebase_test_dir)
6
+ FileUtils.mkdir_p(@rebase_test_dir)
7
+ rebase(@rebase_test_dir)
8
+ #File.open("delete_me.txt", 'w') { |f| f << "" }
9
+ end
10
+
11
+
12
+ After('@rebase-test') do
13
+ FileUtils.rm_rf(@rebase_test_dir)
14
+ end
15
+
16
+
17
+ When /create the cwd sub-directory named "([^\"]*)"/ do |dir|
18
+ FileUtils.mkdir_p(dir)
19
+ end
20
+
21
+
22
+ When /delete the cwd sub-directory named "([^\"]*)"/ do |dir|
23
+ FileUtils.rm_rf(dir)
24
+ end
25
+
26
+
27
+ When /^"([^\"]*)" should have a soft link in the aruba working directory$/ do \
28
+ |dir|
29
+ link = File.join(current_dir.to_s, dir)
30
+ File.symlink?(link).should be_true
31
+ end
32
+
33
+
34
+ When /the rebase-test before block conditions/ do
35
+ # Do nothing
36
+ end
37
+
38
+
39
+ When /soft links should exist in the aruba working directory/ do
40
+ File.symlink?(@soft_link_target).should be_true
41
+ end
42
+
43
+
44
+ When /do aruba (.*) step$/ do |aruba_step|
45
+ begin
46
+ When(aruba_step)
47
+ rescue => e
48
+ @aruba_exception = e
49
+ end
50
+ end
51
+
52
+
53
+ When /the clean_up api method should fail/ do
54
+ begin
55
+ clean_up
56
+ fail("clean_up api method did not raise error and should have")
57
+ rescue => @last_stderr
58
+ end
59
+ end
60
+
61
+
62
+ When /^the output should contain the JRuby version$/ do
63
+ Then %{the output should contain "#{JRUBY_VERSION}"}
64
+ end
65
+
66
+
67
+ When /^the output should contain the current Ruby version$/ do
68
+ Then %{the output should contain "#{RUBY_VERSION}"}
69
+ end
70
+
71
+
72
+ When /^aruba should fail with "([^\"]*)"$/ do |error_message|
73
+ @aruba_exception.message.should =~ compile_and_escape(error_message)
74
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'aruba'
3
+ require 'fileutils'
4
+
5
+ begin
6
+ # rspec-2
7
+ require 'rspec/expectations'
8
+ rescue LoadError
9
+ # rspec-1
10
+ require 'spec/expectations'
11
+ end
12
+
13
+ Before do
14
+ FileUtils.rm(Dir['config/*.yml'])
15
+ end
data/lib/aruba/api.rb ADDED
@@ -0,0 +1,296 @@
1
+ require 'tempfile'
2
+ require 'rbconfig'
3
+
4
+ module Aruba
5
+ module Api
6
+
7
+ def announce_or_puts(msg)
8
+ if(@puts)
9
+ puts(msg)
10
+ else
11
+ announce(msg)
12
+ end
13
+ end
14
+
15
+ def append_to_file(file_name, file_content)
16
+ in_current_dir do
17
+ File.open(file_name, 'a') { |f| f << file_content }
18
+ end
19
+ end
20
+
21
+ def aruba_working_dir
22
+ @aruba_working_dir
23
+ end
24
+
25
+ def x_ray(dir)
26
+ fail("In x_ray")
27
+ end
28
+
29
+ # This allows before hooks to set aruba's working directory
30
+ # relative to user's cwd
31
+ def aruba_working_dir_set(dir)
32
+ @aruba_working_dir = dir
33
+ dirs_init
34
+ end
35
+
36
+ # You can override the default working directory by setting
37
+ # the environment variable ARUBA_WORKING_DIR
38
+ ARUBA_WORKING_DIR_DEFAULT = 'tmp/aruba'
39
+
40
+ def aruba_working_dir_init
41
+
42
+ if defined?(ENV[ARUBA_WORKING_DIR])
43
+ @aruba_working_dir = [ENV[ARUBA_WORKING_DIR]]
44
+ else
45
+ @aruba_working_dir ||= ['tmp/aruba']
46
+ end
47
+
48
+ dirs_init
49
+
50
+ clean_up
51
+
52
+ rebase_dirs_clear
53
+
54
+ if defined?(ENV[ARUBA_REBASE])
55
+ rebase(ENV[ARUBA_REBASE].split(%r{,|;\s*}))
56
+ end
57
+
58
+ end
59
+
60
+ def cd(dir)
61
+ dirs << dir
62
+ raise "#{current_dir} is not a directory." unless File.directory?(current_dir)
63
+ end
64
+
65
+ def check_directory_presence(paths, expect_presence)
66
+ in_current_dir do
67
+ paths.each do |path|
68
+ if expect_presence
69
+ File.should be_directory(path)
70
+ else
71
+ File.should_not be_directory(path)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def check_file_content(file, partial_content, expect_match)
78
+ regexp = compile_and_escape(partial_content)
79
+ in_current_dir do
80
+ content = IO.read(file)
81
+ if expect_match
82
+ content.should =~ regexp
83
+ else
84
+ content.should_not =~ regexp
85
+ end
86
+ end
87
+ end
88
+
89
+ def check_file_presence(paths, expect_presence)
90
+ in_current_dir do
91
+ paths.each do |path|
92
+ if expect_presence
93
+ File.should be_file(path)
94
+ else
95
+ File.should_not be_file(path)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ def clean_up(dir = current_dir)
102
+ check_tmp_dir = File.expand_path(dir)
103
+ if File.fnmatch('**/tmp/**',check_tmp_dir)
104
+ clean_up!
105
+ else
106
+ raise "#{check_tmp_dir} is outside the tmp subtree and may not be deleted."
107
+ end
108
+ end
109
+
110
+ def clean_up!(dir = current_dir)
111
+ FileUtils.rm_rf(dir)
112
+ _mkdir(dir)
113
+ end
114
+
115
+ def combined_output
116
+ @last_stdout.to_s + (@last_stderr.to_s == '' ? \
117
+ '' : "\n#{'-'*70}\n#{@last_stderr}")
118
+ end
119
+
120
+ def compile_and_escape(string)
121
+ Regexp.compile(Regexp.escape(string))
122
+ end
123
+
124
+ def create_dir(dir_name)
125
+ in_current_dir do
126
+ _mkdir(dir_name)
127
+ end
128
+ end
129
+
130
+ def create_file(file_name, file_content)
131
+ in_current_dir do
132
+ _mkdir(File.dirname(file_name))
133
+ File.open(file_name, 'w') { |f| f << file_content }
134
+ end
135
+ end
136
+
137
+ def create_rvm_gemset(rvm_gemset)
138
+ raise "You haven't specified what ruby version rvm should use." if @rvm_ruby_version.nil?
139
+ run "rvm --create #{@rvm_ruby_version}@#{rvm_gemset}"
140
+ end
141
+
142
+ def current_dir
143
+ File.join(*dirs)
144
+ end
145
+
146
+ def current_ruby
147
+ if @rvm_ruby_version
148
+ rvm_ruby_version_with_gemset = @rvm_gemset ? "#{@rvm_ruby_version}@#{@rvm_gemset}" : @rvm_ruby_version
149
+ "rvm #{rvm_ruby_version_with_gemset} ruby"
150
+ else
151
+ File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
152
+ end
153
+ end
154
+
155
+ def delete_rvm_gemset(rvm_gemset)
156
+ raise "You haven't specified what ruby version rvm should use." if @rvm_ruby_version.nil?
157
+ run "rvm --force gemset delete #{@rvm_ruby_version}@#{rvm_gemset}"
158
+ end
159
+
160
+ def detect_ruby(cmd)
161
+ if cmd =~ /^ruby\s/
162
+ cmd.gsub(/^ruby\s/, "#{current_ruby} ")
163
+ else
164
+ cmd
165
+ end
166
+ end
167
+
168
+ COMMON_RUBY_SCRIPTS = /^(?:bundle|cucumber|gem|jeweler|rails|rake|rspec|spec)\s/
169
+
170
+ def detect_ruby_script(cmd)
171
+ if cmd =~ COMMON_RUBY_SCRIPTS
172
+ "ruby -S #{cmd}"
173
+ else
174
+ cmd
175
+ end
176
+ end
177
+
178
+ def pick_up
179
+ @pick_up
180
+ end
181
+
182
+ def dirs
183
+ @dirs ||= dirs_init
184
+ end
185
+
186
+ def dirs_init
187
+ @dirs = []
188
+ @dirs << aruba_working_dir
189
+ end
190
+
191
+ def in_current_dir(&block)
192
+ _mkdir(current_dir)
193
+ Dir.chdir(current_dir, &block)
194
+ end
195
+
196
+ def install_gems(gemfile)
197
+ create_file("Gemfile", gemfile)
198
+ if ENV['GOTGEMS'].nil?
199
+ run("gem install bundler")
200
+ run("bundle install")
201
+ end
202
+ end
203
+
204
+ def _mkdir(dir_name)
205
+ FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
206
+ end
207
+
208
+ def unescape(string)
209
+ eval(%{"#{string}"})
210
+ end
211
+
212
+ def use_rvm(rvm_ruby_version)
213
+ if File.exist?('config/aruba-rvm.yml')
214
+ @rvm_ruby_version = YAML.load_file('config/aruba-rvm.yml')[rvm_ruby_version] || rvm_ruby_version
215
+ else
216
+ @rvm_ruby_version = rvm_ruby_version
217
+ end
218
+ end
219
+
220
+ def rebase(dirs=nil)
221
+
222
+ rebase_dirs_add(dirs) if dirs
223
+
224
+ working_dir = File.expand_path(File.join(FileUtils.pwd, aruba_working_dir))
225
+
226
+ if rebase_dirs and File.fnmatch('**/tmp/**', working_dir)
227
+ rebase_dirs.each do |dir|
228
+ FileUtils.ln_s(File.join(user_working_dir, dir.to_s),
229
+ working_dir, :force => true)
230
+ end
231
+ else
232
+ raise "Aruba's working directory, #{working_dir}, \n" +
233
+ "is outside the tmp subtree and may not be rebased."
234
+ end
235
+
236
+ end
237
+
238
+ def rebase_dirs
239
+ @aruba_rebase_dirs
240
+ end
241
+
242
+ def rebase_dirs_add(dirs=nil)
243
+ return unless dirs
244
+ dirs = dirs.to_a.flatten
245
+ @aruba_rebase_dirs ||= []
246
+ @aruba_rebase_dirs = (@aruba_rebase_dirs + dirs).uniq
247
+ end
248
+
249
+ def rebase_dirs_clear
250
+ @aruba_rebase_dirs = []
251
+ end
252
+
253
+ def run(cmd, fail_on_error=true)
254
+ cmd = detect_ruby_script(cmd)
255
+ cmd = detect_ruby(cmd)
256
+
257
+ announce_or_puts("$ #{cmd}") if @announce_cmd
258
+
259
+ stderr_file = Tempfile.new('cucumber')
260
+ stderr_file.close
261
+ in_current_dir do
262
+ mode = RUBY_VERSION =~ /^1\.9/ ? {:external_encoding=>"UTF-8"} : 'r'
263
+ IO.popen("#{cmd} 2> #{stderr_file.path}", mode) do |io|
264
+ @last_stdout = io.read
265
+
266
+ announce_or_puts(@last_stdout) if @announce_stdout
267
+ end
268
+
269
+ @last_exit_status = $?.exitstatus
270
+ end
271
+ @last_stderr = IO.read(stderr_file.path)
272
+
273
+ announce_or_puts(@last_stderr) if @announce_stderr
274
+
275
+ if(@last_exit_status != 0 && fail_on_error)
276
+ fail("Exit status was #{@last_exit_status}. Output:\n#{combined_output}")
277
+ end
278
+
279
+ @last_stderr
280
+ end
281
+
282
+ def use_rvm_gemset(rvm_gemset, empty_gemset)
283
+ @rvm_gemset = rvm_gemset
284
+ if empty_gemset && ENV['GOTGEMS'].nil?
285
+ delete_rvm_gemset(rvm_gemset)
286
+ create_rvm_gemset(rvm_gemset)
287
+ end
288
+ end
289
+
290
+ def user_working_dir
291
+ # This allows us to find the user's original working directory
292
+ @user_working_dir ||= FileUtils.pwd
293
+ end
294
+
295
+ end #api module
296
+ end # aruba module
@@ -0,0 +1,234 @@
1
+ require 'aruba/api'
2
+
3
+ World(Aruba::Api)
4
+
5
+
6
+ Before do
7
+ aruba_working_dir_init
8
+ end
9
+
10
+
11
+ Before('@aruba-tmpdir') do
12
+ aruba_working_dir_init
13
+ end
14
+
15
+
16
+ Before('@no-aruba-tmpdir') do
17
+ puts aruba_working_dir
18
+ aruba_working_dir_set('./')
19
+ dirs_init
20
+ end
21
+
22
+
23
+ Before('@announce-cmd') do
24
+ @announce_cmd = true
25
+ end
26
+
27
+
28
+ Before('@announce-stdout') do
29
+ @announce_stdout = true
30
+ end
31
+
32
+
33
+ Before('@announce-stderr') do
34
+ @announce_stderr = true
35
+ end
36
+
37
+
38
+ Before('@announce') do
39
+ @announce_stdout = true
40
+ @announce_stderr = true
41
+ @announce_cmd = true
42
+ end
43
+
44
+
45
+ When /append to "([^\"]*)" with:$/ do |file_name, file_content|
46
+ append_to_file(file_name, file_content)
47
+ end
48
+
49
+
50
+ When /cd to "([^\"]*)"$/ do |dir|
51
+ cd(dir)
52
+ end
53
+
54
+
55
+ When /clean up the working directory/ do
56
+ clean_up
57
+ end
58
+
59
+
60
+ When /do have a directory named "([^\"]*)"$/ do |dir_name|
61
+ create_dir(dir_name)
62
+ end
63
+
64
+
65
+ When /do have a file named "([^\"]*)" with:$/ do |file_name, file_content|
66
+ create_file(file_name, file_content)
67
+ end
68
+
69
+
70
+ When /do have an empty file named "([^\"]*)"$/ do |file_name|
71
+ create_file(file_name, "")
72
+ end
73
+
74
+
75
+ When /using rvm "([^\"]*)"$/ do |rvm_ruby_version|
76
+ use_rvm(rvm_ruby_version)
77
+ end
78
+
79
+
80
+ When /using( an empty)? rvm gemset "([^\"]*)"$/ do |empty_gemset, rvm_gemset|
81
+ use_rvm_gemset(rvm_gemset, empty_gemset)
82
+ end
83
+
84
+
85
+ When /using rvm gemset "([^\"]*)" with Gemfile:$/ do |rvm_gemset, gemfile|
86
+ use_rvm_gemset(rvm_gemset, true)
87
+ install_gems(gemfile)
88
+ end
89
+
90
+
91
+ When /rebase the directory named "([^\"]*)"$/ do |dir|
92
+ rebase(dir)
93
+ end
94
+
95
+
96
+ When /run "(.*)"$/ do |cmd|
97
+ run(unescape(cmd), false)
98
+ end
99
+
100
+
101
+ When /run "(.*)" with errors?$/ do |cmd|
102
+ run(unescape(cmd), false)
103
+ end
104
+
105
+
106
+ When /run "(.*)" without errors?$/ do |cmd|
107
+ run(unescape(cmd), true)
108
+ end
109
+
110
+
111
+ When /output should contain "([^\"]*)"$/ do |partial_output|
112
+ combined_output.should =~ compile_and_escape(partial_output)
113
+ end
114
+
115
+
116
+ When /output should not contain "([^\"]*)"$/ do |partial_output|
117
+ combined_output.should_not =~ compile_and_escape(partial_output)
118
+ end
119
+
120
+
121
+ When /output should contain:$/ do |partial_output|
122
+ combined_output.should =~ compile_and_escape(partial_output)
123
+ end
124
+
125
+
126
+ When /output should not contain:$/ do |partial_output|
127
+ combined_output.should_not =~ compile_and_escape(partial_output)
128
+ end
129
+
130
+
131
+ When /output should contain exactly "([^\"]*)"$/ do |exact_output|
132
+ combined_output.should == unescape(exact_output)
133
+ end
134
+
135
+
136
+ When /output should contain exactly:$/ do |exact_output|
137
+ combined_output.should == exact_output
138
+ end
139
+
140
+
141
+ # "the output should match" allows regex in the partial_output, if
142
+ # you don't need regex, use "the output should contain" instead since
143
+ # that way, you don't have to escape regex characters that
144
+ # appear naturally in the output
145
+ When /output should match \/([^\/]*)\/$/ do |partial_output|
146
+ combined_output.should =~ /#{partial_output}/
147
+ end
148
+
149
+
150
+ When /output should match:$/ do |partial_output|
151
+ combined_output.should =~ /#{partial_output}/m
152
+ end
153
+
154
+
155
+ When /exit status should be (\d+)$/ do |exit_status|
156
+ @last_exit_status.should == exit_status.to_i
157
+ end
158
+
159
+
160
+ When /exit status should not be (\d+)$/ do |exit_status|
161
+ @last_exit_status.should_not == exit_status.to_i
162
+ end
163
+
164
+
165
+ When /rebase the directory "()"$/ do |dir|
166
+ rebase(dir.to_a)
167
+ end
168
+
169
+
170
+ When /should (pass|fail) with:$/ do |pass_fail, partial_output|
171
+ When "output should contain:", partial_output
172
+ if pass_fail == 'pass'
173
+ @last_exit_status.should == 0
174
+ else
175
+ @last_exit_status.should_not == 0
176
+ end
177
+ end
178
+
179
+
180
+ When /stderr should be empty$/ do
181
+ @last_stderr.should == ""
182
+ end
183
+
184
+
185
+ When /stderr should contain "([^\"]*)"$/ do |partial_output|
186
+ @last_stderr.should =~ compile_and_escape(partial_output)
187
+ end
188
+
189
+
190
+ When /stdout should contain "([^\"]*)"$/ do |partial_output|
191
+ @last_stdout.should =~ compile_and_escape(partial_output)
192
+ end
193
+
194
+
195
+ When /stderr should not contain "([^\"]*)"$/ do |partial_output|
196
+ @last_stderr.should_not =~ compile_and_escape(partial_output)
197
+ end
198
+
199
+
200
+ When /stdout should not contain "([^\"]*)"$/ do |partial_output|
201
+ @last_stdout.should_not =~ compile_and_escape(partial_output)
202
+ end
203
+
204
+
205
+ When /following files? should exist:$/ do |files|
206
+ check_file_presence(files.raw.map{|file_row| file_row[0]}, true)
207
+ end
208
+
209
+
210
+ When /following files? should not exist:$/ do |files|
211
+ check_file_presence(files.raw.map{|file_row| file_row[0]}, false)
212
+ end
213
+
214
+
215
+ When /following directories? should exist:$/ do |directories|
216
+ check_directory_presence(directories.raw.map{
217
+ |directory_row| directory_row[0]}, true)
218
+ end
219
+
220
+
221
+ When /following directories? should not exist:$/ do |directories|
222
+ check_file_presence(directories.raw.map{
223
+ |directory_row| directory_row[0]}, false)
224
+ end
225
+
226
+
227
+ When /file "([^\"]*)" should contain "([^\"]*)"$/ do |file, partial_content|
228
+ check_file_content(file, partial_content, true)
229
+ end
230
+
231
+
232
+ When /file "([^\"]*)" should not contain "([^\"]*)"$/ do |file, partial_content|
233
+ check_file_content(file, partial_content, false)
234
+ end
data/lib/aruba.rb ADDED
@@ -0,0 +1 @@
1
+ require 'aruba/cucumber_steps'