'
) in html
@pytest.mark.sphinx('epub', testroot='html_assets')
def test_epub_assets(app: SphinxTestApp) -> None:
app.build(force_all=True)
# epub_sytlesheets (same as html_css_files)
content = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
assert (
''
) in content
assert (
''
) in content
@pytest.mark.sphinx(
'epub',
testroot='html_assets',
confoverrides={'epub_css_files': ['css/epub.css']},
)
def test_epub_css_files(app: SphinxTestApp) -> None:
app.build(force_all=True)
# epub_css_files
content = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
assert (
''
) in content
# files in html_css_files are not outputted
assert (
''
) not in content
assert (
''
) not in content
@pytest.mark.sphinx('epub', testroot='roles-download')
def test_html_download_role(app: SphinxTestApp) -> None:
app.build()
assert not (app.outdir / '_downloads' / 'dummy.dat').exists()
content = (app.outdir / 'index.xhtml').read_text(encoding='utf8')
assert (
''
'dummy.dat
'
) in content
assert (
''
'not_found.dat
'
) in content
assert (
''
'Sphinx logo'
' [https://www.sphinx-doc.org/en/master'
'/_static/sphinx-logo.svg]
'
) in content
@pytest.mark.sphinx('epub', testroot='toctree-duplicated')
def test_duplicated_toctree_entry(app: SphinxTestApp) -> None:
app.build(force_all=True)
assert 'WARNING: duplicated ToC entry found: foo.xhtml' in app.warning.getvalue()
@pytest.mark.skipif(
'DO_EPUBCHECK' not in os.environ,
reason='Skipped because DO_EPUBCHECK is not set',
)
@pytest.mark.sphinx('epub', testroot='root')
def test_run_epubcheck(app: SphinxTestApp) -> None:
app.build()
if not runnable(['java', '-version']):
pytest.skip('Unable to run Java; skipping test')
epubcheck = Path(os.environ.get('EPUBCHECK_PATH', '/usr/share/java/epubcheck.jar'))
if not epubcheck.exists():
pytest.skip('Could not find epubcheck; skipping test')
try:
subprocess.run(
['java', '-jar', epubcheck, app.outdir / 'SphinxTests.epub'], # NoQA: S607
capture_output=True,
check=True,
)
except CalledProcessError as exc:
print(exc.stdout.decode('utf-8'))
print(exc.stderr.decode('utf-8'))
msg = f'epubcheck exited with return code {exc.returncode}'
raise AssertionError(msg) from exc
def test_xml_name_pattern_check() -> None:
assert _XML_NAME_PATTERN.match('id-pub')
assert _XML_NAME_PATTERN.match('webpage')
assert not _XML_NAME_PATTERN.match('1bfda21')
@pytest.mark.usefixtures('_http_teapot')
@pytest.mark.sphinx('epub', testroot='images')
def test_copy_images(app: SphinxTestApp) -> None:
app.build()
images_dir = Path(app.outdir) / '_images'
images = {image.name for image in images_dir.rglob('*')}
images.discard('python-logo.png')
assert images == {
# 'ba30773957c3fe046897111afd65a80b81cad089.png', # epub: image from data:image/png URI in source
'img.png',
'rimg.png',
'rimg1.png',
'svgimg.svg',
'testimäge.png',
}
@pytest.mark.sphinx('epub', testroot='builder-dirhtml')
def test_epub_manifest_path_separator_normalization(app: SphinxTestApp) -> None:
"""Test that path separators are normalized to forward slashes
in EPUB manifests, even on Windows.
"""
app.build()
# Read the content.opf file
opf_path = app.outdir / 'content.opf'
assert opf_path.exists(), 'content.opf was not generated'
# Parse manifest and spine elements
# Verify that all idrefs in spine match ids in manifest
tree = ET.parse(str(opf_path)) # noqa: S314
root = tree.getroot()
# Define namespace
ns = {'opf': 'http://www.idpf.org/2007/opf'}
# Collect items from manifest
manifest_ids: set[str | None] = set()
manifest_hrefs: dict[str, str] = {}
for item in root.findall('.//opf:manifest/opf:item', ns):
item_id: str | None = item.get('id')
item_href: str | None = item.get('href')
manifest_ids.add(item_id)
if item_id is not None and item_href is not None:
manifest_hrefs[item_id] = item_href
# Check idrefs in spine
spine_idrefs = []
for itemref in root.findall('.//opf:spine/opf:itemref', ns):
idref: str | None = itemref.get('idref')
spine_idrefs.append(idref)
# Verify all spine idrefs exist in manifest
for idref in spine_idrefs:
assert idref in manifest_ids, (
f"spine idref '{idref}' does not exist in manifest"
)
# Verify hrefs do not contain backslashes
# (should be normalized to forward slashes even on Windows)
for item_id, href in manifest_hrefs.items():
assert '\\' not in href, (
f"manifest item '{item_id}' href '{href}' contains backslashes"
)
# Verify no duplicate IDs are assigned to the same href
href_to_ids: dict[str, list[str | None]] = {}
for item_id, href in manifest_hrefs.items():
# Normalize path for comparison
normalized_href = href.replace('\\', '/')
if normalized_href not in href_to_ids:
href_to_ids[normalized_href] = []
href_to_ids[normalized_href].append(item_id)
# Detect duplicate IDs
duplicates: dict[str, list[str | None]] = {
href: ids for href, ids in href_to_ids.items() if len(ids) > 1
}
assert not duplicates, f'Multiple IDs assigned to the same file: {duplicates}'
@pytest.mark.sphinx('epub', testroot='builder-dirhtml')
def test_epub_manifest_subdirectory_paths(app: SphinxTestApp) -> None:
"""Test that path separators are correctly normalized to forward slashes
even for paths containing subdirectories.
"""
app.build()
opf_path = app.outdir / 'content.opf'
assert opf_path.exists()
tree = ET.parse(str(opf_path)) # noqa: S314
root = tree.getroot()
ns: dict[str, str] = {'opf': 'http://www.idpf.org/2007/opf'}
# Check all manifest item hrefs
for item in root.findall('.//opf:manifest/opf:item', ns):
href: str | None = item.get('href')
if href is not None:
# Verify no backslashes are present
assert '\\' not in href, (
f"href '{href}' contains backslashes (should be forward slashes)"
)
# For paths with subdirectories, verify they are separated by forward slashes
if href is not None and '/' in href:
# Verify the path is correctly constructed
parts: list[str] = href.split('/')
assert all(parts), f"href '{href}' contains empty path segments"
@pytest.mark.sphinx('epub', testroot='basic')
def test_epub_spine_idref_consistency(app: SphinxTestApp) -> None:
"""Test that spine idrefs and manifest ids are consistent.
Verify that path separator normalization ensures the same file
is reliably referenced with the same ID.
"""
app.build()
opf_path = app.outdir / 'content.opf'
tree = ET.parse(str(opf_path)) # noqa: S314
root = tree.getroot()
ns: dict[str, str] = {'opf': 'http://www.idpf.org/2007/opf'}
# Create id→href mapping from manifest
id_to_href = {}
for item in root.findall('.//opf:manifest/opf:item', ns):
item_id: str | None = item.get('id')
item_href: str | None = item.get('href')
id_to_href[item_id] = item_href
# For each idref in spine, verify corresponding href exists
# and that href is unique
spine_hrefs = []
for itemref in root.findall('.//opf:spine/opf:itemref', ns):
idref: str | None = itemref.get('idref')
assert idref in id_to_href, f"manifest item not found for spine idref '{idref}'"
href = id_to_href[idref]
spine_hrefs.append(href)
# Warn if the same href is referenced multiple times
# (normally each file should appear only once in spine)
href_counts = Counter(spine_hrefs)
duplicated_hrefs: list[str | None] = [
href for href, count in href_counts.items() if count > 1
]
# Note: Some EPUBs may intentionally reference the same file multiple times,
# so this is logged as informational rather than a strict error
assert len(duplicated_hrefs) == 0