|
2 | 2 |
|
3 | 3 | import copy |
4 | 4 | import doctest |
| 5 | +import json |
5 | 6 | import os |
6 | 7 | import subprocess |
7 | 8 | import sys |
@@ -566,14 +567,33 @@ def setUp(self): |
566 | 567 | self._write('ptr.txt', '/a\n') |
567 | 568 |
|
568 | 569 | def _write(self, name, content): |
569 | | - with open(os.path.join(self.dir, name), 'w') as f: |
| 570 | + with open(os.path.join(self.dir, name), 'w', encoding='utf-8') as f: |
570 | 571 | f.write(content) |
571 | 572 |
|
572 | | - def _run(self, *args): |
| 573 | + def _run(self, *args, utf8=True): |
573 | 574 | env = dict(os.environ, PYTHONPATH=self.ROOT, PYTHONUTF8='1') |
| 575 | + if not utf8: |
| 576 | + # Keep pipes in UTF-8 while exercising locale-dependent file I/O. |
| 577 | + env.update(PYTHONUTF8='0', PYTHONIOENCODING='utf-8', |
| 578 | + LC_ALL='C', PYTHONCOERCECLOCALE='0') |
574 | 579 | return subprocess.run([sys.executable, self.SCRIPT] + list(args), |
575 | 580 | cwd=self.dir, env=env, capture_output=True, |
576 | | - text=True) |
| 581 | + encoding='utf-8') |
| 582 | + |
| 583 | + def test_utf8_json_file_without_utf8_mode(self): |
| 584 | + self._write('unicode.json', '{"value": "🚀"}') |
| 585 | + proc = self._run('/value', 'unicode.json', utf8=False) |
| 586 | + self.assertEqual(proc.returncode, 0, proc.stderr) |
| 587 | + self.assertEqual(json.loads(proc.stdout), '🚀') |
| 588 | + self.assertEqual(proc.stderr, '') |
| 589 | + |
| 590 | + def test_utf8_pointer_file_without_utf8_mode(self): |
| 591 | + self._write('unicode.json', json.dumps({'ä': 'expected'})) |
| 592 | + self._write('unicode.txt', '/ä\n') |
| 593 | + proc = self._run('-f', 'unicode.txt', 'unicode.json', utf8=False) |
| 594 | + self.assertEqual(proc.returncode, 0, proc.stderr) |
| 595 | + self.assertEqual(json.loads(proc.stdout), 'expected') |
| 596 | + self.assertEqual(proc.stderr, '') |
577 | 597 |
|
578 | 598 | def test_positional_pointer(self): |
579 | 599 | proc = self._run('/a', 'a.json', 'b.json') |
|
0 commit comments