File: test_cli.py

package info (click to toggle)
mdformat 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 748 kB
  • sloc: python: 11,287; makefile: 9
file content (514 lines) | stat: -rw-r--r-- 16,475 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import os
import sys
from unittest.mock import patch

import pytest

import mdformat
from mdformat._cli import get_plugin_info_str, run, wrap_paragraphs
from mdformat.plugins import CODEFORMATTERS, PARSER_EXTENSIONS
from tests.utils import (
    FORMATTED_MARKDOWN,
    UNFORMATTED_MARKDOWN,
    ASTChangingPlugin,
    PrefixPostprocessPlugin,
)


def test_no_files_passed():
    assert run(()) == 0


def test_format(tmp_path):
    file_path = tmp_path / "test_markdown.md"
    file_path.write_text(UNFORMATTED_MARKDOWN)
    assert run((str(file_path),)) == 0
    assert file_path.read_text() == FORMATTED_MARKDOWN


def test_format__folder(tmp_path):
    file_path_1 = tmp_path / "test_markdown1.md"
    file_path_2 = tmp_path / "test_markdown2.md"
    file_path_3 = tmp_path / "not_markdown3"
    file_path_1.write_text(UNFORMATTED_MARKDOWN)
    file_path_2.write_text(UNFORMATTED_MARKDOWN)
    file_path_3.write_text(UNFORMATTED_MARKDOWN)
    assert run((str(tmp_path),)) == 0
    assert file_path_1.read_text() == FORMATTED_MARKDOWN
    assert file_path_2.read_text() == FORMATTED_MARKDOWN
    assert file_path_3.read_text() == UNFORMATTED_MARKDOWN


def test_format__folder_leads_to_invalid(tmp_path):
    file_path_1 = tmp_path / "test_markdown1.md"
    file_path_1.mkdir()
    assert run((str(tmp_path),)) == 0
    assert file_path_1.is_dir()


def test_format__symlinks(tmp_path):
    # Create two MD files
    file_path_1 = tmp_path / "test_markdown1.md"
    file_path_2 = tmp_path / "test_markdown2.md"
    file_path_1.write_text(UNFORMATTED_MARKDOWN)
    file_path_2.write_text(UNFORMATTED_MARKDOWN)

    # Create a symlink to both files: one in the root folder, one in a sub folder
    subdir_path = tmp_path / "subdir"
    subdir_path.mkdir()
    symlink_1 = subdir_path / "symlink1.md"
    symlink_1.symlink_to(file_path_1)
    symlink_2 = tmp_path / "symlink2.md"
    symlink_2.symlink_to(file_path_2)

    # Format file 1 via directory of the symlink, and file 2 via symlink
    assert run([str(subdir_path), str(symlink_2)]) == 0

    # Assert that files are formatted and symlinks are not overwritten
    assert file_path_1.read_text() == FORMATTED_MARKDOWN
    assert file_path_2.read_text() == FORMATTED_MARKDOWN
    assert symlink_1.is_symlink()
    assert symlink_2.is_symlink()


def test_broken_symlink(tmp_path, capsys):
    # Create a broken symlink
    file_path = tmp_path / "test_markdown1.md"
    symlink_path = tmp_path / "symlink"
    symlink_path.symlink_to(file_path)

    with pytest.raises(SystemExit) as exc_info:
        run([str(symlink_path)])
    assert exc_info.value.code == 2
    captured = capsys.readouterr()
    assert "does not exist" in captured.err


def test_invalid_file(capsys):
    with pytest.raises(SystemExit) as exc_info:
        run(("this is not a valid filepath?`=|><@{[]\\/,.%ยค#'",))
    assert exc_info.value.code == 2
    captured = capsys.readouterr()
    assert "does not exist" in captured.err


@pytest.mark.skipif(os.name == "nt", reason="No os.mkfifo on windows")
def test_fifo(tmp_path, capsys):
    fifo_path = tmp_path / "fifo1"
    os.mkfifo(fifo_path)
    with pytest.raises(SystemExit) as exc_info:
        run((str(fifo_path),))
    assert exc_info.value.code == 2
    assert "does not exist" in capsys.readouterr().err


def test_check(tmp_path):
    file_path = tmp_path / "test_markdown.md"
    file_path.write_bytes(FORMATTED_MARKDOWN.encode())
    assert run((str(file_path), "--check")) == 0


def test_check__fail(tmp_path):
    file_path = tmp_path / "test_markdown.md"
    file_path.write_text(UNFORMATTED_MARKDOWN)
    assert run((str(file_path), "--check")) == 1


