From 86ee3b0d06cc73e45ee1c1f7625ee3bcada621bb Mon Sep 17 00:00:00 2001 From: WU Leizhi Date: Mon, 5 Oct 2026 20:53:26 +0800 Subject: [PATCH] fix: correct CLI default title flag --- markdownify/main.py | 2 +- tests/test_main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/test_main.py diff --git a/markdownify/main.py b/markdownify/main.py index ba70671..c0c96cd 100755 --- a/markdownify/main.py +++ b/markdownify/main.py @@ -26,7 +26,7 @@ def main(argv=sys.argv[1:]): parser.add_argument('-a', '--autolinks', action='store_true', help="A boolean indicating whether the 'automatic link' style " "should be used when a 'a' tag's contents match its href.") - parser.add_argument('--default-title', action='store_false', + parser.add_argument('--default-title', action='store_true', help="A boolean to enable setting the title of a link to its " "href, if no title is given.") parser.add_argument('--heading-style', default=UNDERLINED, diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..a8b72aa --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,42 @@ +from io import StringIO +import sys + +from markdownify.main import main + + +def test_cli_stdin_default_and_flag(monkeypatch, capfd): + html = 'Example' + + monkeypatch.setattr(sys, 'stdin', StringIO(html)) + main([]) + assert capfd.readouterr().out == '[Example](https://example.com)\n' + + monkeypatch.setattr(sys, 'stdin', StringIO(html)) + main(['--default-title']) + assert capfd.readouterr().out == '[Example](https://example.com "https://example.com")\n' + + +def test_cli_named_file_and_explicit_title(tmp_path, monkeypatch, capfd): + source = tmp_path / 'input.html' + source.write_text( + 'Example', + encoding='utf-8', + ) + + main([str(source)]) + assert capfd.readouterr().out == '[Example](https://example.com "Custom")\n' + + main(['--default-title', str(source)]) + assert capfd.readouterr().out == '[Example](https://example.com "Custom")\n' + + +def test_cli_autolinks_option_is_unchanged(monkeypatch, capfd): + html = 'https://example.com' + + monkeypatch.setattr(sys, 'stdin', StringIO(html)) + main(['-a']) + assert capfd.readouterr().out == '\n' + + monkeypatch.setattr(sys, 'stdin', StringIO(html)) + main(['-a', '--default-title']) + assert capfd.readouterr().out == '[https://example.com](https://example.com "https://example.com")\n'