File tree 3 files changed +73
-0
lines changed
lib/overcommit/hook/pre_commit
spec/overcommit/hook/pre_commit
3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -566,6 +566,13 @@ PreCommit:
566
566
- ' **/Gemfile'
567
567
- ' **/Rakefile'
568
568
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
+
569
576
RuboCop :
570
577
enabled : false
571
578
description : ' Analyze with RuboCop'
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments