Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pytest_examples/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ExamplesConfig:
ruff_ignore: list[str] | None = None
white_space_dot: bool = False
"""If True, replace spaces with `Β·` in example diffs."""
encoding: str | None = 'utf-8'
"""File encoding to use for write file operations. Default is 'utf-8'."""

def black_mode(self):
return BlackMode(
Expand Down
5 changes: 4 additions & 1 deletion pytest_examples/eval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def set_config(
ruff_line_length: int | None = None,
ruff_select: list[str] | None = None,
ruff_ignore: list[str] | None = None,
encoding: str | None = 'utf-8',
):
"""Set the config for lints.

Expand All @@ -56,6 +57,7 @@ def set_config(
ruff_line_length: In general, we disable line-length checks in ruff, to let black take care of them.
ruff_select: Ruff rules to select
ruff_ignore: Ruff rules to ignore
encoding: File encoding to use for write file operations, defaults to 'utf-8'.
"""
self.config = ExamplesConfig(
line_length=line_length,
Expand All @@ -67,6 +69,7 @@ def set_config(
ruff_line_length=ruff_line_length,
ruff_select=ruff_select,
ruff_ignore=ruff_ignore,
encoding=encoding,
)

@property
Expand Down Expand Up @@ -269,5 +272,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=self.config.encoding)
return python_file
2 changes: 1 addition & 1 deletion pytest_examples/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=config.encoding)
stdout, stderr = p.communicate(example.source, timeout=10)
if p.returncode == 1 and stdout:

Expand Down
6 changes: 3 additions & 3 deletions pytest_examples/modify_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .find_examples import CodeExample


def _modify_files(examples: list[CodeExample]) -> str:
def _modify_files(examples: list[CodeExample], encoding: str | None = 'utf-8') -> str:
"""Internal use only, update examples in place."""
# The same example shouldn't appear more than once
unique_examples: set[str] = set()
Expand All @@ -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=encoding)
count = 0
for ex in g:
example: CodeExample = ex
Expand All @@ -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=encoding)

return '\n'.join(msg)
20 changes: 20 additions & 0 deletions tests/test_run_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading