Last active
June 2, 2018 22:31
-
-
Save arempe93/e8323c53a0ac3d84a4e8236b6ad96ad5 to your computer and use it in GitHub Desktop.
Adds a spec:lint rake task to detect debugging code in specs, such as logging or rspec tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'English' | |
FOUND_RESULTS = 0 | |
NO_RESULTS = 1 | |
ERROR = 2 | |
DEBUGGING_CODE = [ | |
'"puts "', | |
'" p "', | |
'"focus: true"' | |
].freeze | |
def green(str) | |
"\e[32m#{str}\e[0m" | |
end | |
def red(str) | |
"\e[31m#{str}\e[0m" | |
end | |
namespace :spec do | |
desc 'Lint specs for debugging code' | |
task :lint do | |
puts 'Linting specs...' | |
patterns = "-e #{DEBUGGING_CODE.join(' -e ')}" | |
command = "grep -nr #{patterns} spec/**/**/* --color=always" | |
output = `#{command}` | |
status = $CHILD_STATUS.exitstatus | |
if status == FOUND_RESULTS | |
puts red("\nFound #{output.split("\n").size} errors:") | |
puts output | |
puts "\n" | |
abort red('Done, with errors') | |
elsif status == ERROR | |
puts command | |
puts output | |
puts "\n" | |
abort red('Grep error occurred') | |
end | |
puts green('Done without errors!') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment