Debian Codegraph @

tests/benchmark/test_api.py

raw
1 from collections.abc import Callable
2 from pathlib import Path
3 from typing import Protocol
4
5 import pytest
6
7 from isort import api
8
9
10 class BenchmarkFixture(Protocol):
11 def pedantic(self, target: Callable[[], object], *, iterations: int, rounds: int) -> None: ...
12
13
14 imperfect_content = "import b\nimport a\n"
15 fixed_content = "import a\nimport b\n"
16
17
18 @pytest.fixture
19 def imperfect(tmp_path: Path) -> Path:
20 imperfect_file = tmp_path / "test_needs_changes.py"
21 imperfect_file.write_text(imperfect_content, "utf8")
22 return imperfect_file
23
24
25 def test_sort_file(benchmark: BenchmarkFixture, imperfect: Path) -> None:
26 def sort_file() -> None:
27 api.sort_file(imperfect)
28
29 benchmark.pedantic(sort_file, iterations=10, rounds=100)
30 assert imperfect.read_text() == fixed_content
31
32
33 def test_sort_file_in_place(benchmark: BenchmarkFixture, imperfect: Path) -> None:
34 def sort_file() -> None:
35 api.sort_file(imperfect, overwrite_in_place=True)
36
37 benchmark.pedantic(sort_file, iterations=10, rounds=100)
38 assert imperfect.read_text() == fixed_content