|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
1 | 5 | import pytest |
2 | 6 | from reqstool_python_decorators.decorators.decorators import SVCs |
3 | 7 | from reqstool_python_decorators.processors.decorator_processor import DecoratorProcessor |
@@ -325,3 +329,63 @@ def test_process_decorated_data_no_state_accumulation(process_decorator_instance |
325 | 329 | # Should have exactly 1 entry, not 2 due to state accumulation |
326 | 330 | entries = data["requirement_annotations"]["implementations"]["REQ_001"] |
327 | 331 | assert len(entries) == 1 |
| 332 | + |
| 333 | + |
| 334 | +# --------------------------------------------------------------------------- |
| 335 | +# non-ASCII sources / platform locale encoding |
| 336 | +# --------------------------------------------------------------------------- |
| 337 | + |
| 338 | +# "Đ" is C4 90 in UTF-8; 0x90 is undefined in cp1252, so a locale-decoded read fails on Windows, |
| 339 | +# and any non-ASCII byte fails under an ASCII locale on Linux and macOS. |
| 340 | +_NON_ASCII_SOURCE = '# Đ non-ASCII comment\n@Requirements("REQ_001")\ndef my_func():\n """Đ"""\n' |
| 341 | + |
| 342 | +_RUN_PROCESSOR = ( |
| 343 | + "import sys\n" |
| 344 | + "from reqstool_python_decorators.processors.decorator_processor import DecoratorProcessor\n" |
| 345 | + "DecoratorProcessor().process_decorated_data([sys.argv[1]], output_file=sys.argv[2])\n" |
| 346 | +) |
| 347 | + |
| 348 | + |
| 349 | +@SVCs("SVC_DECORATORS_002", "SVC_DECORATORS_003") |
| 350 | +def test_process_decorated_data_non_ascii_source_with_non_utf8_locale(tmp_path): |
| 351 | + src_dir = tmp_path / "src" |
| 352 | + src_dir.mkdir() |
| 353 | + (src_dir / "mod.py").write_bytes(_NON_ASCII_SOURCE.encode("utf-8")) |
| 354 | + output_file = tmp_path / "out" / "annotations.yml" |
| 355 | + |
| 356 | + # Run in a child process with UTF-8 mode off and a legacy locale to mimic a default Windows setup. |
| 357 | + env = { |
| 358 | + **os.environ, |
| 359 | + "PYTHONUTF8": "0", |
| 360 | + "PYTHONCOERCECLOCALE": "0", |
| 361 | + "PYTHONIOENCODING": "utf-8", |
| 362 | + "LC_ALL": "C", |
| 363 | + "LANG": "C", |
| 364 | + } |
| 365 | + result = subprocess.run( |
| 366 | + [sys.executable, "-c", _RUN_PROCESSOR, str(src_dir), str(output_file)], |
| 367 | + capture_output=True, |
| 368 | + text=True, |
| 369 | + env=env, |
| 370 | + ) |
| 371 | + |
| 372 | + assert result.returncode == 0, result.stderr |
| 373 | + data = YAML().load(output_file.read_text(encoding="utf-8")) |
| 374 | + assert [ |
| 375 | + e["fullyQualifiedName"].rsplit(".", 1)[-1] |
| 376 | + for e in data["requirement_annotations"]["implementations"]["REQ_001"] |
| 377 | + ] == ["my_func"] |
| 378 | + |
| 379 | + |
| 380 | +@SVCs("SVC_DECORATORS_002") |
| 381 | +def test_get_functions_and_classes_honours_pep263_encoding_declaration( |
| 382 | + process_decorator_instance: DecoratorProcessor, tmp_path |
| 383 | +): |
| 384 | + src_file = tmp_path / "latin1_mod.py" |
| 385 | + src_file.write_bytes( |
| 386 | + '# -*- coding: latin-1 -*-\n# é\n@Requirements("REQ_001")\ndef my_func():\n pass\n'.encode("latin-1") |
| 387 | + ) |
| 388 | + |
| 389 | + process_decorator_instance.get_functions_and_classes(src_file, ["Requirements"]) |
| 390 | + |
| 391 | + assert [r["name"] for r in process_decorator_instance.req_svc_results] == ["my_func"] |
0 commit comments