tenseleyflow/documentlanguagemodel / 0308fce

Browse files

Stabilize CLI output tests

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
0308fce10f22c0e8f43b22d1dfa15e88c38c7822
Parents
664a49d
Tree
688e6e4

3 changed files

StatusFile+-
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 @@
33
 from __future__ import annotations
44
 
55
 import json
6
+import re
67
 from pathlib import Path
78
 
89
 from typer.testing import CliRunner
@@ -12,6 +13,7 @@ from dlm.cli.app import app
1213
 _FRONTMATTER = (
1314
     "---\ndlm_id: 01KPQ9X1000000000000000000\ndlm_version: 7\nbase_model: smollm2-135m\n---\n"
1415
 )
16
+_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]")
1517
 
1618
 
1719
 def _write_dlm(path: Path, body: str = "") -> None:
@@ -33,6 +35,11 @@ def _write_sway(path: Path, probes: list[dict]) -> None:
3335
     )
3436
 
3537
 
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
+
3643
 _FAIL_WITH_REF = {
3744
     "name": "dgemm_semantics",
3845
     "kind": "section_internalization",
@@ -120,7 +127,7 @@ class TestHarvestCmd:
120127
             app, ["--home", str(tmp_path), "harvest", str(doc), "--sway-json", str(sway)]
121128
         )
122129
         assert result.exit_code == 1, result.output
123
-        assert "not valid JSON" in result.output
130
+        assert "not valid JSON" in _normalized_output(result)
124131
 
125132
     def test_missing_reference_strict_exit_1(self, tmp_path: Path) -> None:
126133
         doc = tmp_path / "doc.dlm"
tests/unit/cli/test_train_no_cache_flag.pymodified
@@ -3,12 +3,15 @@
33
 from __future__ import annotations
44
 
55
 import os
6
+import re
67
 from pathlib import Path
78
 
89
 from typer.testing import CliRunner
910
 
1011
 from dlm.cli.app import app
1112
 
13
+_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]")
14
+
1215
 
1316
 def _write_minimal_dlm(path: Path) -> None:
1417
     path.write_text(
@@ -23,6 +26,11 @@ def _write_minimal_dlm(path: Path) -> None:
2326
     )
2427
 
2528
 
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
+
2634
 def test_no_cache_flag_sets_env_var(tmp_path: Path) -> None:
2735
     """The flag sets ``DLM_DISABLE_TOKENIZED_CACHE=1`` early enough
2836
     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:
8088
     runner = CliRunner()
8189
     result = runner.invoke(app, ["train", "--help"])
8290
     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()
8594
 
8695
 
8796
 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.
99
 
1010
 from __future__ import annotations
1111
 
12
+import re
1213
 from pathlib import Path
1314
 from typing import Any
1415
 
@@ -17,6 +18,8 @@ from typer.testing import CliRunner
1718
 
1819
 from dlm.cli.app import app
1920
 
21
+_ANSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]")
22
+
2023
 
2124
 def _write_minimal_dlm(path: Path) -> None:
2225
     path.write_text(
@@ -31,6 +34,11 @@ def _write_minimal_dlm(path: Path) -> None:
3134
     )
3235
 
3336
 
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
+
3442
 class _ResolveCaptureError(Exception):
3543
     """Sentinel raised after capturing the kwargs of resolve_base_model.
3644
 
@@ -94,4 +102,4 @@ class TestTrainSkipExportProbes:
94102
         runner = CliRunner()
95103
         result = runner.invoke(app, ["train", "--help"])
96104
         assert result.exit_code == 0
97
-        assert "--skip-export-probes" in result.output
105
+        assert "--skip-export-probes" in _normalized_output(result)