Skip to content

Commit f2f769c

Browse files
authored
run python tests in CI (#489)
* run python tests in CI * use pytest fixture to make console output tests portable * use Path-type instead of str for storing paths to Rules-files.
1 parent ead8963 commit f2f769c

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

.github/workflows/python.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Python
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
test:
11+
name: Python Tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: astral-sh/setup-uv@v6
16+
- name: Run tests
17+
working-directory: PythonScripts
18+
run: uv run pytest

PythonScripts/audit_translations/auditor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ def get_rules_dir(rules_dir: Optional[str] = None) -> Path:
4444
return package_dir.parent.parent / "Rules" / "Languages"
4545

4646

47-
def get_yaml_files(lang_dir: Path, region_dir: Optional[Path] = None) -> List[str]:
47+
def get_yaml_files(lang_dir: Path, region_dir: Optional[Path] = None) -> List[Path]:
4848
"""Get all YAML files to audit for a language, including region overrides."""
49-
files: set[str] = set()
49+
files: set[Path] = set()
5050

5151
def collect_from(directory: Path, root: Path) -> None:
5252
if not directory.exists():
5353
return
5454
for f in directory.glob("*.yaml"):
5555
if f.name != "prefs.yaml": # Skip prefs.yaml as it's not translated
56-
files.add(str(f.relative_to(root)))
56+
files.add(f.relative_to(root))
5757
shared_dir = directory / "SharedRules"
5858
if shared_dir.exists():
5959
for f in shared_dir.glob("*.yaml"):
60-
files.add(str(f.relative_to(root)))
60+
files.add(f.relative_to(root))
6161

6262
collect_from(lang_dir, lang_dir)
6363
if region_dir:
@@ -177,7 +177,7 @@ def print_diff_item(diff: RuleDifference, line_en: int, line_tr: int, verbose: b
177177
def issue_base(rule: RuleInfo, file_name: str, language: str) -> dict:
178178
return {
179179
"language": language,
180-
"file": file_name,
180+
"file": Path(file_name).as_posix(),
181181
"rule_name": rule.name or "",
182182
"rule_tag": rule.tag or "",
183183
"rule_key": rule.key,

PythonScripts/audit_translations/tests/test_auditor.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44

55
from pathlib import Path
66

7+
import pytest
8+
79
from ..auditor import collect_issues, compare_files, console, get_yaml_files, list_languages, print_warnings
810
from ..dataclasses import ComparisonResult, RuleDifference, RuleInfo
911

1012

13+
@pytest.fixture()
14+
def fixed_console_width():
15+
"""Pin Rich console to 80 columns so golden-file comparisons are portable."""
16+
old = console.width
17+
console.width = 80
18+
yield
19+
console.width = old
20+
21+
1122
def make_rule(name: str, tag: str, line: int, raw: str) -> RuleInfo:
1223
return RuleInfo(
1324
name=name,
@@ -155,7 +166,7 @@ def test_get_yaml_files_includes_region(tmp_path) -> None:
155166
(region_dir / "unicode.yaml").write_text("---", encoding="utf-8")
156167

157168
files = get_yaml_files(lang_dir, region_dir)
158-
assert set(files) == {"base.yaml", "SharedRules/shared.yaml", "unicode.yaml"}
169+
assert set(files) == {Path("base.yaml"), Path("SharedRules/shared.yaml"), Path("unicode.yaml")}
159170

160171

161172
def test_list_languages_includes_region_codes(tmp_path) -> None:
@@ -183,9 +194,11 @@ def test_list_languages_includes_region_codes(tmp_path) -> None:
183194
assert "zz-aa" in output
184195

185196

186-
def test_print_warnings_omits_snippets_when_not_verbose() -> None:
197+
def test_print_warnings_omits_snippets_when_not_verbose(fixed_console_width) -> None:
187198
"""
188199
Ensure the print_warnings output matches the non-verbose golden snapshot.
200+
201+
Uses pytest fixture for console width.
189202
"""
190203
base_dir = Path(__file__).parent
191204
fixtures_dir = base_dir / "fixtures"
@@ -202,9 +215,11 @@ def test_print_warnings_omits_snippets_when_not_verbose() -> None:
202215
assert output == golden_path.read_text(encoding="utf-8")
203216

204217

205-
def test_print_warnings_includes_snippets_when_verbose() -> None:
218+
def test_print_warnings_includes_snippets_when_verbose(fixed_console_width) -> None:
206219
"""
207220
Ensure the print_warnings output matches the verbose golden snapshot.
221+
222+
Uses pytest fixture for console width.
208223
"""
209224
base_dir = Path(__file__).parent
210225
fixtures_dir = base_dir / "fixtures"

0 commit comments

Comments
 (0)