@@ -48,6 +48,12 @@ def install(self, allow_errors=False):
4848 raise
4949 return False , exc .output
5050
51+ def write_ignore_list (self , * args ):
52+ with WorkDir (os .path .join (self .repo .git_dir , '..' )):
53+ with open ('.clang-format-hook-exclude' , 'w' ) as exclude_file :
54+ exclude_file .write ('\n ' .join (args ))
55+ exclude_file .write ('\n ' )
56+
5157 def test_install (self ):
5258 res , output = self .install ()
5359 self .assertTrue (res )
@@ -205,6 +211,57 @@ def test_install_from_scripts_dir(self):
205211 # The file on disk is updated.
206212 self .assertEqual (self .repo .read_file (data .FILENAME ), data .FIXED )
207213
214+ def test_commit_ignorefile_empty (self ):
215+ self .install ()
216+
217+ self .repo .write_file (data .FILENAME , data .CODE )
218+ self .repo .add (data .FILENAME )
219+
220+ # Don't ignore anything.
221+ self .write_ignore_list ()
222+
223+ self .repo .commit (input_text = 'a\n ' )
224+ # The file on disk is updated as it's not ignored.
225+ self .assertEqual (self .repo .read_file (data .FILENAME ), data .FIXED )
226+
227+ def test_commit_ignorefile_comments_only (self ):
228+ self .install ()
229+
230+ self .repo .write_file (data .FILENAME , data .CODE )
231+ self .repo .add (data .FILENAME )
232+
233+ # Don't ignore anything, but add comments and empty lines.
234+ self .write_ignore_list ('# Hello world' , '' , '# Bar baz' )
235+
236+ self .repo .commit (input_text = 'a\n ' )
237+ # The file on disk is updated as it's not ignored.
238+ self .assertEqual (self .repo .read_file (data .FILENAME ), data .FIXED )
239+
240+ def test_commit_ignorefile_ignore_foo (self ):
241+ self .install ()
242+
243+ self .repo .write_file (data .FILENAME , data .CODE )
244+ self .repo .add (data .FILENAME )
245+
246+ # Ignore the C file we modify.
247+ self .write_ignore_list (data .FILENAME )
248+
249+ self .repo .commit ()
250+ # The file on disk is updated but its formatting was not fixed.
251+ self .assertEqual (self .repo .read_file (data .FILENAME ), data .CODE )
252+
253+ def test_commit_ignorefile_ignore_pattern (self ):
254+ self .install ()
255+
256+ self .repo .write_file (data .FILENAME , data .CODE )
257+ self .repo .add (data .FILENAME )
258+
259+ # Ignore the C file we modify through a pattern.
260+ self .write_ignore_list (r'f.o\.c' )
261+
262+ self .repo .commit ()
263+ # The file on disk is updated but its formatting was not fixed.
264+ self .assertEqual (self .repo .read_file (data .FILENAME ), data .CODE )
208265
209266
210267class HookClonedTestCase (CloneRepoMixin ,
0 commit comments