Skip to content

Commit 3ea3fcb

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Begin adding specs for pre-commit hooks
Change-Id: I6ff20a406ae961b952ce91ca5bf6faed61e3b549 Reviewed-on: http://gerrit.causes.com/35563 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent 9bc2914 commit 3ea3fcb

26 files changed

+504
-6076
lines changed

TODO

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Allow SKIP arguments to be case insensitive
1818
Move ReleaseNote check into Causes repo
1919
Enable hook by simply including it in repo-specific .overcommit.yml (no need
2020
to specify enabled: true)
21+
Display when a hook has been explicitly skipped when it would otherwise run
22+
Display when no applicable hooks ran (e.g. instead of "All pre-commit checks passed")
2123

2224

2325
Allow features of hooks to be customized (stealth, required, etc.)

lib/overcommit/configuration.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def plugin_directory
1313
File.join(Overcommit::Utils.repo_root, @hash['plugin_directory'])
1414
end
1515

16-
# Returns the hooks that have been enabled for a hook type.
17-
def enabled_hooks(hook_type)
16+
# Returns the built-in hooks that have been enabled for a hook type.
17+
def enabled_builtin_hooks(hook_type)
1818
@hash[hook_type].keys.
1919
select { |hook_name| hook_name != 'ALL' }.
2020
select { |hook_name| @hash[hook_type][hook_name]['enabled'] != false }

lib/overcommit/configuration_loader.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def self.load_repo_config
1616
end
1717
end
1818

19+
def self.default_configuration
20+
@default_config ||= load_from_file(DEFAULT_CONFIG_PATH)
21+
end
22+
1923
private
2024

2125
# Loads a configuration, ensuring it extends the default configuration.
@@ -39,9 +43,5 @@ def self.load_from_file(file)
3943

4044
Overcommit::Configuration.new(hash)
4145
end
42-
43-
def self.default_configuration
44-
@default_config ||= load_from_file(DEFAULT_CONFIG_PATH)
45-
end
4646
end
4747
end

lib/overcommit/hook/post_checkout/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Overcommit::Hook::PostCheckout
55
class Base < Overcommit::Hook::Base
66
extend Forwardable
77

8-
def_delegator :@context,
9-
*%i[previous_head new_head branch_checkout? file_checkout?]
8+
def_delegators :@context,
9+
:previous_head, :new_head, :branch_checkout?, :file_checkout?
1010
end
1111
end

lib/overcommit/hook/pre_commit/coffee_lint.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def run
77
end
88

99
result = command("coffeelint --quiet #{applicable_files.join(' ')}")
10-
11-
return (result.success? ? :good : :bad), result.stdout
10+
return :good if result.success?
11+
return :bad, result.stdout
1212
end
1313
end
1414
end

lib/overcommit/hook/pre_commit/css_lint.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module Overcommit::Hook::PreCommit
22
# Runs `csslint` against any modified CSS files.
33
class CssLint < Base
4-
CSS_LINT = Overcommit::Utils.script_path('csslint-rhino.js')
5-
64
def run
7-
return :warn, 'Rhino is not installed' unless in_path?('rhino')
5+
unless in_path?('csslint')
6+
return :warn, 'csslint not installed -- run `npm install -g csslint`'
7+
end
88

99
paths = applicable_files.join(' ')
1010

11-
result = command("rhino #{CSS_LINT} --quiet --format=compact #{paths} | grep 'Error - '")
11+
result = command("csslint --quiet --format=compact #{paths} | grep 'Error - '")
1212
output = result.stdout
1313
return (output !~ /Error - (?!Unknown @ rule)/ ? :good : :bad), output
1414
end

lib/overcommit/hook/pre_commit/js_hint.rb

+4-17
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,14 @@ module Overcommit::Hook::PreCommit
22
# Runs `jshint` against any modified JavaScript files.
33
class JsHint < Base
44
def run
5-
return :warn, 'Need either `jshint` or `rhino` in path' unless runner
5+
unless in_path?('jshint')
6+
return :warn, 'jshint not installed -- run `npm install -g jshint`'
7+
end
68

7-
result = runner.call(applicable_files.join(' '))
9+
result = command("jshint #{applicable_files.join(' ')}")
810
output = result.stdout
911

1012
return (output.empty? ? :good : :bad), output
1113
end
12-
13-
private
14-
15-
JS_HINT_PATH = Overcommit::Utils.script_path 'jshint.js'
16-
JS_HINT_RUNNER_PATH = Overcommit::Utils.script_path 'jshint_runner.js'
17-
18-
def runner
19-
if in_path?('jshint')
20-
lambda { |paths| command("jshint #{paths}") }
21-
elsif in_path?('rhino')
22-
lambda do |paths|
23-
command("rhino -strict -f #{JS_HINT_PATH} #{JS_HINT_RUNNER_PATH} #{paths} 2>&1 | grep -v warning | grep -v -e '^js: '")
24-
end
25-
end
26-
end
2714
end
2815
end

lib/overcommit/hook/pre_commit/python_flake8.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module Overcommit::Hook::PreCommit
22
# Runs `flake8` against any modified Python files.
33
class PythonFlake8 < Base
44
def run
5-
return :warn, 'Run `pip install flake8`' unless in_path?('flake8')
5+
unless in_path?('flake8')
6+
return :warn, 'flake8 not installed -- run `pip install flake8`'
7+
end
68

79
result = command("flake8 #{applicable_files.join(' ')}")
810

lib/overcommit/hook/pre_commit/whitespace.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Overcommit::Hook::PreCommit
2-
# Checks for invalid whitespace (hard tabs or trailig whitespace) in files.
2+
# Checks for invalid whitespace (hard tabs or trailing whitespace) in files.
33
class Whitespace < Base
44
def run
55
paths = applicable_files.join(' ')

lib/overcommit/hook_runner.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def load_hooks
5454
# Load hooks that ship with Overcommit, ignoring ones that are excluded from
5555
# the repository's configuration.
5656
def load_builtin_hooks
57-
@config.enabled_hooks(underscored_hook_type).each do |hook_name|
57+
@config.enabled_builtin_hooks(underscored_hook_type).each do |hook_name|
5858
underscored_hook_name = Overcommit::Utils.underscorize(hook_name)
5959
require "overcommit/hook/#{underscored_hook_type}/#{underscored_hook_name}"
6060
@hooks << create_hook(hook_name)

0 commit comments

Comments
 (0)