Skip to content

Commit 7628ebd

Browse files
committed
tests: add cache_toml kwarg to _cli.run for tests to disable TOML caching
1 parent f6ce9c9 commit 7628ebd

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

src/mdformat/_cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def emit(self, record: logging.LogRecord) -> None:
2424
sys.stderr.write(f"Warning: {record.msg}\n")
2525

2626

27-
def run(cli_args: Sequence[str]) -> int: # noqa: C901
27+
def run(cli_args: Sequence[str], cache_toml: bool = True) -> int: # noqa: C901
2828
arg_parser = make_arg_parser(
2929
mdformat.plugins._PARSER_EXTENSION_DISTS,
3030
mdformat.plugins._CODEFORMATTER_DISTS,
@@ -47,8 +47,9 @@ def run(cli_args: Sequence[str]) -> int: # noqa: C901
4747
format_errors_found = False
4848
renderer_warning_printer = RendererWarningPrinter()
4949
for path in file_paths:
50+
read_toml = read_toml_opts if cache_toml else read_toml_opts.__wrapped__
5051
try:
51-
toml_opts, toml_path = read_toml_opts(path.parent if path else Path.cwd())
52+
toml_opts, toml_path = read_toml(path.parent if path else Path.cwd())
5253
except InvalidConfError as e:
5354
print_error(str(e))
5455
return 1

tests/test_config_file.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
from mdformat._cli import run
8-
from tests.utils import FORMATTED_MARKDOWN, UNFORMATTED_MARKDOWN, run_with_clear_cache
8+
from tests.utils import FORMATTED_MARKDOWN, UNFORMATTED_MARKDOWN
99

1010

1111
def test_cli_override(tmp_path):
@@ -97,7 +97,7 @@ def test_conf_with_stdin(tmp_path, capfd, monkeypatch):
9797
monkeypatch.setattr(sys, "stdin", StringIO("1. one\n1. two\n1. three"))
9898

9999
with mock.patch("mdformat._cli.Path.cwd", return_value=tmp_path):
100-
assert run_with_clear_cache(("-",)) == 0
100+
assert run(("-",), cache_toml=False) == 0
101101
captured = capfd.readouterr()
102102
assert captured.out == "1. one\n2. two\n3. three\n"
103103

@@ -161,11 +161,11 @@ def test_conf_no_validate(tmp_path):
161161
"mdformat.renderer._context.get_list_marker_type",
162162
return_value="?",
163163
):
164-
assert run_with_clear_cache((str(file_path),)) == 1
164+
assert run((str(file_path),), cache_toml=False) == 1
165165
assert file_path.read_text() == content
166166

167167
config_path = tmp_path / ".mdformat.toml"
168168
config_path.write_text("validate = false")
169169

170-
assert run_with_clear_cache((str(file_path),)) == 0
170+
assert run((str(file_path),), cache_toml=False) == 0
171171
assert file_path.read_text() == "1? ordered\n"

tests/test_plugins.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
SuffixPostprocessPlugin,
2323
TablePlugin,
2424
TextEditorPlugin,
25-
run_with_clear_cache,
2625
)
2726

2827

@@ -444,7 +443,7 @@ def test_no_codeformatters__toml(tmp_path, monkeypatch):
444443
file1_path.write_text(unformatted)
445444
config_path = tmp_path / ".mdformat.toml"
446445
config_path.write_text("codeformatters = []")
447-
assert run_with_clear_cache((str(tmp_path),)) == 0
446+
assert run((str(tmp_path),), cache_toml=False) == 0
448447
assert file1_path.read_text() == unformatted
449448

450449

@@ -464,5 +463,5 @@ def test_no_extensions__toml(tmp_path, monkeypatch):
464463
file1_path.write_text(unformatted)
465464
config_path = tmp_path / ".mdformat.toml"
466465
config_path.write_text("extensions = []")
467-
assert run_with_clear_cache((str(tmp_path),)) == 0
466+
assert run((str(tmp_path),), cache_toml=False) == 0
468467
assert file1_path.read_text() == unformatted

tests/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import json
44
from typing import TYPE_CHECKING
55

6-
from mdformat._cli import run
7-
from mdformat._conf import read_toml_opts
86
from mdformat.renderer import RenderContext, RenderTreeNode
97

108
if TYPE_CHECKING:
@@ -14,11 +12,6 @@
1412
FORMATTED_MARKDOWN = "# A header\n"
1513

1614

17-
def run_with_clear_cache(*args, **kwargs):
18-
read_toml_opts.cache_clear()
19-
return run(*args, **kwargs)
20-
21-
2215
class JSONFormatterPlugin:
2316
"""A code formatter plugin that formats JSON."""
2417

0 commit comments

Comments
 (0)