Question

I am looking for a ruby code quality checker that could catch or notify if something like a 'debugger' statement gets accidentally committed to ruby code. It would also be ideal if it could look in a rails project and scan any places that may contain ruby code like a haml file.

The idea would be that this would be run with cruisecontrol.rb and against a code base where there are holes in test coverage.

Était-ce utile?

La solution

Don't over-think it! Just write a simple script to search for it over your app's directory.

Using command-line grep:

grep -r --include='*.rb' debugger .

Using ack:

ack --type=ruby debugger

Using Ruby's grep:

Dir['**/*.rb'].each do |path|
  File.open(path, 'r') do |file|
    file.grep /debugger/ do
      puts path
    end
  end
end

(Ruby code lovingly adapted from _why's Wearing Ruby Slippers to Work)

You might also want to consider searching over Javascript files for "debugger" as well, and possibly "console.log", etc.

You may also want to make this be more strict to reduce false positives by using something like /^\s*debugger\s*$/ to only match it when it's the only thing on the line. Adapting this to work for HAML probably requires a bit more: /^\s*[-=]\s*debugger\s*$/.

Autres conseils

Great answer by @Andrew Mashall. For our rails project we use a slightly modified version:

#!/usr/bin/env ruby                                                                                                                                                                                                                          

expressions = [/^\s*binding\.pry/, /^\s*debugger/, /^\s*console\.log/, /^\s*save_and_open_page/]                                                                                                                                             
debuggers = []                                                                                                                                                                                                                               
dirs = `git ls-files |grep '\\(rb\\|haml\\|rake\\|js\\|coffee\\)$'`.split                                                                                                                                                                    
dirs.each do |path|                                                                                                                                                                                                                          
  File.open(path, 'r') do |file|
    contents = file.read
    expressions.each do |expr|                                                                                                                                                                                                               
      contents.match expr do                                                                                                                                                                                                                      
        debuggers << path                                                                                                                                                                                                                    
      end                                                                                                                                                                                                                                    
    end                                                                                                                                                                                                                                      
  end                                                                                                                                                                                                                                        
end                                                                                                                                                                                                                                          
raise "debugger statements found in #{debuggers.to_s}" unless debuggers.empty?

We also added it into our .git/hooks/pre-commit script and to our continuous-integration build to double-check we don't leave debug statements around...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top