Static code analysis is a method of computer program debugging that is done by examining the code without executing the program. The process provides an understanding of the code structure, and can help to ensure that the code adheres to industry standards. Automated tools can assist programmers and developers in carrying out static analysis. The process of scrutinizing code by visual inspection alone (by looking at a printout, for example), without the assistance of automated tools, is sometimes called program understanding or program comprehension.
A Ruby static code analyzer, based on the community Ruby style guide.
RuboCop is a Ruby static code analyzer. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide.
Most aspects of its behavior can be tweaked via various configuration options.
Apart from reporting problems in your code, RuboCop can also automatically fix some of the problems for you.
$ gem install rubocop
Bash
or using bundler
gem 'rubocop', require: false
Ruby
$ cd my/cool/ruby/project $ rubocop
Bash
HTML Formatter
rubocop app -R --format html -o result.html
Bash
JSON Formatter
rubocop app -R --format json -o result.json
Bash
{ "metadata": { "rubocop_version": "0.9.0", "ruby_engine": "ruby", "ruby_version": "2.0.0", "ruby_patchlevel": "195", "ruby_platform": "x86_64-darwin12.3.0" }, "files": [{ "path": "lib/foo.rb", "offenses": [] }, { "path": "lib/bar.rb", "offenses": [{ "severity": "convention", "message": "Line is too long. [81/80]", "cop_name": "LineLength", "corrected": true, "location": { "line": 546, "column": 80, "length": 4 } }, { "severity": "warning", "message": "Unreachable code detected.", "cop_name": "UnreachableCode", "corrected": false, "location": { "line": 15, "column": 9, "length": 10 } } ] } ], "summary": { "offense_count": 2, "target_file_count": 2, "inspected_file_count": 2 } }
JSON
A static analysis security vulnerability scanner for Ruby on Rails applications.
Brakeman is an open source static analysis tool which checks Ruby on Rails applications for security vulnerabilities.
Check out Brakeman Pro if you are looking for a commercially-supported version with a GUI and advanced features.
gem install brakeman
Bash
or using bundler:
group :development do gem 'brakeman', :require => false end
Ruby
brakeman
Bash
The output format is determined by the file extension or by using the -f option. Current options are: text, html, tabs, json, markdown, csv, and codeclimate
brakeman -o output.html -o output.json
Bash
Code smell detector for Ruby
gem install reek
Bash
reek [options] [dir_or_source_file]*
Bash
# Smelly class class Smelly # This will reek of UncommunicativeMethodName def x y = 10 # This will reek of UncommunicativeVariableName end end
Ruby
$ reek demo.rb Inspecting 1 file(s): S demo.rb -- 2 warnings: [4]:UncommunicativeMethodName: Smelly#x has the name 'x' [https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Method-Name.md] [5]:UncommunicativeVariableName: Smelly#x has the variable name 'y' [https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Variable-Name.md]
Bash
reek app --format html > report.html
Bash
Code coverage for Ruby 1.9+ with a powerful configuration library and automatic merging of coverage across test suites
SimpleCov is a code coverage analysis tool for Ruby. It uses Ruby’s built-in Coverage library to gather code coverage data, but makes processing its results much easier by providing a clean API to filter, group, merge, format, and display those results, giving you a complete code coverage suite that can be set up with just a couple lines of code.
Add SimpleCov to your Gemfile
and bundle install
:
gem 'simplecov', :require => false, :group => :test
Ruby
Load and launch SimpleCov at the very top of your test/test_helper.rb
(or spec_helper.rb
, cucumber env.rb
, or whatever your preferred test
framework uses):
if ENV['RAILS_ENV'] == 'test' require 'simplecov' SimpleCov.start 'rails' puts "required simplecov" end
Ruby
Run your tests, open up coverage/index.html
in your browser and check out
what you’ve missed so far.
Add the following to your .gitignore
file to ensure that coverage results
are not tracked by Git (optional):
coverage
Coverage results report, fully browsable locally with sorting and much more:
Source file coverage details view:
Overcommit is a gem for configuring Git hooks. It is excellent for keeping the code quality high. It allows tuning git hooks for linters launch before every commit.
Rubocop
?Fasterer
?Simplecov
?Overcommit
?