Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Pep8 and Pep257 Hooks #493

Merged
merged 1 commit into from
Jun 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
* [Mdl](lib/overcommit/hook/pre_commit/mdl.rb)
* [`*`MergeConflicts](lib/overcommit/hook/pre_commit/merge_conflicts.rb)
* [NginxTest](lib/overcommit/hook/pre_commit/nginx_test.rb)
* [Pep257](lib/overcommit/hook/pre_commit/pep257.rb)
* [Pep8](lib/overcommit/hook/pre_commit/pep8.rb)
* [PuppetLint](lib/overcommit/hook/pre_commit/puppet_lint.rb)
* [Pycodestyle](lib/overcommit/hook/pre_commit/pycodestyle.rb)
* [Pydocstyle](lib/overcommit/hook/pre_commit/pydocstyle.rb)
* [Pyflakes](lib/overcommit/hook/pre_commit/pyflakes.rb)
* [Pylint](lib/overcommit/hook/pre_commit/pylint.rb)
* [PythonFlake8](lib/overcommit/hook/pre_commit/python_flake8.rb)
Expand Down
18 changes: 16 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,14 @@ PreCommit:
flags: ['-t']
include: '**/nginx.conf'

Pep257:
Pep257: # Deprecated – use Pydocstyle instead.
enabled: false
description: 'Analyze docstrings with pep257'
required_executable: 'pep257'
install_command: 'pip install pep257'
include: '**/*.py'

Pep8:
Pep8: # Deprecated – use Pycodestyle instead.
enabled: false
description: 'Analyze with pep8'
required_executable: 'pep8'
Expand All @@ -463,6 +463,20 @@ PreCommit:
- '--error-level=all'
include: '**/*.pp'

Pycodestyle:
enabled: false
description: 'Analyze with pycodestyle'
required_executable: 'pycodestyle'
install_command: 'pip install pycodestyle'
include: '**/*.py'

Pydocstyle:
enabled: false
description: 'Analyze docstrings with pydocstyle'
required_executable: 'pydocstyle'
install_command: 'pip install pydocstyle'
include: '**/*.py'

Pyflakes:
enabled: false
description: 'Analyze with pyflakes'
Expand Down
21 changes: 21 additions & 0 deletions lib/overcommit/hook/pre_commit/pycodestyle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Overcommit::Hook::PreCommit
# Runs `pycodestyle` against any modified Python files.
#
# @see https://pypi.python.org/pypi/pycodestyle
class Pycodestyle < Base
def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp

return :pass if result.success? && output.empty?

# example message:
# path/to/file.py:88:5: E301 expected 1 blank line, found 0
extract_messages(
output.split("\n"),
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+):\d+:\s(?<type>E|W)/,
lambda { |type| type.include?('W') ? :warning : :error }
)
end
end
end
21 changes: 21 additions & 0 deletions lib/overcommit/hook/pre_commit/pydocstyle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Overcommit::Hook::PreCommit
# Runs `pydocstyle` against any modified Python files.
#
# @see https://pypi.python.org/pypi/pydocstyle
class Pydocstyle < Base
def run
result = execute(command, args: applicable_files)
return :pass if result.success?

output = result.stderr.chomp

# example message:
# path/to/file.py:1 in public method `foo`:
# D102: Docstring missing
extract_messages(
output.gsub(/:\s+/, ': ').split("\n"),
/^(?<file>(?:\w:)?[^:]+):(?<line>\d+)/
)
end
end
end
50 changes: 50 additions & 0 deletions spec/overcommit/hook/pre_commit/pycodestyle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::Pycodestyle do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.py file2.py])
end

context 'when pycodestyle exits successfully' do
before do
result = double('result')
result.stub(success?: true, stdout: '')
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when pycodestyle exits unsucessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
subject.stub(:execute).and_return(result)
end

context 'and it reports a warning' do
before do
result.stub(:stdout).and_return([
'file1.py:1:1: W391 blank line at end of file'
].join("\n"))
end

it { should warn }
end

context 'and it reports an error' do
before do
result.stub(:stdout).and_return([
'file1.py:1:80: E501 line too long (80 > 79 characters)'
].join("\n"))
end

it { should fail_hook }
end
end
end
41 changes: 41 additions & 0 deletions spec/overcommit/hook/pre_commit/pydocstyle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::Pydocstyle do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.py file2.py])
end

context 'when pydocstyle exits successfully' do
before do
result = double('result')
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when pydocstyle exits unsucessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
subject.stub(:execute).and_return(result)
end

context 'and it reports an error' do
before do
result.stub(:stderr).and_return([
'file1.py:1 in public method `foo`:',
' D102: Docstring missing'
].join("\n"))
end

it { should fail_hook }
end
end
end