Skip to content

Commit 9c1d8fa

Browse files
ajgonsds
authored andcommitted
Add pre commit hook for FixMe
1 parent d4b90b9 commit 9c1d8fa

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master
44

5+
* Add `FixMe` pre-commit hook, to ensure that no "token" words slips through.
6+
These strings are things you should fix now, not later.
57
* Add [`YAMLLint`](https://github.com/adrienverge/yamllint) pre-commit hook
68
* Add `LicenceHeader` pre-commit enforcement to ensure open source projects
79
contain proper licence comments.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
487487
* [EsLint](lib/overcommit/hook/pre_commit/es_lint.rb)
488488
* [ExecutePermissions](lib/overcommit/hook/pre_commit/execute_permissions.rb)
489489
* [Fasterer](lib/overcommit/hook/pre_commit/fasterer.rb)
490+
* [FixMe](lib/overcommit/hook/pre_commit/fix_me.rb)
490491
* [Foodcritic](lib/overcommit/hook/pre_commit/foodcritic.rb)
491492
* [ForbiddenBranches](lib/overcommit/hook/pre_commit/forbidden_branches.rb)
492493
* [GoLint](lib/overcommit/hook/pre_commit/go_lint.rb)

config/default.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ PreCommit:
254254
install_command: 'gem install fasterer'
255255
include: '**/*.rb'
256256

257+
FixMe:
258+
enabled: false
259+
description: 'Check for "token" strings'
260+
required_executable: 'grep'
261+
flags: ['-IEHnw']
262+
keywords: ['BROKEN', 'BUG', 'ERROR', 'FIXME', 'HACK', 'NOTE', 'OPTIMIZE', 'REVIEW', 'TODO', 'WTF', 'XXX']
263+
257264
Foodcritic:
258265
enabled: false
259266
description: 'Analyze with Foodcritic'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Overcommit::Hook::PreCommit
2+
# Check for "token" strings
3+
class FixMe < Base
4+
def run
5+
keywords = config['keywords']
6+
result = execute(command, args: [keywords.join('|')] + applicable_files)
7+
8+
extract_messages(
9+
result.stdout.split("\n"),
10+
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+)/,
11+
lambda { |_type| :warning }
12+
)
13+
end
14+
end
15+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::FixMe do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
let(:staged_file) { 'filename.txt' }
8+
9+
before do
10+
subject.stub(:applicable_files).and_return([staged_file])
11+
end
12+
13+
around do |example|
14+
repo do
15+
File.open(staged_file, 'w') { |f| f.write(contents) }
16+
`git add #{staged_file}`
17+
example.run
18+
end
19+
end
20+
21+
context 'when file contains FIXME' do
22+
let(:contents) { 'eval(params[:q]) # FIXME maybe this is a bad idea?' }
23+
24+
it { should warn }
25+
end
26+
27+
context 'when file contains TODO with special chars around it' do
28+
let(:contents) { 'users = (1..1000).map { |i| User.find(1) } #TODO: make it better' }
29+
30+
it { should warn }
31+
end
32+
33+
context 'when file does not contain any FixMe words' do
34+
let(:contents) { 'if HACKY_CONSTANT.blank?' }
35+
36+
it { should pass }
37+
end
38+
end

0 commit comments

Comments
 (0)