Use pathlib in more places

This commit is contained in:
Adam Turner
2025-02-07 13:51:42 +00:00
parent c2bea73132
commit 22e0f8944b
28 changed files with 170 additions and 161 deletions

View File

@@ -8,6 +8,8 @@ from typing import TYPE_CHECKING
from sphinx.transforms.post_transforms.images import ImageConverter
if TYPE_CHECKING:
import os
from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata
@@ -20,7 +22,9 @@ class MyConverter(ImageConverter):
def is_available(self) -> bool:
return True
def convert(self, _from: str, _to: str) -> bool:
def convert(
self, _from: str | os.PathLike[str], _to: str | os.PathLike[str]
) -> bool:
"""Mock converts the image from SVG to PDF."""
shutil.copyfile(_from, _to)
return True

View File

@@ -1,7 +1,7 @@
import os
import sys
from pathlib import Path
source_dir = os.path.abspath('.')
source_dir = str(Path.cwd().resolve())
if source_dir not in sys.path:
sys.path.insert(0, source_dir)
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']

View File

@@ -4,9 +4,11 @@ from __future__ import annotations
import pytest
from sphinx.util._pathlib import _StrPath
@pytest.mark.sphinx('html', testroot='environment-record-dependencies')
def test_record_dependencies_cleared(app):
app.builder.read()
assert app.env.dependencies['index'] == set()
assert app.env.dependencies['api'] == {'example_module.py'}
assert 'index' not in app.env.dependencies
assert app.env.dependencies['api'] == {_StrPath('example_module.py')}

View File

@@ -3,6 +3,7 @@
from __future__ import annotations
import re
from pathlib import Path
from unittest import mock
import pytest
@@ -144,10 +145,10 @@ def test_copy_asset_overwrite(app):
def test_template_basename():
assert _template_basename('asset.txt') is None
assert _template_basename('asset.txt.jinja') == 'asset.txt'
assert _template_basename('sidebar.html.jinja') == 'sidebar.html'
assert _template_basename(Path('asset.txt')) is None
assert _template_basename(Path('asset.txt.jinja')) == Path('asset.txt')
assert _template_basename(Path('sidebar.html.jinja')) == Path('sidebar.html')
def test_legacy_template_basename():
assert _template_basename('asset.txt_t') == 'asset.txt'
assert _template_basename(Path('asset.txt_t')) == Path('asset.txt')