Skip to content

Commit 87c0e68

Browse files
jawshooahsds
authored andcommitted
Add pre-commit hook for pyflakes
Add hook for pyflakes. Disable by default, as flake8 includes pyflakes. Change-Id: I45e22e5f24f5955e8cec63c3f8c6252b4855648f Reviewed-on: http://gerrit.causes.com/47096 Tested-by: jenkins <jenkins@brigade.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent 44a685d commit 87c0e68

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

config/default.yml

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ PreCommit:
212212
- '**/*.rb'
213213
- '**/*.rake'
214214

215+
Pyflakes:
216+
enabled: false
217+
description: 'Analyzing with pyflakes'
218+
required_executable: 'pyflakes'
219+
install_command: 'pip install pyflakes'
220+
include: '**/*.py'
221+
215222
PythonFlake8:
216223
description: 'Analyzing with flake8'
217224
required_executable: 'flake8'
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

0 commit comments

Comments
 (0)