Skip to content

Instantly share code, notes, and snippets.

@arempe93
Last active June 2, 2018 22:31
Show Gist options
  • Save arempe93/e8323c53a0ac3d84a4e8236b6ad96ad5 to your computer and use it in GitHub Desktop.
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
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