Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/markitdown/src/markitdown/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
import codecs
import io
from typing import Any, Dict
from textwrap import dedent
from importlib.metadata import entry_points
Expand Down Expand Up @@ -248,8 +249,9 @@ def main():
markitdown = MarkItDown(enable_plugins=args.use_plugins)

if args.filename is None:
# Windows pipe-backed stdin can report seekable() even though it cannot rewind.
result = markitdown.convert_stream(
sys.stdin.buffer,
io.BytesIO(sys.stdin.buffer.read()),
stream_info=stream_info,
keep_data_uris=args.keep_data_uris,
)
Expand Down
32 changes: 30 additions & 2 deletions packages/markitdown/tests/test_cli_misc.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#!/usr/bin/env python3 -m pytest
import io
import subprocess
import sys
from types import SimpleNamespace

from markitdown import __version__
from markitdown.__main__ import main

# This file contains CLI tests that are not directly tested by the FileTestVectors.
# This includes things like help messages, version numbers, and invalid flags.


def test_version() -> None:
result = subprocess.run(
["python", "-m", "markitdown", "--version"], capture_output=True, text=True
[sys.executable, "-m", "markitdown", "--version"],
capture_output=True,
text=True,
)

assert result.returncode == 0, f"CLI exited with error: {result.stderr}"
Expand All @@ -17,7 +24,9 @@ def test_version() -> None:

def test_invalid_flag() -> None:
result = subprocess.run(
["python", "-m", "markitdown", "--foobar"], capture_output=True, text=True
[sys.executable, "-m", "markitdown", "--foobar"],
capture_output=True,
text=True,
)

assert result.returncode != 0, f"CLI exited with error: {result.stderr}"
Expand All @@ -27,6 +36,25 @@ def test_invalid_flag() -> None:
assert "SYNTAX" in result.stderr, "Expected 'SYNTAX' to appear in STDERR"


def test_windows_pipe_input_is_buffered_before_conversion(monkeypatch, capsys) -> None:
class WindowsPipe(io.BytesIO):
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
if whence == io.SEEK_END:
return super().seek(offset, whence)
return 0

stdin = SimpleNamespace(
buffer=WindowsPipe(b"<html><body><h1>Test HTML</h1></body></html>")
)
monkeypatch.setattr(sys, "stdin", stdin)
monkeypatch.setattr(sys, "argv", ["markitdown", "-x", "html"])

main()

captured = capsys.readouterr()
assert captured.out.strip() == "# Test HTML"


if __name__ == "__main__":
"""Runs this file's tests from the command line."""
test_version()
Expand Down
21 changes: 13 additions & 8 deletions packages/markitdown/tests/test_cli_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
import subprocess
import locale
import sys
from typing import List

if __name__ == "__main__":
Expand Down Expand Up @@ -46,7 +47,7 @@ def test_output_to_stdout(shared_tmp_dir, test_vector) -> None:

result = subprocess.run(
[
"python",
sys.executable,
"-m",
"markitdown",
os.path.join(TEST_FILES_DIR, test_vector.filename),
Expand All @@ -69,7 +70,7 @@ def test_output_to_file(shared_tmp_dir, test_vector) -> None:
output_file = os.path.join(shared_tmp_dir, test_vector.filename + ".output")
result = subprocess.run(
[
"python",
sys.executable,
"-m",
"markitdown",
"-o",
Expand All @@ -83,7 +84,7 @@ def test_output_to_file(shared_tmp_dir, test_vector) -> None:
assert result.returncode == 0, f"CLI exited with error: {result.stderr}"
assert os.path.exists(output_file), f"Output file not created: {output_file}"

with open(output_file, "r") as f:
with open(output_file, "r", encoding="utf-8") as f:
output_data = f.read()
for test_string in test_vector.must_include:
assert test_string in output_data
Expand All @@ -104,10 +105,9 @@ def test_input_from_stdin_without_hints(shared_tmp_dir, test_vector) -> None:

result = subprocess.run(
[
"python",
sys.executable,
"-m",
"markitdown",
os.path.join(TEST_FILES_DIR, test_vector.filename),
],
input=test_input,
capture_output=True,
Expand Down Expand Up @@ -135,7 +135,12 @@ def test_convert_url(shared_tmp_dir, test_vector):

time.sleep(1) # Ensure we don't hit rate limits
result = subprocess.run(
["python", "-m", "markitdown", TEST_FILES_URL + "/" + test_vector.filename],
[
sys.executable,
"-m",
"markitdown",
TEST_FILES_URL + "/" + test_vector.filename,
],
capture_output=True,
text=False,
)
Expand All @@ -155,7 +160,7 @@ def test_output_to_file_with_data_uris(shared_tmp_dir, test_vector) -> None:
output_file = os.path.join(shared_tmp_dir, test_vector.filename + ".output")
result = subprocess.run(
[
"python",
sys.executable,
"-m",
"markitdown",
"--keep-data-uris",
Expand All @@ -170,7 +175,7 @@ def test_output_to_file_with_data_uris(shared_tmp_dir, test_vector) -> None:
assert result.returncode == 0, f"CLI exited with error: {result.stderr}"
assert os.path.exists(output_file), f"Output file not created: {output_file}"

with open(output_file, "r") as f:
with open(output_file, "r", encoding="utf-8") as f:
output_data = f.read()
for test_string in test_vector.must_include:
assert test_string in output_data
Expand Down
10 changes: 5 additions & 5 deletions packages/markitdown/tests/test_cu_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,11 @@ def test_use_cu_reads_from_stdin(self, capsys):
enable_plugins=False,
cu_endpoint="https://fake-cu",
)
markitdown_instance.convert_stream.assert_called_once_with(
input_buffer,
stream_info=None,
keep_data_uris=False,
)

assert markitdown_instance.convert_stream.call_count == 1
call_args, call_kwargs = markitdown_instance.convert_stream.call_args
assert call_args[0].read() == b"fake pdf"
assert call_kwargs == {"stream_info": None, "keep_data_uris": False}
assert capsys.readouterr().out == "converted\n"


Expand Down
Loading