def test_check__multi_fail(capsys, tmp_path):
    """Test for --check flag when multiple files are unformatted.

    Test that the names of all unformatted files are listed when using
    --check.
    """
    file_path1 = tmp_path / "test_markdown1.md"
    file_path2 = tmp_path / "test_markdown2.md"
    file_path1.write_text(UNFORMATTED_MARKDOWN)
    file_path2.write_text(UNFORMATTED_MARKDOWN)
    assert run((str(tmp_path), "--check")) == 1
    captured = capsys.readouterr()
    assert str(file_path1) in captured.err
    assert str(file_path2) in captured.err


def example_formatter(code, info):
    return "dummy\n"


def test_formatter_plugin(tmp_path, monkeypatch):
    monkeypatch.setitem(CODEFORMATTERS, "lang", example_formatter)
    file_path = tmp_path / "test_markdown.md"
    file_path.write_text("```lang\nother\n```\n")
    assert run((str(file_path),)) == 0
    assert file_path.read_text() == "```lang\ndummy\n```\n"


def test_dash_stdin(capfd, patch_stdin):
    patch_stdin(UNFORMATTED_MARKDOWN)
    assert run(("-",)) == 0
    captured = capfd.readouterr()
    assert captured.out == FORMATTED_MARKDOWN


def test_wrap_paragraphs():
    with patch("shutil.get_terminal_size", return_value=(72, 24)):
        assert wrap_paragraphs(
            [
                'Error: Could not format "/home/user/file_name_longer_than_wrap_width--------------------------------------.md".',  # noqa: E501
                "The formatted Markdown renders to different HTML than the input Markdown. "  # noqa: E501
                "This is likely a bug in mdformat. "
                "Please create an issue report here: "
                "https://github.com/hukkin/mdformat/issues",
            ]
        ) == (
            "Error: Could not format\n"
            '"/home/user/file_name_longer_than_wrap_width--------------------------------------.md".\n'  # noqa: E501
            "\n"
            "The formatted Markdown renders to different HTML than the input\n"
            "Markdown. This is likely a bug in mdformat. Please create an issue\n"
            "report here: https://github.com/hukkin/mdformat/issues\n"
        )


def test_version(capsys):
    with pytest.raises(SystemExit) as exc_info:
        run(["--version"])
    assert exc_info.value.code == 0
    captured = capsys.readouterr()
    assert captured.out.startswith(f"mdformat {mdformat.__version__}")


def test_no_wrap(tmp_path):
    file_path = tmp_path / "test_markdown.md"
    file_path.write_text(
        "all\n"
        "these newlines\n"
        "except the one in this hardbreak  \n"
        "should be\n"
        "removed because they are\n"
        "in the same paragraph\n"
        "\n"
        "This however is the next\n"
        "paragraph.    Whitespace should  be collapsed\n"
        "    \t here\n"
    )
    assert run([str(file_path), "--wrap=no"]) == 0
    assert (
        file_path.read_text()
        == "all these newlines except the one in this hardbreak\\\n"
        "should be removed because they are in the same paragraph\n"
        "\n"
        "This however is the next paragraph. Whitespace should be collapsed here\n"
    )


def test_wrap(tmp_path):
    file_path = tmp_path / "test_markdown.md"
    file_path.write_text(
        "This\n"
        "text\n"
        "should\n"
        "be wrapped again so that wrap width is whatever is defined below. "
        "Also     whitespace            should\t\tbe             collapsed. "
        "Next up a second paragraph:\n"
        "\n"
        "This paragraph should also be wrapped. "
        "Here's some more text to wrap.  "
        "Here's some more text to wrap.  "
        "Here's some more text to wrap.  "
    )
    assert run([str(file_path), "--wrap=60"]) == 0
    assert (
        file_path.read_text()
        == "This text should be wrapped again so that wrap width is\n"
        "whatever is defined below. Also whitespace should be\n"
        "collapsed. Next up a second paragraph:\n"
        "\n"
        "This paragraph should also be wrapped. Here's some more text\n"
        "to wrap. Here's some more text to wrap. Here's some more\n"
        "text to wrap.\n"
    )


def test_consecutive_wrap_width_lines(tmp_path):
    """Test a case where two consecutive lines' width equals wrap width.

    There was a bug where this would cause an extra newline and split
    the paragraph, hence the test.
    """
    wrap_width = 20
    file_path = tmp_path / "test_markdown.md"
    text = "A" * wrap_width + "\n" + "A" * wrap_width + "\n"
    file_path.write_text(text)
    assert run([str(file_path), f"--wrap={wrap_width}"]) == 0
    assert file_path.read_text() == text


