@@ -139,6 +139,17 @@ def test_revisionrange_parse(revision_range, expect):
139
139
assert result == expect
140
140
141
141
142
+ def git_call (cmd , encoding = None ):
143
+ """Returns a mocked call to git"""
144
+ return call (
145
+ cmd .split (),
146
+ cwd = str (Path ("/path" )),
147
+ encoding = encoding ,
148
+ stderr = PIPE ,
149
+ env = {"LC_ALL" : "C" , "PATH" : os .environ ["PATH" ]},
150
+ )
151
+
152
+
142
153
@pytest .mark .kwparametrize (
143
154
dict (
144
155
revision = ":WORKTREE:" ,
@@ -147,31 +158,31 @@ def test_revisionrange_parse(revision_range, expect):
147
158
dict (
148
159
revision = "HEAD" ,
149
160
expect_git_calls = [
150
- "git show HEAD:./my.txt" ,
151
- "git log -1 --format=%ct HEAD -- my.txt" ,
161
+ git_call ( "git show HEAD:./my.txt" ) ,
162
+ git_call ( "git log -1 --format=%ct HEAD -- my.txt" , encoding = "utf-8" ) ,
152
163
],
153
164
expect_textdocument_calls = [
154
- call .from_lines ([ b"1627107028" ] , mtime = "2021-07-24 06:10:28.000000 +0000" )
165
+ call .from_bytes ( b"1627107028" , mtime = "2021-07-24 06:10:28.000000 +0000" )
155
166
],
156
167
),
157
168
dict (
158
169
revision = "HEAD^" ,
159
170
expect_git_calls = [
160
- "git show HEAD^:./my.txt" ,
161
- "git log -1 --format=%ct HEAD^ -- my.txt" ,
171
+ git_call ( "git show HEAD^:./my.txt" ) ,
172
+ git_call ( "git log -1 --format=%ct HEAD^ -- my.txt" , encoding = "utf-8" ) ,
162
173
],
163
174
expect_textdocument_calls = [
164
- call .from_lines ([ b"1627107028" ] , mtime = "2021-07-24 06:10:28.000000 +0000" )
175
+ call .from_bytes ( b"1627107028" , mtime = "2021-07-24 06:10:28.000000 +0000" )
165
176
],
166
177
),
167
178
dict (
168
179
revision = "master" ,
169
180
expect_git_calls = [
170
- "git show master:./my.txt" ,
171
- "git log -1 --format=%ct master -- my.txt" ,
181
+ git_call ( "git show master:./my.txt" ) ,
182
+ git_call ( "git log -1 --format=%ct master -- my.txt" , encoding = "utf-8" ) ,
172
183
],
173
184
expect_textdocument_calls = [
174
- call .from_lines ([ b"1627107028" ] , mtime = "2021-07-24 06:10:28.000000 +0000" )
185
+ call .from_bytes ( b"1627107028" , mtime = "2021-07-24 06:10:28.000000 +0000" )
175
186
],
176
187
),
177
188
expect_git_calls = [],
@@ -189,17 +200,7 @@ def test_git_get_content_at_revision_obtain_file_content(
189
200
190
201
git .git_get_content_at_revision (Path ("my.txt" ), revision , Path ("/path" ))
191
202
192
- expected_calls = [
193
- call (
194
- expected_call .split (),
195
- cwd = str (Path ("/path" )),
196
- encoding = "utf-8" ,
197
- stderr = PIPE ,
198
- env = {"LC_ALL" : "C" , "PATH" : os .environ ["PATH" ]},
199
- )
200
- for expected_call in expect_git_calls
201
- ]
202
- assert check_output .call_args_list == expected_calls
203
+ assert check_output .call_args_list == expect_git_calls
203
204
assert text_document_class .method_calls == expect_textdocument_calls
204
205
205
206
@@ -403,6 +404,58 @@ def test_git_get_content_at_revision_stderr(git_repo, capfd, caplog):
403
404
assert caplog .text == ""
404
405
405
406
407
+ @pytest .fixture (scope = "module" )
408
+ def encodings_repo (tmp_path_factory ):
409
+ """Create an example Git repository using various encodings for the same file"""
410
+ tmpdir = tmp_path_factory .mktemp ("branched_repo" )
411
+ git_repo = GitRepoFixture .create_repository (tmpdir )
412
+ # Commit without an encoding cookie, defaults to utf-8
413
+ git_repo .add ({"file.py" : "darker = 'plus foncé'\n " }, commit = "Default encoding" )
414
+ git_repo .create_tag ("default" )
415
+ # Commit without an encoding cookie but with a utf-8 signature
416
+ content = "darker = 'plus foncé'\n " .encode ("utf-8-sig" )
417
+ git_repo .add ({"file.py" : content }, commit = "utf-8-sig" )
418
+ git_repo .create_tag ("utf-8-sig" )
419
+ # Commit with an iso-8859-1 encoding cookie
420
+ content = "# coding: iso-8859-1\n darker = 'plus foncé'\n " .encode ("iso-8859-1" )
421
+ git_repo .add ({"file.py" : content }, commit = "iso-8859-1" )
422
+ git_repo .create_tag ("iso-8859-1" )
423
+ # Commit with a utf-8 encoding cookie
424
+ content = "# coding: utf-8\n python = 'パイソン'\n " .encode ("utf-8" )
425
+ git_repo .add ({"file.py" : content }, commit = "utf-8" )
426
+ git_repo .create_tag ("utf-8" )
427
+ # Current worktree content (not committed) with a shitfjs encoding cookie
428
+ content = "# coding: shiftjis\n python = 'パイソン'\n " .encode ("shiftjis" )
429
+ git_repo .add ({"file.py" : content })
430
+ return git_repo
431
+
432
+
433
+ @pytest .mark .kwparametrize (
434
+ dict (commit = "default" , encoding = "utf-8" , lines = ("darker = 'plus foncé'" ,)),
435
+ dict (commit = "utf-8-sig" , encoding = "utf-8-sig" , lines = ("darker = 'plus foncé'" ,)),
436
+ dict (
437
+ commit = "iso-8859-1" ,
438
+ encoding = "iso-8859-1" ,
439
+ lines = ("# coding: iso-8859-1" , "darker = 'plus foncé'" ),
440
+ ),
441
+ dict (
442
+ commit = "utf-8" , encoding = "utf-8" , lines = ("# coding: utf-8" , "python = 'パイソン'" )
443
+ ),
444
+ dict (
445
+ commit = ":WORKTREE:" ,
446
+ encoding = "shiftjis" ,
447
+ lines = ("# coding: shiftjis" , "python = 'パイソン'" ),
448
+ ),
449
+ )
450
+ def test_git_get_content_at_revision_encoding (encodings_repo , commit , encoding , lines ):
451
+ """Git file is loaded using its historical encoding"""
452
+ result = git .git_get_content_at_revision (
453
+ Path ("file.py" ), commit , encodings_repo .root
454
+ )
455
+ assert result .encoding == encoding
456
+ assert result .lines == lines
457
+
458
+
406
459
@pytest .mark .kwparametrize (
407
460
dict (retval = 0 , expect = True ),
408
461
dict (retval = 1 , expect = False ),
0 commit comments