require 'nokogiri' Given /^a Ruby application$/ do ruby_app_root.should_not be_nil end Given /^a Gemfile$/ do gemfile = ruby_app_path('Gemfile') unless File.exist?(gemfile) basedir = File.expand_path('../../..', __FILE__) File.open(gemfile, 'w') do |file| file.puts "gem 'right_support', :path=>'#{basedir}'" end end end Given /^a gem dependency on '(.*)'$/ do |dependency| step 'a Gemfile' gem, version = dependency.split(/\s+/, 2) gemfile = ruby_app_path('Gemfile') File.open(gemfile, 'a') do |file| file.puts "gem '#{gem}', '#{version}'" end end Given /^a Rakefile$/ do rakefile = ruby_app_path('Rakefile') unless File.exist?(rakefile) File.open(rakefile, 'w') do |file| file.puts "# Auto-generated by #{__FILE__}" end end end Given /^the Rakefile contains a RightSupport::CI::RakeTask$/ do step 'a Rakefile' rakefile = ruby_app_path('Rakefile') File.open(rakefile, 'w') do |file| file.puts "require 'right_support/ci/rake_task'" file.puts "RightSupport::CI::RakeTask.new" end end Given /^the Rakefile contains a RightSupport::CI::RakeTask with parameter '(.*)'$/ do |ns| step 'a Rakefile' rakefile = ruby_app_path('Rakefile') File.open(rakefile, 'w') do |file| file.puts "require 'right_support/ci/rake_task'" file.puts "RightSupport::CI::RakeTask.new(#{ns})" end end Given /^a trivial (failing )?RSpec spec$/ do |failing| spec_dir = ruby_app_path('spec') spec = ruby_app_path('spec', 'trivial_spec.rb') FileUtils.mkdir_p(spec_dir) unless File.exist?(spec) File.open(spec, 'w') do |file| file.puts "describe String do" file.puts " it 'has a size' do" file.puts " 'joe'.size.should == 3" file.puts " end" file.puts file.puts " it 'is stringy' do" file.puts " pending" file.puts " end" unless failing.nil? file.puts file.puts "it 'meets an impossible ideal' do" file.puts " raise NotImplementedError, 'inconceivable!'" file.puts "end" end file.puts "end" end end end Given /^a trivial (failing )?Cucumber feature$/ do |failing| features_dir = ruby_app_path('features') steps_dir = ruby_app_path('features', 'step_definitions') feature = ruby_app_path('features', 'trivial.feature') steps = ruby_app_path('features', 'step_definitions', 'trivial_steps.rb') FileUtils.mkdir_p(features_dir) FileUtils.mkdir_p(steps_dir) unless File.exist?(steps) File.open(steps, 'w') do |file| file.puts "When /^the night has come and the land is dark$/ do; end" file.puts "When /^the moon is the only light we see$/ do; end" file.puts "Then /^I won't be afraid.*$/ do; end" file.puts "Then /^as long as you stand.*by me$/ do; end" file.puts "Then /^you run away as fast as you can$/ do; raise NotImplementedError; end" end end unless File.exist?(feature) File.open(feature, 'w') do |file| file.puts "Feature: Song Lyrics from the 1950s" file.puts file.puts " Scenario: Stand By Me" file.puts " When the night has come and the land is dark" file.puts " And the moon is the only light we see" file.puts " Then I won't be afraid, oh, I won't be afraid" if failing.nil? file.puts " And as long as you stand, stand by me" else file.puts " And you run away as fast as you can" end end end end When /^I install the bundle$/ do ruby_app_shell('bundle install') end When /^I rake '(.*)'$/ do |task| @ruby_app_output = ruby_app_shell("bundle exec rake #{task} --trace ", :ignore_errors => true) end When /^I debug the app shell$/ do STDOUT.puts "Opening shell in a separate window." if RUBY_PLATFORM =~ /darwin/ ruby_app_shell("open -a Terminal .") else raise "Don't know how to open an app shell for #{RUBY_PLATFORM}; please contribute your knowledge to #{__FILE__}" end STDOUT.puts "Press Enter to continue Cucumber execution..." STDOUT.flush STDIN.readline end Then /^the output should contain '(.*)'$/ do |expected_output| @ruby_app_output.should include(expected_output) end Then /^the output should not contain '(.*)'$/ do |expected_output| @ruby_app_output.should_not include(expected_output) end Then /^the directory '(.*)' should contain files/ do |dir| dir = ruby_app_path(dir) File.directory?(dir).should be_true Dir[File.join(dir, '*')].should_not be_empty end Then /^the file '(.*)' should mention ([0-9]) (passing|failing) test cases?$/ do |file, n, pass_fail| file = ruby_app_path(file) n = Integer(n) Pathname.new(file).should exist doc = Nokogiri.XML(File.open(file, 'r')) all_testcases = doc.css('testcase').size failing_testcases = doc.css('testcase failure').size passing_testcases = all_testcases - failing_testcases case pass_fail when 'passing' passing_testcases.should == n when 'failing' failing_testcases.should == n else raise NotImplementedError, "WTF #{pass_fail}" end end Then /^the command should (succeed|fail)$/ do |success| if success == 'succeed' $?.exitstatus.should == 0 elsif success == 'fail' $?.exitstatus.should_not == 0 else raise NotImplementedError, "Unknown expectation #{success}" end end