1
1
# -*- coding: utf-8 -*-
2
2
3
3
import contextlib
4
+ import io
4
5
import os
5
6
import sys
6
7
import platform
12
13
from StringIO import StringIO
13
14
except ImportError :
14
15
# python 3.x
15
- from io import StringIO
16
+ from io import StringIO # pylint: disable=ungrouped-imports
16
17
17
18
from click .testing import CliRunner
18
19
30
31
from gitlint import hooks
31
32
from gitlint import __version__
32
33
from gitlint import config
34
+ from gitlint .utils import DEFAULT_ENCODING
33
35
34
36
35
37
@contextlib .contextmanager
@@ -194,8 +196,8 @@ def test_msg_filename(self, _):
194
196
195
197
with tempdir () as tmpdir :
196
198
msg_filename = os .path .join (tmpdir , "msg" )
197
- with open (msg_filename , 'w' ) as f :
198
- f .write ("Commït title\n " )
199
+ with io . open (msg_filename , 'w' , encoding = DEFAULT_ENCODING ) as f :
200
+ f .write (u "Commït title\n " )
199
201
200
202
with patch ('gitlint.display.stderr' , new = StringIO ()) as stderr :
201
203
result = self .cli .invoke (cli .cli , ["--msg-filename" , msg_filename ])
@@ -260,7 +262,7 @@ def test_debug(self, sh, _):
260
262
u"file6.txt\n påth/to/file7.txt\n " ]
261
263
262
264
with patch ('gitlint.display.stderr' , new = StringIO ()) as stderr :
263
- config_path = self .get_sample_path ("config/ gitlintconfig" )
265
+ config_path = self .get_sample_path (os . path . join ( "config" , " gitlintconfig") )
264
266
result = self .cli .invoke (cli .cli , ["--config" , config_path , "--debug" , "--commits" ,
265
267
"foo...bar" ])
266
268
@@ -276,8 +278,8 @@ def test_debug(self, sh, _):
276
278
u"DEBUG: gitlint.cli Python version: {0}" .format (sys .version ),
277
279
u"DEBUG: gitlint.cli Git version: git version 1.2.3" ,
278
280
u"DEBUG: gitlint.cli Gitlint version: {0}" .format (__version__ ),
279
- self .get_expected ('debug_configuration_output1' , { 'config_path' : config_path ,
280
- 'target' : os .path .abspath (os .getcwd ())}),
281
+ self .get_expected ('debug_configuration_output1' ,
282
+ { 'config_path' : config_path , 'target' : os .path .realpath (os .getcwd ())}),
281
283
u"DEBUG: gitlint.cli No --msg-filename flag, no or empty data passed to stdin. " +
282
284
u"Attempting to read from the local repo." ,
283
285
u"DEBUG: gitlint.lint Linting commit 6f29bf81a8322a04071bb794666e48c443a90360" ,
@@ -307,7 +309,7 @@ def test_extra_path(self, _):
307
309
308
310
# Test extra-path pointing to a file
309
311
with patch ('gitlint.display.stderr' , new = StringIO ()) as stderr :
310
- extra_path = self .get_sample_path ("user_rules/ my_commit_rules.py" )
312
+ extra_path = self .get_sample_path (os . path . join ( "user_rules" , " my_commit_rules.py") )
311
313
result = self .cli .invoke (cli .cli , ["--extra-path" , extra_path , "--debug" ])
312
314
expected_output = u"1: UC1 Commit violåtion 1: \" Contënt 1\" \n " + \
313
315
"3: B6 Body message is missing\n "
@@ -333,7 +335,7 @@ def test_contrib_negative(self, _):
333
335
def test_config_file (self , _ ):
334
336
""" Test for --config option """
335
337
with patch ('gitlint.display.stderr' , new = StringIO ()) as stderr :
336
- config_path = self .get_sample_path ("config/ gitlintconfig" )
338
+ config_path = self .get_sample_path (os . path . join ( "config" , " gitlintconfig") )
337
339
result = self .cli .invoke (cli .cli , ["--config" , config_path ])
338
340
self .assertEqual (result .output , "" )
339
341
self .assertEqual (stderr .getvalue (), "1: T5\n 3: B6\n " )
@@ -358,7 +360,7 @@ def test_config_file_negative(self):
358
360
self .assertEqual (result .exit_code , self .USAGE_ERROR_CODE )
359
361
360
362
# Invalid config file
361
- config_path = self .get_sample_path ("config/ invalid-option-value" )
363
+ config_path = self .get_sample_path (os . path . join ( "config" , " invalid-option-value") )
362
364
result = self .cli .invoke (cli .cli , ["--config" , config_path ])
363
365
self .assertEqual (result .exit_code , self .CONFIG_ERROR_CODE )
364
366
@@ -382,7 +384,7 @@ def test_target_negative(self):
382
384
self .assertEqual (result .output .split ("\n " )[3 ], expected_msg )
383
385
384
386
# try setting a file as target
385
- target_path = self .get_sample_path ("config/ gitlintconfig" )
387
+ target_path = self .get_sample_path (os . path . join ( "config" , " gitlintconfig") )
386
388
result = self .cli .invoke (cli .cli , ["--target" , target_path ])
387
389
self .assertEqual (result .exit_code , self .USAGE_ERROR_CODE )
388
390
expected_msg = u"Error: Invalid value for \" --target\" : Directory \" {0}\" is a file." .format (target_path )
@@ -394,9 +396,9 @@ def test_generate_config(self, generate_config):
394
396
result = self .cli .invoke (cli .cli , ["generate-config" ], input = u"tëstfile\n " )
395
397
self .assertEqual (result .exit_code , 0 )
396
398
expected_msg = u"Please specify a location for the sample gitlint config file [.gitlint]: tëstfile\n " + \
397
- u"Successfully generated {0}\n " .format (os .path .abspath (u"tëstfile" ))
399
+ u"Successfully generated {0}\n " .format (os .path .realpath (u"tëstfile" ))
398
400
self .assertEqual (result .output , expected_msg )
399
- generate_config .assert_called_once_with (os .path .abspath (u"tëstfile" ))
401
+ generate_config .assert_called_once_with (os .path .realpath (u"tëstfile" ))
400
402
401
403
def test_generate_config_negative (self ):
402
404
""" Negative test for the generate-config subcommand """
@@ -408,7 +410,7 @@ def test_generate_config_negative(self):
408
410
self .assertEqual (result .output , expected_msg )
409
411
410
412
# Existing file
411
- sample_path = self .get_sample_path ("config/ gitlintconfig" )
413
+ sample_path = self .get_sample_path (os . path . join ( "config" , " gitlintconfig") )
412
414
result = self .cli .invoke (cli .cli , ["generate-config" ], input = sample_path )
413
415
self .assertEqual (result .exit_code , self .USAGE_ERROR_CODE )
414
416
expected_msg = "Please specify a location for the sample gitlint " + \
@@ -443,7 +445,7 @@ def test_install_hook(self, install_hook):
443
445
self .assertEqual (result .exit_code , 0 )
444
446
self .assertEqual (result .output , expected )
445
447
expected_config = config .LintConfig ()
446
- expected_config .target = os .path .abspath (os .getcwd ())
448
+ expected_config .target = os .path .realpath (os .getcwd ())
447
449
install_hook .assert_called_once_with (expected_config )
448
450
449
451
@patch ('gitlint.hooks.GitHookInstaller.install_commit_msg_hook' )
@@ -467,7 +469,7 @@ def test_install_hook_negative(self, install_hook):
467
469
self .assertEqual (result .exit_code , self .GIT_CONTEXT_ERROR_CODE )
468
470
self .assertEqual (result .output , u"tëst\n " )
469
471
expected_config = config .LintConfig ()
470
- expected_config .target = os .path .abspath (os .getcwd ())
472
+ expected_config .target = os .path .realpath (os .getcwd ())
471
473
install_hook .assert_called_once_with (expected_config )
472
474
473
475
@patch ('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook' )
@@ -479,7 +481,7 @@ def test_uninstall_hook(self, uninstall_hook):
479
481
self .assertEqual (result .exit_code , 0 )
480
482
self .assertEqual (result .output , expected )
481
483
expected_config = config .LintConfig ()
482
- expected_config .target = os .path .abspath (os .getcwd ())
484
+ expected_config .target = os .path .realpath (os .getcwd ())
483
485
uninstall_hook .assert_called_once_with (expected_config )
484
486
485
487
@patch ('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook' , side_effect = hooks .GitHookInstallerError (u"tëst" ))
@@ -489,5 +491,5 @@ def test_uninstall_hook_negative(self, uninstall_hook):
489
491
self .assertEqual (result .exit_code , self .GIT_CONTEXT_ERROR_CODE )
490
492
self .assertEqual (result .output , u"tëst\n " )
491
493
expected_config = config .LintConfig ()
492
- expected_config .target = os .path .abspath (os .getcwd ())
494
+ expected_config .target = os .path .realpath (os .getcwd ())
493
495
uninstall_hook .assert_called_once_with (expected_config )
0 commit comments