Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JuPlutonic/3588f1ae731b6738e4561d6a5be298f9 to your computer and use it in GitHub Desktop.
Save JuPlutonic/3588f1ae731b6738e4561d6a5be298f9 to your computer and use it in GitHub Desktop.
Remove cucumber unused steps
@steps_path = ENV['STEPS_PATH']
@features_path = ENV['FEATURES_PATH']
@steps_array = []
@unused_steps = []
def steps_files(path)
Dir[path + '**/*.rb']
end
def features_files(path)
Dir[path + '**/*.feature']
end
def is_step?(line)
line.match(/^(.+)$/)
end
def extract_step(line)
if line.include?('step(%')
line[/^step\(%\((.+)\)$/, 1]
else
line[%r{\/\^(.*)\$\/}, 1]
end
end
def regexify_string(file_path, received_line, received_scanned_line)
# lines = []
# variants = File.read(file_path).split('|')
# variants.each do |variant|
# lines << line.gsub(/<.*>/, variant.strip) unless variant.strip.length > 30
# end
# lines
lines = []
field_index = 0
field = received_line[/<(.*)>/, 1]
search_started = false
example_started = false
variants_started = false
first_example_line = true
File.foreach(file_path) do |line|
search_started = true if received_line == line
if search_started && line.include?('Examples:')
example_started = true
next
end
if example_started && first_example_line
fields = line.split('|').collect { |x| x.strip || x }
field_index = fields.find_index(field)
first_example_line = false
variants_started = true
next
end
if example_started && variants_started
break if line.split('|').length == 1
field_index = 0 if field_index.nil?
# p received_line
# p line
# p field_index
lines << received_line.gsub(/<.*>/, line.split('|')[field_index].strip)
end
end
# p lines
lines
end
def all_cucumber_steps
steps_files(@steps_path).each do |file_path|
File.foreach(file_path) do |line|
@steps_array << extract_step(line) if extract_step(line)
end
end
@steps_array
end
def print_results
features_unused_steps = []
lines = []
scanned_line = 0
replace_regex = Regexp.new '<.*>'
all_cucumber_steps.each do |step|
usage_found = false
features_files(@features_path).each do |file_path|
File.foreach(file_path) do |line|
scanned_line += 1
lines = regexify_string(file_path, line, scanned_line) if line.scan(replace_regex).length >= 1 && line.split('<').length == 2
if lines
lines.each do |possible_line|
regex = Regexp.new step
result = possible_line.scan(regex)
usage_found = true if result.length >= 1
break if usage_found
end
end
regex = Regexp.new step
result = line.scan(regex)
usage_found = true if result.length >= 1
break if usage_found
end
break if usage_found
end
next if usage_found
p "'#{step}' usage was not found in feature files"
features_unused_steps << step
end
features_unused_steps.each do |step|
usage_found = false
steps_files(@steps_path).each do |file_path|
File.foreach(file_path) do |line|
if line.include?('%(')
regex = Regexp.new step
result = line.scan(regex)
usage_found = true if result.length >= 1
end
break if usage_found
end
break if usage_found
end
if usage_found
p "'#{step}' usage was found in cucumber steps"
next
end
@unused_steps << step
end
p @unused_steps.count
end
def unused_steps_lines
p 'getting steps lines to delete'
# p @unused_steps
lines_to_delete = []
steps_files(@steps_path).each do |file_path|
scanned_line = 0
begin_deleted_line = 0
end_deleted_line = 0
scan_for_end = false
file_lines = []
File.foreach(file_path) do |line|
scanned_line += 1
if scan_for_end
# p 'scan for end registered'
# p line
if line == "end\n"
end_deleted_line = scanned_line
scan_for_end = false
end
elsif @unused_steps.include? extract_step(line)
# p true.to_s
begin_deleted_line = scanned_line
scan_for_end = true
end
# p 'b: ' + begin_deleted_line.to_s
# p 'e: ' + end_deleted_line.to_s
if begin_deleted_line.positive? && end_deleted_line.positive?
end_deleted_line += 1 unless end_deleted_line == File.read(file_path).scan(/\n/).count
lines = [
begin_deleted_line,
end_deleted_line
]
file_lines << lines
begin_deleted_line = 0
end_deleted_line = 0
end
end
lines_to_delete << [
file_path,
file_lines
]
end
lines_to_delete
end
def delete_lines(lines_to_delete)
p 'deleting got lines'
# p lines_to_delete
lines_to_delete.each do |file_block|
file_lines = ''
file_path = file_block[0]
line_n = 0
numbers_array = []
File.foreach(file_path) do |line|
lines_array = file_block[1]
lines_array.each do |begin_end_line_array|
begin_line = begin_end_line_array[0]
end_line = begin_end_line_array[1]
(end_line - begin_line + 1).times do
numbers_array << begin_line
begin_line += 1
end
end
line_n += 1
file_lines += line unless numbers_array.include? line_n
end
File.open(file_path, 'w') do |file|
file.puts file_lines
end
end
end
print_results
delete_lines(unused_steps_lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment