Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class InvalidPath(Exception):
"""Exception raised when a path does not exist."""

def __init__(self, path: Path):
super().__init__(f'File "{path}" does not exist.')
self.path = path


Expand Down
6 changes: 5 additions & 1 deletion src/mdformat/_conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from collections.abc import Mapping
from contextlib import suppress
import functools
from pathlib import Path
from types import MappingProxyType
Expand Down Expand Up @@ -35,7 +36,10 @@ class InvalidConfError(Exception):
@functools.lru_cache
def read_toml_opts(conf_dir: Path) -> tuple[Mapping, Path | None]:
conf_path = conf_dir / ".mdformat.toml"
if not conf_path.is_file():
is_file = False
with suppress(OSError):
is_file = conf_path.is_file()
if not is_file:
parent_dir = conf_dir.parent
if conf_dir == parent_dir:
return {}, None
Expand Down
22 changes: 22 additions & 0 deletions tests/test_config_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import sys
from unittest import mock

Expand Down Expand Up @@ -168,3 +169,24 @@ def test_conf_no_validate(tmp_path):

assert run((str(file_path),), cache_toml=False) == 0
assert file_path.read_text() == "1? ordered\n"


def test_conf_search_with_oserror(tmp_path):
config_path = tmp_path / ".mdformat.toml"
config_path.write_text("wrap = 'no'")

subdir_path = tmp_path / "subdir"
subdir_path.mkdir()
file_path = subdir_path / "test_markdown.md"
file_path.write_text("remove\nthis\nwrap")

original_is_file = Path.is_file

def mock_is_file(self):
if str(self).endswith("subdir/.mdformat.toml"):
raise OSError(5, "Input/output error")
return original_is_file(self)

with mock.patch("pathlib.Path.is_file", new=mock_is_file):
assert run((str(file_path),), cache_toml=False) == 0
assert file_path.read_text() == "remove this wrap\n"