File tree 3 files changed +89
-0
lines changed
lib/overcommit/hook/pre_commit
spec/overcommit/hook/pre_commit
3 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -212,6 +212,13 @@ PreCommit:
212
212
- ' **/*.rb'
213
213
- ' **/*.rake'
214
214
215
+ Pyflakes :
216
+ enabled : false
217
+ description : ' Analyzing with pyflakes'
218
+ required_executable : ' pyflakes'
219
+ install_command : ' pip install pyflakes'
220
+ include : ' **/*.py'
221
+
215
222
PythonFlake8 :
216
223
description : ' Analyzing with flake8'
217
224
required_executable : ' flake8'
Original file line number Diff line number Diff line change
1
+ module Overcommit ::Hook ::PreCommit
2
+ # Runs `pyflakes` against any modified Python files.
3
+ class Pyflakes < Base
4
+ MESSAGE_REGEX = /^(?<file>[^:]+):(?<line>\d +):/
5
+
6
+ def run
7
+ result = execute ( command + applicable_files )
8
+ return :pass if result . success?
9
+
10
+ errors = get_messages ( result . stderr , :error )
11
+ warnings = get_messages ( result . stdout , :warning )
12
+
13
+ errors + warnings
14
+ end
15
+
16
+ private
17
+
18
+ def get_messages ( output , type )
19
+ # example message:
20
+ # path/to/file.py:57: local variable 'x' is assigned to but never used
21
+ extract_messages (
22
+ output . split ( "\n " ) . grep ( MESSAGE_REGEX ) ,
23
+ MESSAGE_REGEX ,
24
+ proc { type }
25
+ )
26
+ end
27
+ end
28
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::PreCommit ::Pyflakes do
4
+ let ( :config ) { Overcommit ::ConfigurationLoader . default_configuration }
5
+ let ( :context ) { double ( 'context' ) }
6
+ subject { described_class . new ( config , context ) }
7
+
8
+ before do
9
+ subject . stub ( :applicable_files ) . and_return ( %w[ file1.py file2.py ] )
10
+ end
11
+
12
+ context 'when pyflakes exits successfully' do
13
+ before do
14
+ result = double ( 'result' )
15
+ result . stub ( :success? => true )
16
+ subject . stub ( :execute ) . and_return ( result )
17
+ end
18
+
19
+ it { should pass }
20
+ end
21
+
22
+ context 'when pyflakes exits unsucessfully' do
23
+ let ( :result ) { double ( 'result' ) }
24
+
25
+ before do
26
+ result . stub ( success? : false , stdout : '' , stderr : '' )
27
+ subject . stub ( :execute ) . and_return ( result )
28
+ end
29
+
30
+ context 'and it reports a warning' do
31
+ before do
32
+ result . stub ( :stdout ) . and_return ( [
33
+ "file1.py:1: local variable 'x' is assigned to but never used"
34
+ ] . join ( "\n " ) )
35
+
36
+ subject . stub ( :modified_lines_in_file ) . and_return ( [ 2 , 3 ] )
37
+ end
38
+
39
+ it { should warn }
40
+ end
41
+
42
+ context 'and it reports an error' do
43
+ before do
44
+ result . stub ( :stderr ) . and_return ( [
45
+ 'file1.py:1:1: invalid syntax'
46
+ ] . join ( "\n " ) )
47
+
48
+ subject . stub ( :modified_lines_in_file ) . and_return ( [ 1 , 2 ] )
49
+ end
50
+
51
+ it { should fail_hook }
52
+ end
53
+ end
54
+ end
You can’t perform that action at this time.
0 commit comments