File tree 3 files changed +66
-0
lines changed
lib/overcommit/hook/pre_commit
spec/overcommit/hook/pre_commit
3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -322,6 +322,13 @@ PreCommit:
322
322
required_executable : ' grep'
323
323
flags : ['-IHn', "^<<<<<<<[ \t]"]
324
324
325
+ NginxTest :
326
+ enabled : false
327
+ description : ' Testing nginx configs'
328
+ required_executable : ' nginx'
329
+ flags : ['-t']
330
+ include : ' **/nginx.conf'
331
+
325
332
Pep257 :
326
333
enabled : false
327
334
description : ' Analyzing docstrings with pep257'
Original file line number Diff line number Diff line change
1
+ module Overcommit ::Hook ::PreCommit
2
+ # Runs `nginx -t` against any modified Nginx config files.
3
+ #
4
+ # @see https://www.nginx.com/resources/wiki/start/topics/tutorials/commandline/
5
+ class NginxTest < Base
6
+ MESSAGE_REGEX = /^nginx: .+ in (?<file>.+):(?<line>\d +)$/
7
+
8
+ def run
9
+ messages = [ ]
10
+
11
+ applicable_files . each do |file |
12
+ result = execute ( command + [ '-c' , file ] )
13
+ next if result . success?
14
+
15
+ messages += extract_messages (
16
+ result . stderr . split ( "\n " ) . grep ( MESSAGE_REGEX ) ,
17
+ MESSAGE_REGEX
18
+ )
19
+ end
20
+
21
+ messages
22
+ end
23
+ end
24
+ end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Overcommit ::Hook ::PreCommit ::NginxTest 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
+ subject . stub ( :applicable_files ) . and_return ( %w[ nginx.conf ] )
12
+ subject . stub ( :execute ) . and_return ( result )
13
+ end
14
+
15
+ context 'when nginx -t exits successfully' do
16
+ before do
17
+ result . stub ( :success? ) . and_return ( true )
18
+ end
19
+
20
+ it { should pass }
21
+ end
22
+
23
+ context 'when nginx -t exits unsuccessfully' do
24
+ let ( :result ) { double ( 'result' ) }
25
+
26
+ before do
27
+ result . stub ( success? : false , stderr : normalize_indent ( <<-OUT ) )
28
+ nginx: [emerg] unknown directive "erver" in nginx.conf:2
29
+ nginx: configuration file nginx.conf test failed
30
+ OUT
31
+ end
32
+
33
+ it { should fail_hook }
34
+ end
35
+ end
You can’t perform that action at this time.
0 commit comments