require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
namespace :cc do
desc "Run features in a style suitable for Continuous Integration"
task :ci do |t|
def feature_dir(dir, &block)
puts("=" * 80)
puts("feature_dir(#{dir.inspect})")
puts("=" * 80)
system(%Q{echo "
#{File.basename(dir)}
" | tee -a #{@index_html}})
block.call
system(%Q{echo "
" | tee -a #{@index_html}})
end
def run_feature(feature_file)
puts("=" * 80)
puts("run_feature(#{feature_file.inspect})")
puts("=" * 80)
filename = feature_file.gsub(/(\w*).feature/, '\1.html')
puts("#{feature_file} -> public/#{filename}")
system(%Q{mkdir -pv #{File.dirname("public/#{filename}")}})
push = (@pushed ? nil : %Q{SETUP="YES"})
output_file = File.join(@output_dir, filename)
command = [push, "bundle exec cucumber", "features/support", feature_file, ENV['EXTRA_CUCUMBER_ARGS'], "--format html", "--out", output_file].flatten.compact.join(" ")
@pushed = true if !@pushed
puts("command=#{command.inspect}")
system(command)
@exit_codes << $?.exitstatus
passed = "PASSED"
failed = "FAILED"
status = ((@exit_codes[-1] == 0) ? passed : failed)
system(%Q{echo "#{File.basename(filename.gsub('.html', ''))} (#{status})" | tee -a #{@index_html}})
end
@output_dir = File.join(Dir.pwd, "public")
@index_html = File.join(@output_dir, "index.html")
@pushed = false
@exit_codes = Array.new
puts("Cleaning up...")
system(%Q{rm -rfv .cucumber-chef/aws/artifacts})
system(%Q{rm -rfv public})
system(%Q{mkdir -v public})
puts("Generating Cucumber-Chef html reports...")
system(%Q{echo "Cucumber-Chef Report" | tee public/index.html})
system(%Q{echo "Cucumber-Chef Report
" | tee -a public/index.html})
Dir.glob("features/*").each do |fdir|
if File.directory?(fdir)
if (ffiles = Dir.glob("#{fdir}/*.feature")).count > 0
feature_dir(fdir) do
ffiles.each do |ffile|
run_feature(ffile)
end
end
end
else
run_feature(fdir)
end
end
system(%Q{echo "
" | tee -a public/index.html})
# If we had a failure be sure to surface it
exit_code = (@exit_codes.any?{ |ec| ec != 0 } ? 1 : 0)
exit(exit_code)
end
Cucumber::Rake::Task.new(:cucumber) do |t|
feature_dirs = Array.new
feature_dir_glob = File.join(Dir.pwd, "*cookbook*", "*", "*feature*")
feature_dirs << Dir.glob(feature_dir_glob)
feature_dir_glob = File.join(Dir.pwd, "*feature*")
feature_dirs << Dir.glob(feature_dir_glob)
opts = [
"--exclude support/cookbooks",
"--exclude support/data_bags",
"--exclude support/environments",
"--exclude support/keys",
"--exclude support/roles",
feature_dirs
].flatten.compact.join(" ")
t.cucumber_opts = opts
end
end