Ruby quality check code that the debugger could capture expression

I'm looking for a ruby ​​code quality checker tool that could catch or notify if something like a 'debugger' assertion accidentally ends up in ruby ​​code. It would also be ideal if he could look in the rails project and scan for any places that might contain ruby ​​code like the haml file.

The idea would be that this would be done with cruisecontrol.rb and against a codebase where there are holes in the testing coverage.

+3


source to share


2 answers


Don't change your mind! Just write a simple script to find it in your application directory.

Using the command line grep

:

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

      

Usage ack

:

ack --type=ruby debugger

      



Using Ruby grep

:

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

      

(Ruby code is lovingly adapted from _why Wearing ruby ​​slippers to work )

You might also consider looking for Javascript files for "debugger"

, and possibly, "console.log"

etc.

You can also make it stricter to reduce false positives by using something like /^\s*debugger\s*$/

just to match when it's the only thing on the line. Adapting this to work for HAML, probably needs a little more: /^\s*[-=]\s*debugger\s*$/

.

+5


source


Great answer from @Andrew Mashall. For our rails project, we are using 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 to our .git/hooks/pre-commit

script and to our continuous integration build to double check that we are not leaving debug assertions around ...

+2


source







All Articles