Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reek checker plugin to detect ruby code smells #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ bin/rake
Gemfile.lock
coverage/
*.gem
.idea/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions lib/plugins/pre_commit/checks/reek.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pre-commit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = %q{pre-commit}
s.version = "0.17.0"
s.version = "0.18.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please remove the version bump from your PR?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not bump the version here, the core team will maintain version numbers.

edit: I missed the note above :x


s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Shajith Chacko, Josh Lubaway"]
Expand Down
8 changes: 8 additions & 0 deletions test/files/smelly_file.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/files/valid_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions test/unit/plugins/pre_commit/checks/reek_test.rb
Original file line number Diff line number Diff line change
@@ -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