Skip to content
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
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ PreCommit:
- '**/Gemfile'
- '**/Rakefile'

RstLint:
enabled: false
description: 'Analyze reStructuredText files with rst-lint'
required_executable: 'rst-lint'
install_command: 'pip install restructuredtext_lint'
include: '**/*.rst'

RuboCop:
enabled: false
description: 'Analyze with RuboCop'
Expand Down
25 changes: 25 additions & 0 deletions lib/overcommit/hook/pre_commit/rst_lint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Overcommit::Hook::PreCommit
# Runs `rst-lint` against any modified reStructuredText files
#
# @see https://github.com/twolfson/restructuredtext-lint
class RstLint < Base
MESSAGE_REGEX = /
^(?<type>INFO|WARNING|ERROR|SEVERE)(?<file>(?:\w:)?[^:]+):(?<line>\d+)\s(?<msg>.+)
/x

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp

return :pass if result.success?
return [:fail, result.stderr] unless result.stderr.empty?

# example message:
# WARNING README.rst:7 Title underline too short.
extract_messages(
output.split("\n"),
MESSAGE_REGEX
)
end
end
end
41 changes: 41 additions & 0 deletions spec/overcommit/hook/pre_commit/rst_lint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

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

let(:result) { double('result') }

before do
result.stub(success?: success, stdout: stdout, stderr: stderr)
subject.stub(:applicable_files).and_return(%w[file1.rst file2.rst])
subject.stub(:execute).and_return(result)
end

context 'when rst-lint exits successfully' do
let(:success) { true }
let(:stdout) { '' }
let(:stderr) { '' }

it { should pass }
end

context 'when rst-lint exits unsuccessfully' do
let(:success) { false }

context 'and it reports an error' do
let(:stdout) { 'WARNING file1.rst:7 Title underline too short.' }
let(:stderr) { '' }

it { should fail_hook }
end

context 'when there is an error running rst-lint' do
let(:stdout) { '' }
let(:stderr) { 'Some runtime error' }

it { should fail_hook }
end
end
end