Skip to content

Instantly share code, notes, and snippets.

@experimatt
Last active August 24, 2021 16:59
Show Gist options
  • Save experimatt/51634676c395ab63021e226cd5233ea2 to your computer and use it in GitHub Desktop.
Save experimatt/51634676c395ab63021e226cd5233ea2 to your computer and use it in GitHub Desktop.
Script to generate a list of rails test cases by folder
#!/usr/bin/env ruby
#
# A script to generate a list of tests within a rails project and output it as a csv
#
# Usage: ruby list_rails_tests.rb [output_file_path]
# Author: Matt Decuir
# Last Updated: 8/24/2021
#
# This scrips assumes you're running it from the project root
#
require 'csv'
# grep_output = `cd #{File.expand_path('../../../', __FILE__)} && grep -nr -e 'def test' -e 'Scenario.*:' -e '^\\s*should\\b' -e '^\\s*test.*do\\b' test`
grep_output = `grep -nr -e 'def test' -e 'Scenario.*:' -e '^\\s*should\\b' -e '^\\s*test.*do\\b' test`
column_names = ['Testing Library','Testing Syntax','Directory','File Name','Line Number','Test Name']
output_file = "tests_#{Time.now.strftime('%Y%m%d%H%M')}.csv"
output_directory = ARGV[0] || Dir.pwd + '/'
output_path = File.expand_path("#{output_directory}#{output_file}", __FILE__)
CSV.open(output_path, 'wb') do |csv|
csv << column_names
grep_output.lines.each do |line|
row = line.split(':',3).map(&:strip)
directory = File.dirname row[0]
file_name = File.basename row[0]
line_num = row[1]
test_name = row[2]
# map library and syntax
case test_name
when /def test/
library = 'MiniTest'
syntax = 'def test'
when /Scenario.*:/
library = 'Cucumber'
syntax = 'Scenario'
when /test.*do$/
library = 'MiniTest'
syntax = 'test do'
when /should/
library = 'Shoulda'
syntax = 'should'
else
library = 'Other'
syntax = 'Other'
end
csv << [library, syntax, directory, file_name, line_num, test_name]
end
puts "#{output_file} saved to #{output_directory}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment