Skip to content

Instantly share code, notes, and snippets.

@tknerr
Created July 18, 2012 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tknerr/3134728 to your computer and use it in GitHub Desktop.
Save tknerr/3134728 to your computer and use it in GitHub Desktop.
ist all available cucumber steps
#
# Based on https://gist.github.com/778535
# In turn based on http://www.natontesting.com/2010/01/11/updated-script-to-list-all-cucumber-step-definitions/
#
desc 'List all defined cucumber steps in features/steps/**/*.rb'
task :steps do
step_definition_dir = "features/steps"
step_files = Dir.glob(File.join(step_definition_dir,'**/*.rb'))
print_steps(step_files)
end
desc 'List all defined cucumber steps in features/**/*.rb, following any required files (might take a while)'
task :allsteps do
features_dir = "features"
step_candidates = Dir.glob(File.join(features_dir,'**/*.rb'))
step_files = deep_find_step_files(step_candidates)
print_steps(step_files)
end
#
# follow all the gem requires, and identify which files have steps in them
#
def deep_find_step_files(step_candidates)
step_files = []
step_candidates.each do |candidate|
begin
File.foreach(candidate) do |line|
if line =~ /require ['"](.*\/.*)['"]/
if libfile = `gem which #{$1} 2>#{ENV['OS'] == 'Windows_NT' ? 'NUL' : '/dev/null'}`.chomp
step_candidates << libfile unless step_candidates.include?(libfile) || libfile =~ /.*\.so$/
end
elsif line =~ /^\s*(?:Given|When|Then)\s+/
step_files << candidate
end
end
rescue
puts "--- skipped #{candidate} due to error"
end
end
step_files
end
#
# find the Given/When/Thens in the given step_files and print the results to stdout
#
def print_steps(step_files)
require 'hirb'
extend Hirb::Console
step_files.uniq.each do |step_file|
puts "File: #{step_file}"
results = []
File.new(step_file).read.each_line.each_with_index do |line, number|
next unless line =~ /^\s*(?:Given|When|Then)\s+|\//
res = /(?:Given|When|Then)[\s\(]*\/(.*)\/([imxo]*)[\s\)]*do\s*(?:$|\|(.*)\|)/.match(line)
next unless res
matches = res.captures
results << OpenStruct.new(
:steps => matches[0],
:modifier => matches[1],
:args => matches[2]
)
end
table results, :resize => false, :fields=>[:steps, :modifier, :args]
puts ""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment