diff --git a/pytest_examples/eval_example.py b/pytest_examples/eval_example.py index e2f19e7..a27ad03 100644 --- a/pytest_examples/eval_example.py +++ b/pytest_examples/eval_example.py @@ -269,5 +269,5 @@ def _mark_for_update(self, example: CodeExample) -> None: def _write_file(self, example: CodeExample) -> Path: python_file = self.tmp_path / f'{example.module_name}.py' - python_file.write_text(example.source) + python_file.write_text(example.source, encoding='utf-8') return python_file diff --git a/pytest_examples/lint.py b/pytest_examples/lint.py index 67133d4..07d22b0 100644 --- a/pytest_examples/lint.py +++ b/pytest_examples/lint.py @@ -51,7 +51,7 @@ def ruff_check( ruff = find_ruff_bin() args = ruff, 'check', '-', *config.ruff_config(), *extra_ruff_args - p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True) + p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding='utf-8') stdout, stderr = p.communicate(example.source, timeout=10) if p.returncode == 1 and stdout: diff --git a/pytest_examples/modify_files.py b/pytest_examples/modify_files.py index dc94387..7df4a07 100644 --- a/pytest_examples/modify_files.py +++ b/pytest_examples/modify_files.py @@ -34,7 +34,7 @@ def _modify_files(examples: list[CodeExample]) -> str: msg = [f'pytest-examples: {len(examples)} examples to update in {len(files)} file(s)...'] for path, g in groupby(examples, key=lambda ex: ex.path): - content = path.read_text() + content = path.read_text(encoding='utf-8') count = 0 for ex in g: example: CodeExample = ex @@ -45,6 +45,6 @@ def _modify_files(examples: list[CodeExample]) -> str: count += 1 msg.append(f' {path} {count} examples updated') - path.write_text(content) + path.write_text(content, encoding='utf-8') return '\n'.join(msg) diff --git a/tests/test_run_examples.py b/tests/test_run_examples.py index d9df4b2..2313092 100644 --- a/tests/test_run_examples.py +++ b/tests/test_run_examples.py @@ -370,3 +370,23 @@ def test_find_run_examples(example: CodeExample, eval_example: EvalExample): result = pytester.runpytest('-p', 'no:pretty', '-v') result.assert_outcomes(passed=1) + + +def test_unicode_emoji(pytester: pytest.Pytester): + pytester.makefile( + '.md', + my_file="```py\nprint('🚀 ✓')\n#> 🚀 ✓\n```", + ) + pytester.makepyfile( + """ +from pytest_examples import find_examples, CodeExample, EvalExample +import pytest + +@pytest.mark.parametrize('example', find_examples('.'), ids=str) +def test_find_run_examples(example: CodeExample, eval_example: EvalExample): + eval_example.run_print_check(example) +""" + ) + + result = pytester.runpytest('-p', 'no:pretty', '-v') + result.assert_outcomes(passed=1)