Skip to content

Commit 18d48cb

Browse files
authored
Exercise real CLI file-type parsing and CU result formatting (#2592)
1 parent 64e3d67 commit 18d48cb

2 files changed

Lines changed: 202 additions & 137 deletions

File tree

‎packages/markitdown/tests/test_cli_misc.py‎

Lines changed: 97 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,37 +116,103 @@ def test_use_cu_and_use_docintel_mutually_exclusive(self):
116116
"--use-cu/--use-content-understanding"
117117
) in result.stderr, result.stderr
118118

119-
def test_cu_file_types_parsing(self):
120-
"""--cu-file-types should parse comma-separated values into enum list."""
121-
from markitdown.converters import ContentUnderstandingFileType
122-
123-
raw = "pdf,jpeg,mp4"
124-
type_names = [t.strip().lower() for t in raw.split(",") if t.strip()]
125-
cu_types = [ContentUnderstandingFileType(name) for name in type_names]
126-
127-
assert cu_types == [
128-
ContentUnderstandingFileType.PDF,
129-
ContentUnderstandingFileType.JPEG,
130-
ContentUnderstandingFileType.MP4,
131-
]
132-
133-
def test_cu_file_types_invalid_value(self):
134-
"""Unknown file type name should raise ValueError."""
135-
from markitdown.converters import ContentUnderstandingFileType
136-
137-
with pytest.raises(ValueError):
138-
ContentUnderstandingFileType("nonsense")
139-
140-
def test_cu_file_types_single_value(self):
141-
"""Single file type (no comma) should parse correctly."""
142-
from markitdown.converters import ContentUnderstandingFileType
143-
144-
cu_types = [
145-
ContentUnderstandingFileType(t.strip().lower())
146-
for t in "wav".split(",")
147-
if t.strip()
148-
]
149-
assert cu_types == [ContentUnderstandingFileType.WAV]
119+
@pytest.mark.parametrize("raw", ["pdf,jpeg,wav", " , PDF, JPEG,, WAV, "])
120+
@pytest.mark.parametrize(
121+
"filename, content_type, analyzer",
122+
[
123+
("test.pdf", "application/pdf", "prebuilt-documentSearch"),
124+
("test.jpg", "image/jpeg", "prebuilt-documentSearch"),
125+
("test.wav", "audio/wav", "prebuilt-audioSearch"),
126+
],
127+
)
128+
def test_cu_file_types_parsing(
129+
self, monkeypatch, capsys, raw, filename, content_type, analyzer
130+
):
131+
self._check_cu_file_types(
132+
monkeypatch, capsys, raw, filename, content_type, analyzer
133+
)
134+
135+
def test_cu_file_types_single_value(self, monkeypatch, capsys):
136+
self._check_cu_file_types(
137+
monkeypatch, capsys, "wav", "test.wav", "audio/wav", "prebuilt-audioSearch"
138+
)
139+
140+
def _check_cu_file_types(
141+
self, monkeypatch, capsys, raw, filename, content_type, analyzer
142+
):
143+
from azure.ai.contentunderstanding.models import AnalysisResult, DocumentContent
144+
from markitdown.converters import _cu_converter
145+
146+
client = MagicMock()
147+
client.begin_analyze_binary.return_value.result.return_value = AnalysisResult(
148+
contents=[DocumentContent(kind="document", markdown="# Parsed CLI input")]
149+
)
150+
client_cls = MagicMock(return_value=client)
151+
monkeypatch.setattr(_cu_converter, "ContentUnderstandingClient", client_cls)
152+
monkeypatch.setenv("AZURE_API_KEY", "test-key")
153+
path = os.path.join(os.path.dirname(__file__), "test_files", filename)
154+
monkeypatch.setattr(
155+
sys,
156+
"argv",
157+
[
158+
"markitdown",
159+
"--use-cu",
160+
"--cu-endpoint",
161+
"https://fake-cu",
162+
"--cu-file-types",
163+
raw,
164+
path,
165+
],
166+
)
167+
168+
main()
169+
170+
client_cls.assert_called_once()
171+
client.begin_analyze_binary.assert_called_once()
172+
request = client.begin_analyze_binary.call_args.kwargs
173+
assert request["analyzer_id"] == analyzer
174+
assert request["content_type"] == content_type
175+
assert "# Parsed CLI input" in capsys.readouterr().out
176+
177+
# An excluded format must use its local converter, not the CU service.
178+
# This also catches a CLI that silently ignores --cu-file-types.
179+
client.begin_analyze_binary.reset_mock()
180+
html_path = os.path.join(
181+
os.path.dirname(__file__), "test_files", "test_blog.html"
182+
)
183+
monkeypatch.setattr(sys, "argv", sys.argv[:-1] + [html_path])
184+
main()
185+
client.begin_analyze_binary.assert_not_called()
186+
output = capsys.readouterr().out
187+
assert "Large language models" in output
188+
assert "# Parsed CLI input" not in output
189+
190+
@pytest.mark.parametrize("raw", ["nonsense", "pdf,nonsense"])
191+
def test_cu_file_types_invalid_value(self, monkeypatch, capsys, raw):
192+
from markitdown.converters import _cu_converter
193+
194+
client_cls = MagicMock()
195+
monkeypatch.setattr(_cu_converter, "ContentUnderstandingClient", client_cls)
196+
monkeypatch.setattr(
197+
sys,
198+
"argv",
199+
[
200+
"markitdown",
201+
"--use-cu",
202+
"--cu-endpoint",
203+
"https://fake-cu",
204+
"--cu-file-types",
205+
raw,
206+
"unused.pdf",
207+
],
208+
)
209+
210+
with pytest.raises(SystemExit) as exc:
211+
main()
212+
213+
assert exc.value.code == 1
214+
assert "Unknown file type: nonsense" in capsys.readouterr().out
215+
client_cls.assert_not_called()
150216

151217
def test_use_cu_wires_kwargs_to_markitdown(self, capsys):
152218
"""--use-cu should pass CU options through to MarkItDown."""

0 commit comments

Comments
 (0)