Skip to content

Commit 7d00bc1

Browse files
authored
Merge pull request #24 from roskakori/23-fix-po-path-must-be-a-path
#23 Fix `AssertionError`: po_path must be a Path
2 parents 7057a25 + 956419b commit 7d00bc1

File tree

6 files changed

+70
-42
lines changed

6 files changed

+70
-42
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
Version 0.2.5, 2025-04-29
4+
5+
- Fix `AssertionError`: po_path must be a Path (#23)
6+
37
Version 0.2.4, 2025-04-29
48

59
- Fix internal handling of default command line arguments (#21)

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ uv run pytest
3030
2. Test and build the distribution archives:
3131
```bash
3232
$ uv run pytest
33+
$ rm -rf dist
3334
$ uv build
3435
```
3536
3. Tag a release (simply replace 0.x.x with the current version number):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "sanpo"
3-
version = "0.2.4"
3+
version = "0.2.5"
44
description = "Sanitize PO files from gettext for version control"
55
authors = [{ name = "Thomas Aglassinger", email = "[email protected]" }]
66
requires-python = ">=3.9, <4"

sanpo/sanitize.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
from collections.abc import Iterable
66
from pathlib import Path
7+
from typing import Union
78

89
_LINE_PATTERNS_TO_REMOVE = [
910
re.compile(line)
@@ -22,16 +23,24 @@
2223
_log = logging.getLogger("sanpo")
2324

2425

25-
def sanitize_file(po_path: Path):
26-
assert isinstance(po_path, Path), f"po_path must be a Path: {po_path!r}"
27-
_log.info("sanitizing %s", po_path)
28-
with po_path.open(encoding="utf-8") as po_file:
26+
def sanitize_file(po_path: Union[Path, str]):
27+
actual_po_path = path_from(po_path)
28+
_log.info("sanitizing %s", actual_po_path)
29+
with actual_po_path.open(encoding="utf-8") as po_file:
2930
lines_to_write = list(sanitized_lines(po_file))
30-
with po_path.open("w", encoding="utf-8") as po_file:
31+
with actual_po_path.open("w", encoding="utf-8") as po_file:
3132
for line_to_write in lines_to_write:
3233
po_file.write(line_to_write)
3334

3435

36+
def path_from(po_path: Union[Path, str]) -> Path:
37+
if isinstance(po_path, Path):
38+
return po_path
39+
if isinstance(po_path, str):
40+
return Path(po_path)
41+
raise TypeError(f"po_path must be a Path or str, but is {type(po_path)}: {po_path!r}")
42+
43+
3544
def sanitized_lines(source_lines: Iterable[str]) -> Iterable[str]:
3645
for line in source_lines:
3746
line_has_to_be_removed = any(

tests/test_command.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def test_can_show_version(self):
2121
def test_can_sanitize_single_file(self):
2222
self.write_po_file(self.test_can_sanitize_single_file.__name__)
2323
initial_po_lines = self.po_lines()
24-
self.assertEqual(main_without_logging_setup([self.po_path]), 0)
24+
assert main_without_logging_setup([self.po_path]) == 0
2525
sanitized_po_lines = self.po_lines()
26-
self.assertNotEqual(initial_po_lines, sanitized_po_lines)
26+
assert initial_po_lines != sanitized_po_lines
2727

2828
def test_can_sanitize_multiple_files(self):
2929
po_path_to_sanitized_po_lines_map = {}
@@ -35,24 +35,26 @@ def test_can_sanitize_multiple_files(self):
3535
po_path_to_sanitized_po_lines_map[self.po_path] = self.po_lines()
3636

3737
po_paths_to_sanitize = list(po_path_to_sanitized_po_lines_map.keys())
38-
self.assertEqual(main_without_logging_setup(po_paths_to_sanitize), 0)
38+
assert main_without_logging_setup(po_paths_to_sanitize) == 0
3939

4040
for po_path, initial_po_lines in po_path_to_sanitized_po_lines_map.items():
4141
sanitized_po_lines = self.po_lines(po_path)
42-
self.assertNotEqual(sanitized_po_lines, initial_po_lines)
42+
assert sanitized_po_lines != initial_po_lines
4343

4444
def test_can_sanitize_none_arguments(self):
4545
self.write_po_file(self.test_can_sanitize_none_arguments.__name__)
4646
with patch("sys.argv", ["sanpo", str(self.po_path)]):
4747
assert main_without_logging_setup() == 0
4848

49-
def test_fails_on_non_existent_po_file(self):
50-
self.assertEqual(main_without_logging_setup(["no_such.po"]), 1)
5149

52-
def test_fails_on_no_po_files(self):
53-
with (
54-
pytest.raises(SystemExit) as error,
55-
patch("sys.argv", ["sanpo"]),
56-
):
57-
main_without_logging_setup()
58-
assert error.value.code == 2
50+
def test_fails_on_non_existent_po_file():
51+
assert main_without_logging_setup(["no_such.po"]) == 1
52+
53+
54+
def test_fails_on_no_po_files():
55+
with (
56+
pytest.raises(SystemExit) as error,
57+
patch("sys.argv", ["sanpo"]),
58+
):
59+
main_without_logging_setup()
60+
assert error.value.code == 2

tests/test_sanitize.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# Copyright (c) 2021, Thomas Aglassinger
22
# All rights reserved. Distributed under the BSD 3-Clause License.
3-
from unittest import TestCase
3+
import pytest
44

55
from sanpo.sanitize import sanitize_file, sanitized_lines
66
from tests._common import PoFileTest
77

88

9-
class SanitizationTest(TestCase):
10-
def test_can_remove_pointless_lines(self):
11-
source_lines = [
12-
r'"Project-Id-Version: PACKAGE VERSION\n"',
13-
r'"Report-Msgid-Bugs-To: \n"',
14-
r'"POT-Creation-Date: 2021-01-01\\n"',
15-
r'"PO-Revision-Date: 2021-01-01\\n"',
16-
r'"Last-Translator: FULL NAME EMAIL@ADDRESS\n"',
17-
r'"Language-Team: LANGUAGE [email protected]\n"',
18-
"PRESERVE",
19-
]
20-
self.assertEqual(list(sanitized_lines(source_lines)), ["PRESERVE"])
21-
22-
def test_can_preserve_metadata_lines(self):
23-
source_lines = [
24-
r'"Project-Id-Version: some 1.2.3\n"',
25-
r'"Report-Msgid-Bugs-To: [email protected]\n"',
26-
r'"Last-Translator: John Doe [email protected]\n"',
27-
r'"Language-Team: en [email protected]\n"',
28-
]
29-
self.assertEqual(list(sanitized_lines(source_lines)), source_lines)
9+
def test_can_remove_pointless_lines():
10+
source_lines = [
11+
r'"Project-Id-Version: PACKAGE VERSION\n"',
12+
r'"Report-Msgid-Bugs-To: \n"',
13+
r'"POT-Creation-Date: 2021-01-01\\n"',
14+
r'"PO-Revision-Date: 2021-01-01\\n"',
15+
r'"Last-Translator: FULL NAME EMAIL@ADDRESS\n"',
16+
r'"Language-Team: LANGUAGE [email protected]\n"',
17+
"PRESERVE",
18+
]
19+
assert list(sanitized_lines(source_lines)) == ["PRESERVE"]
20+
21+
22+
def test_can_preserve_metadata_lines():
23+
source_lines = [
24+
r'"Project-Id-Version: some 1.2.3\n"',
25+
r'"Report-Msgid-Bugs-To: [email protected]\n"',
26+
r'"Last-Translator: John Doe [email protected]\n"',
27+
r'"Language-Team: en [email protected]\n"',
28+
]
29+
assert list(sanitized_lines(source_lines)) == source_lines
3030

3131

3232
class TransformFileTest(PoFileTest):
@@ -35,4 +35,16 @@ def test_can_remove_pointless_lines_in_file(self):
3535
initial_po_lines = self.po_lines()
3636
sanitize_file(self.po_path)
3737
transformed_po_lines = self.po_lines()
38-
self.assertNotEqual(initial_po_lines, transformed_po_lines)
38+
assert initial_po_lines != transformed_po_lines
39+
40+
def test_can_sanitize_str_path(self):
41+
self.write_po_file(self.test_can_sanitize_str_path.__name__)
42+
initial_po_lines = self.po_lines()
43+
sanitize_file(str(self.po_path))
44+
transformed_po_lines = self.po_lines()
45+
assert initial_po_lines != transformed_po_lines
46+
47+
48+
def test_fails_on_int_po_file():
49+
with pytest.raises(TypeError, match="po_path must be a Path or str, but is <class 'int'>: 123"):
50+
sanitize_file(123)

0 commit comments

Comments
 (0)