Skip to content

Instantly share code, notes, and snippets.

@s-mage
Created February 8, 2014 07:30
Show Gist options
  • Save s-mage/8877983 to your computer and use it in GitHub Desktop.
Save s-mage/8877983 to your computer and use it in GitHub Desktop.
Find test in cucumber by tag.
TAG_STRING = /(@\w+\W?)+/
EMPTY_STRING = /^\W*$/
def scan_project(dirname, tag)
Dir.new(dirname).
select { |file| File.extname(file) == '.feature' }.
inject('') { |r, f| r << format_file(f, scan_file(f, tag)) }
end
def format_file(filename, scan_result)
scan_result.empty? ? '' : ("\n" << filename << "\n" << scan_result)
end
def scan_file(filename, tag)
return("\n" << File.read(filename)) if tagged_feature?(filename, tag)
status = :skip
IO.readlines(filename).inject('') do |result, line|
case line
when TAG_STRING
status, processed_line = *process_tag_line(line, tag)
result << processed_line
when EMPTY_STRING
status = :skip
result
else
result << process_regular_line(line, status)
end
end
end
def tagged_feature?(filename, tag)
has_tag? File.open(filename, &:readline), tag
end
def process_tag_line(line, tag)
has_tag?(line, tag) ? [:read, "\n" << line] : [:skip, '']
end
def has_tag?(line, tag)
line.split(' ').include?('@' + tag)
end
def process_regular_line(line, status)
status == :skip ? '' : line
end
dirname, tag = *ARGV
Dir.chdir dirname
puts scan_project(dirname, tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment