From edca04d24c5c87bfe5bf4b11b43a2ff6d4dd9307 Mon Sep 17 00:00:00 2001 From: Leons Petrazickis Date: Thu, 21 Aug 2014 12:39:05 -0400 Subject: [PATCH] Reek checker plugin to detect ruby code smells --- .gitignore | 1 + README.md | 1 + lib/plugins/pre_commit/checks/reek.rb | 33 +++++++++++++++++++ pre-commit.gemspec | 2 +- test/files/smelly_file.rb | 8 +++++ test/files/valid_file.rb | 4 +++ .../plugins/pre_commit/checks/reek_test.rb | 26 +++++++++++++++ 7 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/plugins/pre_commit/checks/reek.rb create mode 100644 test/files/smelly_file.rb create mode 100644 test/unit/plugins/pre_commit/checks/reek_test.rb diff --git a/.gitignore b/.gitignore index a0ac279..b47ba77 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ bin/rake Gemfile.lock coverage/ *.gem +.idea/ diff --git a/README.md b/README.md index 91c4f82..45ddce0 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ These are the available checks: * scss_lint (Check your SCSS files using the [scss-lint gem](https://github.com/causes/scss-lint)) * yaml (Check that your YAML is parsable) * json (Checks if JSON is parsable) +* reek (Checks ruby code for code smells using the [reek gem.](https://github.com/troessner/reek)) ## Default checks diff --git a/lib/plugins/pre_commit/checks/reek.rb b/lib/plugins/pre_commit/checks/reek.rb new file mode 100644 index 0000000..1266bbe --- /dev/null +++ b/lib/plugins/pre_commit/checks/reek.rb @@ -0,0 +1,33 @@ +# encoding: utf-8 + +require 'pre-commit/checks/shell' + +# Plugins for pre-commit +module PreCommit + # Checking plugins for pre-commit + module Checks + # Runs reek to detect ruby code smells + class Reek < Shell + def call(staged_files) + staged_files = staged_files.grep(/\.rb/) + return if staged_files.empty? + + args = (config_file_flag + staged_files).join(' ') + + execute("reek #{args}") + end + + def config_file_flag + config_file ? ['-c', config_file] : [] + end + + def alternate_config_file + 'config/.reek' + end + + def self.description + "Runs reek to detect ruby code smells" + end + end + end +end diff --git a/pre-commit.gemspec b/pre-commit.gemspec index 0e1f84f..31fe5c3 100644 --- a/pre-commit.gemspec +++ b/pre-commit.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = %q{pre-commit} - s.version = "0.17.0" + s.version = "0.18.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Shajith Chacko, Josh Lubaway"] diff --git a/test/files/smelly_file.rb b/test/files/smelly_file.rb new file mode 100644 index 0000000..fbdd7d6 --- /dev/null +++ b/test/files/smelly_file.rb @@ -0,0 +1,8 @@ +# encoding: utf-8 + +# class docs for rubocop +class ValidFile + # Comments with the word: debugger should be allowed + def no_problems_here! + end +end diff --git a/test/files/valid_file.rb b/test/files/valid_file.rb index fbdd7d6..ec270bd 100644 --- a/test/files/valid_file.rb +++ b/test/files/valid_file.rb @@ -5,4 +5,8 @@ class ValidFile # Comments with the word: debugger should be allowed def no_problems_here! end + + # Need a safe version of method! to avoid PrimaDonnaMethod code smell in reek + def no_problems_here + end end diff --git a/test/unit/plugins/pre_commit/checks/reek_test.rb b/test/unit/plugins/pre_commit/checks/reek_test.rb new file mode 100644 index 0000000..92deb01 --- /dev/null +++ b/test/unit/plugins/pre_commit/checks/reek_test.rb @@ -0,0 +1,26 @@ +# encoding: utf-8 + +require 'minitest_helper' +require 'plugins/pre_commit/checks/reek' + +describe PreCommit::Checks::Reek do + let(:config) do + mock = MiniTest::Mock.new + mock.expect(:get, '', ['reek.config']) + mock + end + + let(:check) { PreCommit::Checks::Reek.new(nil, config, []) } + + it "succeeds if nothing changed" do + check.call([]).must_equal nil + end + + it "succeeds if only good changes" do + check.call([fixture_file('valid_file.rb')]).must_equal nil + end + + it "fails if file is smelly" do + check.call([fixture_file('smelly_file.rb')]).must_match(/1 warning/) + end +end