Files
hermes-agent/tests/utils/test_fast_safe_load.py
alt-glitch a1bbd329ea refactor: split utils.py into the utils package
utils.py held three subjects in one 871-line file. It is now a package with
one module per subject: utils/files.py (atomic writes, temp files, file mode
and owner, signatures and digests, JSON and YAML file helpers), utils/urls.py
(the base_url helpers) and utils/env.py (truthy values, env numbers, proxy
env helpers). utils/__init__.py re-exports every public name, so no importer
changes. Function bodies are unchanged, no function-level import moved, and
the logger keeps the name "utils".

Tests patch a name in the module that now defines it. Patches stay on the
package where production reads the name through a function-level
"from utils import". The tests of utils itself move to tests/utils/.

A root module that becomes a folder drops out of anything that copies root
*.py files plus a fixed list of packages, so the fixtures that build a partial
source tree now list utils, as do pyproject's packages.find, three check
scripts and the CI change classifier.
2026-11-16 17:16:20 +05:30

50 lines
1.5 KiB
Python

"""Invariants for utils.fast_safe_load.
fast_safe_load is a drop-in for yaml.safe_load that prefers the libyaml
CSafeLoader C extension for speed. These tests assert the behavior contract
(it parses identically to safe_load across input shapes), not a snapshot of
any particular document.
"""
import io
import hermes_yaml as yaml
from utils import fast_safe_load
_DOCS = [
"", # empty document -> None
"a: 1\nb: two\nc: 3.5\n",
"list: [1, 2, 3]\nnested:\n k: v\n flag: true\n empty: null\n",
"name: skill-x\nmetadata:\n hermes:\n tags: [alpha, beta]\n category: devops\n",
"- one\n- two\n- three\n", # top-level sequence
"scalar string", # bare scalar
]
def test_equivalent_to_safe_load_for_strings():
for doc in _DOCS:
assert fast_safe_load(doc) == yaml.safe_load(doc), repr(doc)
def test_equivalent_to_safe_load_for_file_objects():
for doc in _DOCS:
assert fast_safe_load(io.StringIO(doc)) == yaml.safe_load(io.StringIO(doc)), repr(doc)
def test_rejects_arbitrary_python_objects_like_safe_load():
# Safe loaders must not construct arbitrary Python objects. This tag is
# accepted by the unsafe Loader but rejected by Safe/CSafe loaders.
dangerous = "!!python/object/apply:os.system ['echo pwned']\n"
try:
fast_safe_load(dangerous)
raised = False
except yaml.YAMLError:
raised = True
assert raised, "fast_safe_load must reject python/object tags like safe_load"