Skip to content

Commit db4a26d

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Split Whitespace check into HardTabs and TrailingWhitespace
This will make the check more specific, and give users greater control over which checks they can disable. Change-Id: I4b9353e26f8dcf8b4c07376aaf3c86a8f33932e1 Reviewed-on: http://gerrit.causes.com/35606 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent a909657 commit db4a26d

File tree

6 files changed

+73
-27
lines changed

6 files changed

+73
-27
lines changed

config/default.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ PreCommit:
5858
description: 'Analyzing with haml-lint'
5959
include: '**/*.haml'
6060

61+
HardTabs:
62+
description: 'Checking for hard tabs'
63+
6164
ImageOptim:
6265
description: 'Checking for optimizable images'
6366
include:
@@ -87,8 +90,8 @@ PreCommit:
8790
description: 'Analyzing with scss-lint'
8891
include: '**/*.scss'
8992

90-
Whitespace:
91-
description: 'Checking for invalid whitespace'
93+
TrailingWhitespace:
94+
description: 'Checking for trailing whitespace'
9295

9396
YamlSyntax:
9497
description: 'Checking YAML syntax'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Overcommit::Hook::PreCommit
2+
# Checks for hard tabs in files.
3+
class HardTabs < Base
4+
def run
5+
paths = applicable_files.join(' ')
6+
7+
# Catches hard tabs
8+
result = command("grep -IHn \"\\t\" #{paths}")
9+
unless result.stdout.empty?
10+
return :bad, "Hard tabs detected:\n#{result.stdout}"
11+
end
12+
13+
:good
14+
end
15+
end
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Overcommit::Hook::PreCommit
2+
# Checks for trailing whitespace in files.
3+
class TrailingWhitespace < Base
4+
def run
5+
paths = applicable_files.join(' ')
6+
7+
result = command("grep -IHn \"\\s$\" #{paths}")
8+
unless result.stdout.empty?
9+
return :bad, "Trailing whitespace detected:\n#{result.stdout}"
10+
end
11+
12+
:good
13+
end
14+
end
15+
end

lib/overcommit/hook/pre_commit/whitespace.rb

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::HardTabs 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 hard tabs' do
22+
let(:contents) { "Some\thard\ttabs" }
23+
24+
it { should fail_check }
25+
end
26+
27+
context 'when file has no hard tabs' do
28+
let(:contents) { 'Just some text' }
29+
30+
it { should pass }
31+
end
32+
end

spec/overcommit/hook/pre_commit/whitespace_spec.rb spec/overcommit/hook/pre_commit/trailing_whitespace_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe Overcommit::Hook::PreCommit::Whitespace do
3+
describe Overcommit::Hook::PreCommit::TrailingWhitespace do
44
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
55
let(:context) { double('context') }
66
subject { described_class.new(config, context) }
@@ -18,14 +18,14 @@
1818
end
1919
end
2020

21-
context 'when file contains hard tabs' do
22-
let(:contents) { "Some\thard\ttabs" }
21+
context 'when file contains trailing whitespace' do
22+
let(:contents) { 'Some trailing whitespace ' }
2323

2424
it { should fail_check }
2525
end
2626

27-
context 'when file contains trailing whitespace' do
28-
let(:contents) { 'Some trailing whitespace ' }
27+
context 'when file contains trailing tabs' do
28+
let(:contents) { "Some trailing tabs\t\t" }
2929

3030
it { should fail_check }
3131
end

0 commit comments

Comments
 (0)