Stabilize CLI output tests
Authored by
mfwolffe <wolffemf@dukes.jmu.edu>
- SHA
0308fce10f22c0e8f43b22d1dfa15e88c38c7822- Parents
-
664a49d - Tree
688e6e4
0308fce
0308fce10f22c0e8f43b22d1dfa15e88c38c7822664a49d
688e6e4| Status | File | + | - |
|---|---|---|---|
| M |
tests/unit/cli/test_harvest_cmd.py
|
8 | 1 |
| M |
tests/unit/cli/test_train_no_cache_flag.py
|
11 | 2 |
| M |
tests/unit/cli/test_train_skip_export_probes.py
|
9 | 1 |
tests/unit/cli/test_harvest_cmd.pymodified@@ -3,6 +3,7 @@ | ||
| 3 | 3 | from __future__ import annotations |
| 4 | 4 | |
| 5 | 5 | import json |
| 6 | +import re | |
| 6 | 7 | from pathlib import Path |
| 7 | 8 | |
| 8 | 9 | from typer.testing import CliRunner |
@@ -12,6 +13,7 @@ from dlm.cli.app import app | ||
| 12 | 13 | _FRONTMATTER = ( |
| 13 | 14 | "---\ndlm_id: 01KPQ9X1000000000000000000\ndlm_version: 7\nbase_model: smollm2-135m\n---\n" |
| 14 | 15 | ) |
| 16 | +_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]") | |
| 15 | 17 | |
| 16 | 18 | |
| 17 | 19 | def _write_dlm(path: Path, body: str = "") -> None: |
@@ -33,6 +35,11 @@ def _write_sway(path: Path, probes: list[dict]) -> None: | ||
| 33 | 35 | ) |
| 34 | 36 | |
| 35 | 37 | |
| 38 | +def _normalized_output(result: object) -> str: | |
| 39 | + text = getattr(result, "output", "") + getattr(result, "stderr", "") | |
| 40 | + return " ".join(_ANSI_RE.sub("", text).split()) | |
| 41 | + | |
| 42 | + | |
| 36 | 43 | _FAIL_WITH_REF = { |
| 37 | 44 | "name": "dgemm_semantics", |
| 38 | 45 | "kind": "section_internalization", |
@@ -120,7 +127,7 @@ class TestHarvestCmd: | ||
| 120 | 127 | app, ["--home", str(tmp_path), "harvest", str(doc), "--sway-json", str(sway)] |
| 121 | 128 | ) |
| 122 | 129 | assert result.exit_code == 1, result.output |
| 123 | - assert "not valid JSON" in result.output | |
| 130 | + assert "not valid JSON" in _normalized_output(result) | |
| 124 | 131 | |
| 125 | 132 | def test_missing_reference_strict_exit_1(self, tmp_path: Path) -> None: |
| 126 | 133 | doc = tmp_path / "doc.dlm" |
tests/unit/cli/test_train_no_cache_flag.pymodified@@ -3,12 +3,15 @@ | ||
| 3 | 3 | from __future__ import annotations |
| 4 | 4 | |
| 5 | 5 | import os |
| 6 | +import re | |
| 6 | 7 | from pathlib import Path |
| 7 | 8 | |
| 8 | 9 | from typer.testing import CliRunner |
| 9 | 10 | |
| 10 | 11 | from dlm.cli.app import app |
| 11 | 12 | |
| 13 | +_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]") | |
| 14 | + | |
| 12 | 15 | |
| 13 | 16 | def _write_minimal_dlm(path: Path) -> None: |
| 14 | 17 | path.write_text( |
@@ -23,6 +26,11 @@ def _write_minimal_dlm(path: Path) -> None: | ||
| 23 | 26 | ) |
| 24 | 27 | |
| 25 | 28 | |
| 29 | +def _normalized_output(result: object) -> str: | |
| 30 | + text = getattr(result, "output", "") + getattr(result, "stderr", "") | |
| 31 | + return " ".join(_ANSI_RE.sub("", text).split()) | |
| 32 | + | |
| 33 | + | |
| 26 | 34 | def test_no_cache_flag_sets_env_var(tmp_path: Path) -> None: |
| 27 | 35 | """The flag sets ``DLM_DISABLE_TOKENIZED_CACHE=1`` early enough |
| 28 | 36 | that the trainer's pre-tokenize helper sees it. The flag is a |
@@ -80,8 +88,9 @@ def test_no_cache_flag_help_text(tmp_path: Path) -> None: | ||
| 80 | 88 | runner = CliRunner() |
| 81 | 89 | result = runner.invoke(app, ["train", "--help"]) |
| 82 | 90 | assert result.exit_code == 0 |
| 83 | - assert "--no-cache" in result.output | |
| 84 | - assert "tokenized" in result.output.lower() | |
| 91 | + normalized = _normalized_output(result) | |
| 92 | + assert "--no-cache" in normalized | |
| 93 | + assert "tokenized" in normalized.lower() | |
| 85 | 94 | |
| 86 | 95 | |
| 87 | 96 | def test_no_cache_absent_leaves_env_unset(tmp_path: Path) -> None: |
tests/unit/cli/test_train_skip_export_probes.pymodified@@ -9,6 +9,7 @@ forwarding in. | ||
| 9 | 9 | |
| 10 | 10 | from __future__ import annotations |
| 11 | 11 | |
| 12 | +import re | |
| 12 | 13 | from pathlib import Path |
| 13 | 14 | from typing import Any |
| 14 | 15 | |
@@ -17,6 +18,8 @@ from typer.testing import CliRunner | ||
| 17 | 18 | |
| 18 | 19 | from dlm.cli.app import app |
| 19 | 20 | |
| 21 | +_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]") | |
| 22 | + | |
| 20 | 23 | |
| 21 | 24 | def _write_minimal_dlm(path: Path) -> None: |
| 22 | 25 | path.write_text( |
@@ -31,6 +34,11 @@ def _write_minimal_dlm(path: Path) -> None: | ||
| 31 | 34 | ) |
| 32 | 35 | |
| 33 | 36 | |
| 37 | +def _normalized_output(result: object) -> str: | |
| 38 | + text = getattr(result, "output", "") + getattr(result, "stderr", "") | |
| 39 | + return " ".join(_ANSI_RE.sub("", text).split()) | |
| 40 | + | |
| 41 | + | |
| 34 | 42 | class _ResolveCaptureError(Exception): |
| 35 | 43 | """Sentinel raised after capturing the kwargs of resolve_base_model. |
| 36 | 44 | |
@@ -94,4 +102,4 @@ class TestTrainSkipExportProbes: | ||
| 94 | 102 | runner = CliRunner() |
| 95 | 103 | result = runner.invoke(app, ["train", "--help"]) |
| 96 | 104 | assert result.exit_code == 0 |
| 97 | - assert "--skip-export-probes" in result.output | |
| 105 | + assert "--skip-export-probes" in _normalized_output(result) | |