def test_wrap__hard_break(tmp_path):
    file_path = tmp_path / "test_markdown.md"
    file_path.write_text(
        "This\n"
        "text\n"
        "should\n"
        "be wrapped again\\\n"
        "so that wrap width is whatever is defined below. "
        "Also     whitespace            should\t\tbe             collapsed."
    )
    assert run([str(file_path), "--wrap=60"]) == 0
    assert (
        file_path.read_text() == "This text should be wrapped again\\\n"
        "so that wrap width is whatever is defined below. Also\n"
        "whitespace should be collapsed.\n"
    )


def test_bad_wrap_width(capsys):
    with pytest.raises(SystemExit) as exc_info:
        run(["some-path.md", "--wrap=-1"])
    assert exc_info.value.code == 2
    captured = capsys.readouterr()
    assert "error: argument --wrap" in captured.err


def test_eol__lf(tmp_path):
    file_path = tmp_path / "test.md"
    file_path.write_bytes(b"Oi\r\n")
    assert run([str(file_path)]) == 0
    assert file_path.read_bytes() == b"Oi\n"


def test_eol__crlf(tmp_path):
    file_path = tmp_path / "test.md"
    file_path.write_bytes(b"Oi\n")
    assert run([str(file_path), "--end-of-line=crlf"]) == 0
    assert file_path.read_bytes() == b"Oi\r\n"


def test_eol__keep_lf(tmp_path):
    file_path = tmp_path / "test.md"
    file_path.write_bytes(b"Oi\n")
    assert run([str(file_path), "--end-of-line=keep"]) == 0
    assert file_path.read_bytes() == b"Oi\n"


def test_eol__keep_crlf(tmp_path):
    file_path = tmp_path / "test.md"
    file_path.write_bytes(b"Oi\r\n")
    assert run([str(file_path), "--end-of-line=keep"]) == 0
    assert file_path.read_bytes() == b"Oi\r\n"


def test_eol__crlf_stdin(capfd, patch_stdin):
    patch_stdin("Oi\n")
    assert run(["-", "--end-of-line=crlf"]) == 0
    captured = capfd.readouterr()
    assert captured.out == "Oi\r\n"


def test_eol__check_lf(tmp_path):
    file_path = tmp_path / "test.md"

    file_path.write_bytes(b"lol\r\n")
    assert run((str(file_path), "--check")) == 1

    file_path.write_bytes(b"lol\n")
    assert run((str(file_path), "--check")) == 0


def test_eol__check_crlf(tmp_path):
    file_path = tmp_path / "test.md"

    file_path.write_bytes(b"lol\n")
    assert run((str(file_path), "--check", "--end-of-line=crlf")) == 1

    file_path.write_bytes(b"lol\r\n")
    assert run((str(file_path), "--check", "--end-of-line=crlf")) == 0


def test_eol__check_keep_lf(tmp_path):
    file_path = tmp_path / "test.md"

    file_path.write_bytes(b"lol\n")
    assert run((str(file_path), "--check", "--end-of-line=keep")) == 0

    file_path.write_bytes(b"mixed\nEOLs\r")
    assert run((str(file_path), "--check", "--end-of-line=keep")) == 1


def test_eol__check_keep_crlf(tmp_path):
    file_path = tmp_path / "test.md"

    file_path.write_bytes(b"lol\r\n")
    assert run((str(file_path), "--check", "--end-of-line=keep")) == 0

    file_path.write_bytes(b"mixed\r\nEOLs\n")
    assert run((str(file_path), "--check", "--end-of-line=keep")) == 1


def test_cli_no_validate(tmp_path):
    file_path = tmp_path / "test.md"
    content = "1. ordered"
    file_path.write_text(content)

    with patch("mdformat.renderer._context.get_list_marker_type", return_value="?"):
        assert run((str(file_path),)) == 1
        assert file_path.read_text() == content

        assert run((str(file_path), "--no-validate")) == 0
        assert file_path.read_text() == "1? ordered\n"


def test_get_plugin_info_str():
    info = get_plugin_info_str(
        {"mdformat-tables": ("0.1.0", ["tables"])},
        {"mdformat-black": ("12.1.0", ["python"])},
    )
    assert (
        info
        == """\
installed codeformatters:
  mdformat-black: python

installed extensions:
  mdformat-tables: tables"""
    )


def test_no_timestamp_modify(tmp_path):
    file_path = tmp_path / "test.md"

    file_path.write_bytes(b"lol\n")
    initial_access_time = 0
    initial_mod_time = 0
    os.utime(file_path, (initial_access_time, initial_mod_time))

    # Assert that modification time does not change when no changes are applied
    assert run([str(file_path)]) == 0
    assert os.path.getmtime(file_path) == initial_mod_time


