Skip to content

Tests: harden ruamel detection and gate fixture generators#3362

Open
a-v-popov wants to merge 1 commit intoipspace:ruamel-testsfrom
a-v-popov:ruamel-tests-followup
Open

Tests: harden ruamel detection and gate fixture generators#3362
a-v-popov wants to merge 1 commit intoipspace:ruamel-testsfrom
a-v-popov:ruamel-tests-followup

Conversation

@a-v-popov
Copy link
Copy Markdown
Collaborator

A few items I'd suggest addressing before merging PR #3345, plus two small follow-ups:

1. The detection probe matches the namespace shim, not the library

tests/utils.py does import ruamel, but ruamel is a PEP 420 namespace package — it can survive uninstalls of ruamel.yaml (the ruamel/ directory often lingers, or another package owns part of the namespace). On my system right now:

>>> import ruamel
>>> import ruamel.yaml
ModuleNotFoundError: No module named 'ruamel.yaml'

HAS_RUAMEL becomes True while Box/PyYAML disagree, and clean_ruamel runs for nothing. python-box itself probes with from ruamel.yaml import version_info, YAML (box/converters.py:22). Suggested:

try:
  import ruamel.yaml  # noqa: F401
  HAS_RUAMEL = True
except ImportError:
  HAS_RUAMEL = False

2. yaml.dump should pin default_flow_style=False

Box.to_yaml(...) in python-box 7.x defaults to default_flow_style=False, width=120 and calls yaml.dump(obj, default_flow_style=default_flow_style, width=width, ...) (box/converters.py:195, box/box.py:1018). The PR replaces it with:

return yaml.dump(topology.to_dict(), width=120)

PyYAML's own default for default_flow_style is None ("auto: flow for inner scalar-only collections"), not False. Today's fixtures are all under-mappings, so the two modes coincide and the suite passes. The next fixture with a top-level scalar list, or a deeply nested scalar-only structure, will silently diverge from the format every committed expected/*.yml was generated in. One-line fix:

return yaml.dump(topology.to_dict(), default_flow_style=False, width=120)

3. log._ERROR_LOG = [] is redundant

The previous line was log.err_count = 0, which was a typo from the start — log.err_count doesn't exist; the public counter is get_error_count() at netsim/utils/log.py:389. The new log._ERROR_LOG = [] does target a real attribute, but run_test immediately calls log.init_log_system(header=False), which itself does _ERROR_LOG = [] (log.py:539). The reset is redundant, and reaching into a leading-underscore symbol from outside the module is a smell. Either drop the line, or call log.init_log_system(header=False) if a defensive reset is desired.

Two small follow-ups worth folding in

  • tests/conftest.py (new): the standard-pytest-safe way to surface "ruamel.yaml is installed, things will be slower" is pytest_configure + config.issue_config_time_warning(UserWarning(...)). It appears in pytest's warnings summary, never fails a run, and doesn't touch collection / -k filtering / parallelism. Import HAS_RUAMEL from tests/utils.py so there's a single source of truth.
  • Gate the fixture generators. The PR docs already say error-log fixtures can't be generated under ruamel, but tests/create-error-tests.sh doesn't enforce it — a user can still run it and commit broken .log files. A 4-line python3 -c "import ruamel.yaml" && exit 2 guard at the top closes that hole. tests/create-transformation-test-case.py does work under ruamel (the clean_ruamel walk handles it), so a stderr advisory there is enough.

Reference implementation of all of the above on a-v-popov/netlab@pr-3353-followup (compare view) — verified end-to-end:

  • Without ruamel.yaml: xform + error tests pass; no warning.
  • With ruamel.yaml 0.19.1: xform + error tests pass (+~40% wall time, matching the "slower" claim); UserWarning appears in pytest's warnings summary; nothing fails or skips.
  • create-error-tests.sh with ruamel.yaml installed: exits 2 with an actionable message before doing anything destructive.
  • ruff check . clean.

* tests/utils.py: probe `ruamel.yaml`, not the bare `ruamel` namespace
  package, which can survive uninstalls and produce false positives.
  Pin `yaml.dump(..., default_flow_style=False, width=120)` to match
  `Box.to_yaml`'s historical defaults so future fixtures with top-level
  scalar lists do not silently diverge from the committed format.
  Drop redundant `global HAS_RUAMEL` and promote the explanatory block
  into a real `clean_ruamel` docstring.
* tests/test_transformation.py: remove the redundant
  `log._ERROR_LOG = []` reset (`init_log_system` already does this
  inside `run_test`) and the trailing whitespace in
  `test_coverage_verbose_cases`.
* tests/conftest.py: surface a `UserWarning` via
  `pytest_configure` / `issue_config_time_warning` when `ruamel.yaml`
  is present, so users see the slowdown advisory in pytest's standard
  warnings summary without breaking collection or pass/fail.
* tests/create-transformation-test-case.py: print a stderr advisory
  before fixture generation when `ruamel.yaml` is installed
  (cleanup path keeps it correct, just slow).
* tests/create-error-tests.sh: hard-fail at the top when
  `ruamel.yaml` is importable, since error-log fixtures cannot be
  generated correctly under ruamel and the docs already warn against
  it; refusing to run prevents broken `.log` files from being
  committed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant