1+ from __future__ import unicode_literals
2+
3+ import io
14import logging
25import mock
36import os .path
@@ -20,14 +23,18 @@ def get_short_git_status():
2023 return dict (reversed (line .split ()) for line in git_status .splitlines ())
2124
2225
23- @pytest .yield_fixture
24- def foo_staged (empty_git_dir ):
26+ def write_gitignore ():
2527 with open ('.gitignore' , 'w' ) as gitignore_file :
2628 gitignore_file .write (C .HOOKS_WORKSPACE + '\n ' )
29+
30+
31+ @pytest .yield_fixture
32+ def foo_staged (empty_git_dir ):
33+ write_gitignore ()
2734 local ['git' ]['add' , '.' ]()
2835 local ['git' ]['commit' , '-m' , 'add gitignore' ]()
2936
30- with open ('foo' , 'w' ) as foo_file :
37+ with io . open ('foo' , 'w' ) as foo_file :
3138 foo_file .write (FOO_CONTENTS )
3239 local ['git' ]['add' , 'foo' ]()
3340 foo_filename = os .path .join (empty_git_dir , 'foo' )
@@ -41,7 +48,7 @@ def cmd_runner():
4148
4249def _test_foo_state (path , foo_contents = FOO_CONTENTS , status = 'A' ):
4350 assert os .path .exists (path .foo_filename )
44- assert open (path .foo_filename ).read () == foo_contents
51+ assert io . open (path .foo_filename , encoding = 'utf-8' ).read () == foo_contents
4552 actual_status = get_short_git_status ()['foo' ]
4653 assert status == actual_status
4754
@@ -57,7 +64,7 @@ def test_foo_nothing_unstaged(foo_staged, cmd_runner):
5764
5865
5966def test_foo_something_unstaged (foo_staged , cmd_runner ):
60- with open (foo_staged .foo_filename , 'w' ) as foo_file :
67+ with io . open (foo_staged .foo_filename , 'w' ) as foo_file :
6168 foo_file .write ('herp\n derp\n ' )
6269
6370 _test_foo_state (foo_staged , 'herp\n derp\n ' , 'AM' )
@@ -69,7 +76,7 @@ def test_foo_something_unstaged(foo_staged, cmd_runner):
6976
7077
7178def test_foo_both_modify_non_conflicting (foo_staged , cmd_runner ):
72- with open (foo_staged .foo_filename , 'w' ) as foo_file :
79+ with io . open (foo_staged .foo_filename , 'w' ) as foo_file :
7380 foo_file .write (FOO_CONTENTS + '9\n ' )
7481
7582 _test_foo_state (foo_staged , FOO_CONTENTS + '9\n ' , 'AM' )
@@ -78,7 +85,7 @@ def test_foo_both_modify_non_conflicting(foo_staged, cmd_runner):
7885 _test_foo_state (foo_staged )
7986
8087 # Modify the file as part of the "pre-commit"
81- with open (foo_staged .foo_filename , 'w' ) as foo_file :
88+ with io . open (foo_staged .foo_filename , 'w' ) as foo_file :
8289 foo_file .write (FOO_CONTENTS .replace ('1' , 'a' ))
8390
8491 _test_foo_state (foo_staged , FOO_CONTENTS .replace ('1' , 'a' ), 'AM' )
@@ -87,7 +94,7 @@ def test_foo_both_modify_non_conflicting(foo_staged, cmd_runner):
8794
8895
8996def test_foo_both_modify_conflicting (foo_staged , cmd_runner ):
90- with open (foo_staged .foo_filename , 'w' ) as foo_file :
97+ with io . open (foo_staged .foo_filename , 'w' ) as foo_file :
9198 foo_file .write (FOO_CONTENTS .replace ('1' , 'a' ))
9299
93100 _test_foo_state (foo_staged , FOO_CONTENTS .replace ('1' , 'a' ), 'AM' )
@@ -96,7 +103,7 @@ def test_foo_both_modify_conflicting(foo_staged, cmd_runner):
96103 _test_foo_state (foo_staged )
97104
98105 # Modify in the same place as the stashed diff
99- with open (foo_staged .foo_filename , 'w' ) as foo_file :
106+ with io . open (foo_staged .foo_filename , 'w' ) as foo_file :
100107 foo_file .write (FOO_CONTENTS .replace ('1' , 'b' ))
101108
102109 _test_foo_state (foo_staged , FOO_CONTENTS .replace ('1' , 'b' ), 'AM' )
@@ -106,8 +113,7 @@ def test_foo_both_modify_conflicting(foo_staged, cmd_runner):
106113
107114@pytest .yield_fixture
108115def img_staged (empty_git_dir ):
109- with open ('.gitignore' , 'w' ) as gitignore_file :
110- gitignore_file .write (C .HOOKS_WORKSPACE + '\n ' )
116+ write_gitignore ()
111117 local ['git' ]['add' , '.' ]()
112118 local ['git' ]['commit' , '-m' , 'add gitignore' ]()
113119
@@ -120,8 +126,8 @@ def img_staged(empty_git_dir):
120126def _test_img_state (path , expected_file = 'img1.jpg' , status = 'A' ):
121127 assert os .path .exists (path .img_filename )
122128 assert (
123- open (path .img_filename , 'rb' ).read () ==
124- open (get_resource_path (expected_file ), 'rb' ).read ()
129+ io . open (path .img_filename , 'rb' ).read () ==
130+ io . open (get_resource_path (expected_file ), 'rb' ).read ()
125131 )
126132 actual_status = get_short_git_status ()['img.jpg' ]
127133 assert status == actual_status
@@ -246,3 +252,14 @@ def test_diff_returns_1_no_diff_though(fake_logging_handler, foo_staged):
246252 with staged_files_only (cmd_runner ):
247253 pass
248254 assert not fake_logging_handler .logs
255+
256+
257+ def test_stage_utf8_changes (foo_staged , cmd_runner ):
258+ contents = '\u2603 '
259+ with io .open ('foo' , 'w' , encoding = 'utf-8' ) as foo_file :
260+ foo_file .write (contents )
261+
262+ _test_foo_state (foo_staged , contents , 'AM' )
263+ with staged_files_only (cmd_runner ):
264+ _test_foo_state (foo_staged )
265+ _test_foo_state (foo_staged , contents , 'AM' )
0 commit comments