Skip to content

Commit c8fa204

Browse files
proinsiastrotzig
authored andcommitted
Add rst-lint pre-commit hook (sds#525)
Analyze reStructuredText files with `rst-lint`/`restructuredtext-lint`.
1 parent 907e5ec commit c8fa204

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

config/default.yml

+7
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ PreCommit:
566566
- '**/Gemfile'
567567
- '**/Rakefile'
568568

569+
RstLint:
570+
enabled: false
571+
description: 'Analyze reStructuredText files with rst-lint'
572+
required_executable: 'rst-lint'
573+
install_command: 'pip install restructuredtext_lint'
574+
include: '**/*.rst'
575+
569576
RuboCop:
570577
enabled: false
571578
description: 'Analyze with RuboCop'
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Overcommit::Hook::PreCommit
2+
# Runs `rst-lint` against any modified reStructuredText files
3+
#
4+
# @see https://github.com/twolfson/restructuredtext-lint
5+
class RstLint < Base
6+
MESSAGE_REGEX = /
7+
^(?<type>INFO|WARNING|ERROR|SEVERE)(?<file>(?:\w:)?[^:]+):(?<line>\d+)\s(?<msg>.+)
8+
/x
9+
10+
def run
11+
result = execute(command, args: applicable_files)
12+
output = result.stdout.chomp
13+
14+
return :pass if result.success?
15+
return [:fail, result.stderr] unless result.stderr.empty?
16+
17+
# example message:
18+
# WARNING README.rst:7 Title underline too short.
19+
extract_messages(
20+
output.split("\n"),
21+
MESSAGE_REGEX
22+
)
23+
end
24+
end
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PreCommit::RstLint do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
let(:result) { double('result') }
9+
10+
before do
11+
result.stub(success?: success, stdout: stdout, stderr: stderr)
12+
subject.stub(:applicable_files).and_return(%w[file1.rst file2.rst])
13+
subject.stub(:execute).and_return(result)
14+
end
15+
16+
context 'when rst-lint exits successfully' do
17+
let(:success) { true }
18+
let(:stdout) { '' }
19+
let(:stderr) { '' }
20+
21+
it { should pass }
22+
end
23+
24+
context 'when rst-lint exits unsuccessfully' do
25+
let(:success) { false }
26+
27+
context 'and it reports an error' do
28+
let(:stdout) { 'WARNING file1.rst:7 Title underline too short.' }
29+
let(:stderr) { '' }
30+
31+
it { should fail_hook }
32+
end
33+
34+
context 'when there is an error running rst-lint' do
35+
let(:stdout) { '' }
36+
let(:stderr) { 'Some runtime error' }
37+
38+
it { should fail_hook }
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)