@pytest.mark.skipif(
    sys.version_info < (3, 13), reason="'exclude' only possible on 3.13+"
)
def test_exclude(tmp_path):
    subdir_path_1 = tmp_path / "folder1"
    subdir_path_2 = subdir_path_1 / "folder2"
    file_path_1 = subdir_path_2 / "file1.md"
    subdir_path_1.mkdir()
    subdir_path_2.mkdir()
    file_path_1.write_text(UNFORMATTED_MARKDOWN)
    cwd = tmp_path

    with patch("mdformat._cli.Path.cwd", return_value=cwd):
        for good_pattern in [
            "folder1/folder2/file1.md",
            "**",
            "**/*.md",
            "**/file1.md",
            "folder1/**",
        ]:
            assert run([str(file_path_1), "--exclude", good_pattern]) == 0
            assert file_path_1.read_text() == UNFORMATTED_MARKDOWN

        for bad_pattern in [
            "**file1.md",
            "file1.md",
            "folder1",
            "*.md",
            "*",
            "folder1/*",
        ]:
            file_path_1.write_text(UNFORMATTED_MARKDOWN)
            assert run([str(file_path_1), "--exclude", bad_pattern]) == 0
            assert file_path_1.read_text() == FORMATTED_MARKDOWN


def test_codeformatters(tmp_path, monkeypatch):
    monkeypatch.setitem(CODEFORMATTERS, "enabled-lang", lambda code, info: "dumdum")
    monkeypatch.setitem(CODEFORMATTERS, "disabled-lang", lambda code, info: "dumdum")
    file_path = tmp_path / "test.md"
    unformatted = """\
```disabled-lang
hey
```

```enabled-lang
hey
```
"""
    formatted = """\
```disabled-lang
hey
```

```enabled-lang
dumdum
```
"""
    file_path.write_text(unformatted)
    assert run((str(file_path), "--codeformatters", "enabled-lang")) == 0
    assert file_path.read_text() == formatted


def test_extensions(tmp_path, monkeypatch):
    ast_plugin_name = "ast-plug"
    prefix_plugin_name = "prefix-plug"
    monkeypatch.setitem(PARSER_EXTENSIONS, ast_plugin_name, ASTChangingPlugin)
    monkeypatch.setitem(PARSER_EXTENSIONS, prefix_plugin_name, PrefixPostprocessPlugin)
    unformatted = "original text\n"
    file_path = tmp_path / "test.md"

    file_path.write_text(unformatted)
    assert run((str(file_path), "--extensions", "prefix-plug")) == 0
    assert file_path.read_text() == "Prefixed!original text\n"

    file_path.write_text(unformatted)
    assert run((str(file_path), "--extensions", "ast-plug")) == 0
    assert file_path.read_text() == ASTChangingPlugin.TEXT_REPLACEMENT + "\n"

    file_path.write_text(unformatted)
    assert (
        run((str(file_path), "--extensions", "ast-plug", "--extensions", "prefix-plug"))
        == 0
    )
    assert (
        file_path.read_text() == "Prefixed!" + ASTChangingPlugin.TEXT_REPLACEMENT + "\n"
    )


def test_codeformatters__invalid(tmp_path, capsys):
    file_path = tmp_path / "test.md"
    file_path.write_text("")
    assert run((str(file_path), "--codeformatters", "no-exists")) == 1
    captured = capsys.readouterr()
    assert "Error: Invalid code formatter required" in captured.err


def test_extensions__invalid(tmp_path, capsys):
    file_path = tmp_path / "test.md"
    file_path.write_text("")
    assert run((str(file_path), "--extensions", "no-exists")) == 1
    captured = capsys.readouterr()
    assert "Error: Invalid extension required" in captured.err


def test_no_codeformatters(tmp_path, monkeypatch):
    monkeypatch.setitem(CODEFORMATTERS, "lang", lambda code, info: "dumdum")
    file_path = tmp_path / "test.md"
    original_md = """\
```lang
original code
```
"""
    file_path.write_text(original_md)
    assert run((str(file_path), "--no-codeformatters")) == 0
    assert file_path.read_text() == original_md


def test_no_extensions(tmp_path, monkeypatch):
    plugin_name = "plug-name"
    monkeypatch.setitem(PARSER_EXTENSIONS, plugin_name, ASTChangingPlugin)
    file_path = tmp_path / "test.md"
    original_md = "original md\n"
    file_path.write_text(original_md)
    assert run((str(file_path), "--no-extensions")) == 0
    assert file_path.read_text() == original_md