Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(parser): fail an EPUB with no readable chapter instead of returning nothing (#19814)

Lukas6ef64fcd1be8
diff --git a/deepdoc/parser/epub_parser.py b/deepdoc/parser/epub_parser.py
index bf888344d4..1d901b67a4 100644
--- a/deepdoc/parser/epub_parser.py
+++ b/deepdoc/parser/epub_parser.py
@@ -15,11 +15,15 @@
#
import logging
+import lzma
import warnings
import zipfile
+import zlib
from io import BytesIO
from xml.etree import ElementTree
+from bs4 import ParserRejectedMarkup
+
from .html_parser import RAGFlowHtmlParser
# OPF XML namespaces
@@ -37,6 +41,13 @@ class RAGFlowEpubParser:
and delegating to RAGFlowHtmlParser for chunking."""
def __call__(self, fnm, binary=None, chunk_token_num=512):
+ """Return the text sections of every readable content item, in spine order.
+
+ An item that cannot be read or parsed is skipped with a warning. Raises
+ ValueError for an empty payload, and when items failed and no other item
+ could be parsed. An item that parses to no text (an image-only chapter)
+ still counts as read.
+ """
if binary is not None:
if not binary:
logger.warning(
@@ -51,16 +62,21 @@ class RAGFlowEpubParser:
try:
content_items = self._get_spine_items(zf)
all_sections = []
+ failures = []
+ parsed = 0
html_parser = RAGFlowHtmlParser()
for item_path in content_items:
try:
html_bytes = zf.read(item_path)
- except (KeyError, RuntimeError, zipfile.BadZipFile, NotImplementedError, EOFError) as e:
+ except (KeyError, RuntimeError, zipfile.BadZipFile, NotImplementedError, EOFError, zlib.error, OSError, lzma.LZMAError) as e:
# A spine item can be missing, encrypted, damaged, or stored with a
- # compression method zipfile does not implement. Only that chapter is
- # unreadable; the rest of the book still parses.
+ # compression method zipfile does not implement. A damaged item fails
+ # its CRC check or, before that, its decompressor, which raises its own
+ # error: zlib.error for Deflate, OSError for bzip2, LZMAError for LZMA.
+ # Only that chapter is unreadable; the rest of the book still parses.
logger.warning("Skipping unreadable EPUB content item '%s': %s", item_path, e)
+ failures.append(f"{item_path}: {e}")
continue
if not html_bytes:
logger.debug("Skipping empty EPUB content item: %s", item_path)
@@ -69,13 +85,19 @@ class RAGFlowEpubParser:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
sections = html_parser(item_path, binary=html_bytes, chunk_token_num=chunk_token_num)
- except Exception as e:
- # decode_text refuses a weak codec guess, and a chapter can be
- # mislabelled XHTML. Same reasoning as above.
+ except (ValueError, ParserRejectedMarkup, RecursionError) as e:
+ # decode_text refuses a weak codec guess (UnicodeError is a ValueError),
+ # a numeric character reference past 4300 digits fails int(), html.parser
+ # rejects some malformed markup, and the HTML walker recurses once per
+ # element. Same reasoning as above; any other error is a bug and propagates.
logger.warning("Skipping EPUB content item '%s' that failed to parse: %s", item_path, e)
+ failures.append(f"{item_path}: {e}")
continue
+ parsed += 1
all_sections.extend(sections)
+ if failures and not parsed:
+ raise ValueError(f"No readable content in EPUB: {len(failures)} of {len(content_items)} content items could not be read or parsed ({failures[0]})")
return all_sections
finally:
zf.close()
diff --git a/internal/parser/parser/epub_parser.go b/internal/parser/parser/epub_parser.go
index 59bd1d701b..e3cc30f1e3 100644
--- a/internal/parser/parser/epub_parser.go
+++ b/internal/parser/parser/epub_parser.go
@@ -82,7 +82,10 @@ func (p *EPUBParser) ParseWithResult(ctx context.Context, filename string, data
return ParseResult{Err: fmt.Errorf("epub: opf: %w", err)}
}
- items, detectedEnc := extractEPUBTextItems(reader, opfPath, spineItems)
+ items, detectedEnc, err := extractEPUBTextItems(reader, opfPath, spineItems)
+ if err != nil {
+ return ParseResult{Err: fmt.Errorf("epub: %w", err)}
+ }
if items == nil {
items = []map[string]any{{"text": "", "doc_type_kwd": "text"}}
}
@@ -194,14 +197,31 @@ var (
epubWSRe = regexp.MustCompile(`\s+`)
)
-func extractEPUBTextItems(r *zip.Reader, opfDir string, spineHrefs []string) ([]map[string]any, string) {
+// extractEPUBTextItems reads every spine item and returns one text item per
+// item that has text. An item that cannot be read is skipped; when items were
+// skipped and no other item could be read, it returns an error instead of an
+// empty result. An item that holds no text (an image-only chapter) still counts
+// as read, and an empty item counts as neither.
+func extractEPUBTextItems(r *zip.Reader, opfDir string, spineHrefs []string) ([]map[string]any, string, error) {
var items []map[string]any
+ var failures []string
+ read := 0
detectedEncoding := "utf-8"
for _, href := range spineHrefs {
- text, enc := readEPUBContentFile(r, opfDir, href)
+ raw, err := readEPUBContentFile(r, opfDir, href)
+ if err != nil {
+ failures = append(failures, fmt.Sprintf("%s: %v", href, err))
+ continue
+ }
+ if len(raw) == 0 {
+ continue
+ }
+ read++
+ decoded, enc := DecodeToUTF8(raw, "application/xhtml+xml")
if detectedEncoding == "utf-8" && enc != "" && enc != "utf-8" {
detectedEncoding = enc
}
+ text := stripHTMLTags(string(decoded))
if strings.TrimSpace(text) == "" {
continue
}
@@ -210,13 +230,15 @@ func extractEPUBTextItems(r *zip.Reader, opfDir string, spineHrefs []string) ([]
"doc_type_kwd": "text",
})
}
- return items, detectedEncoding
+ if len(failures) > 0 && read == 0 {
+ return nil, detectedEncoding, fmt.Errorf("no readable content: %d of %d content items could not be read (%s)", len(failures), len(spineHrefs), failures[0])
+ }
+ return items, detectedEncoding, nil
}
// readEPUBContentFile resolves a spine href (relative to the OPF
-// directory) inside the ZIP, reads the raw bytes, and strips HTML to
-// return clean text and detected encoding.
-func readEPUBContentFile(r *zip.Reader, opfDir, href string) (string, string) {
+// directory) inside the ZIP and returns the item's raw bytes.
+func readEPUBContentFile(r *zip.Reader, opfDir, href string) ([]byte, error) {
// Resolve href relative to the directory containing the OPF.
// Use path.Join/Dir (slash separator) because ZIP entry paths are always POSIX.
resolved := path.Join(path.Dir(opfDir), href)
@@ -234,16 +256,10 @@ func readEPUBContentFile(r *zip.Reader, opfDir, href string) (string, string) {
}
}
if err != nil {
- return "", ""
+ return nil, err
}
defer f.Close()
-
- raw, err := io.ReadAll(f)
- if err != nil {
- return "", ""
- }
- decoded, enc := DecodeToUTF8(raw, "application/xhtml+xml")
- return stripHTMLTags(string(decoded)), enc
+ return io.ReadAll(f)
}
// stripHTMLTags removes HTML markup and returns normalized plain text.
diff --git a/internal/parser/parser/epub_parser_test.go b/internal/parser/parser/epub_parser_test.go
new file mode 100644
index 0000000000..05fc917189
--- /dev/null
+++ b/internal/parser/parser/epub_parser_test.go
@@ -0,0 +1,187 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package parser
+
+import (
+ "archive/zip"
+ "bytes"
+ "context"
+ "fmt"
+ "strings"
+ "testing"
+)
+
+// epubTestChapter is one spine item of a test EPUB. A missing chapter is
+// listed in the manifest and spine but has no ZIP entry; a damaged one is
+// stored as a Deflate entry whose data is not a valid Deflate stream.
+type epubTestChapter struct {
+ name string
+ content string
+ missing bool
+ damaged bool
+}
+
+func epubTestHTML(body string) string {
+ return `<?xml version="1.0" encoding="utf-8"?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Test</title></head><body>` + body + `</body></html>`
+}
+
+func buildTestEPUB(t *testing.T, chapters []epubTestChapter) []byte {
+ t.Helper()
+ var manifest, spine strings.Builder
+ for i, ch := range chapters {
+ fmt.Fprintf(&manifest, `<item id="c%d" href="%s" media-type="application/xhtml+xml"/>`, i, ch.name)
+ fmt.Fprintf(&spine, `<itemref idref="c%d"/>`, i)
+ }
+ containerXML := `<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/></rootfiles></container>`
+ contentOPF := `<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf" version="3.0"><manifest>` + manifest.String() + `</manifest><spine>` + spine.String() + `</spine></package>`
+
+ buf := new(bytes.Buffer)
+ zw := zip.NewWriter(buf)
+ write := func(name, content string) {
+ w, err := zw.Create(name)
+ if err != nil {
+ t.Fatalf("create %s: %v", name, err)
+ }
+ if _, err := w.Write([]byte(content)); err != nil {
+ t.Fatalf("write %s: %v", name, err)
+ }
+ }
+ write("META-INF/container.xml", containerXML)
+ write("OEBPS/content.opf", contentOPF)
+ for _, ch := range chapters {
+ switch {
+ case ch.missing:
+ case ch.damaged:
+ w, err := zw.CreateRaw(&zip.FileHeader{Name: "OEBPS/" + ch.name, Method: zip.Deflate, CompressedSize64: 4, UncompressedSize64: 64})
+ if err != nil {
+ t.Fatalf("create raw %s: %v", ch.name, err)
+ }
+ if _, err := w.Write([]byte{0xff, 0xff, 0xff, 0xff}); err != nil {
+ t.Fatalf("write raw %s: %v", ch.name, err)
+ }
+ default:
+ write("OEBPS/"+ch.name, ch.content)
+ }
+ }
+ if err := zw.Close(); err != nil {
+ t.Fatalf("close zip: %v", err)
+ }
+ return buf.Bytes()
+}
+
+func parseTestEPUB(t *testing.T, chapters []epubTestChapter) ParseResult {
+ t.Helper()
+ return NewEPUBParser().ParseWithResult(context.Background(), "book.epub", buildTestEPUB(t, chapters))
+}
+
+func epubTestText(res ParseResult) string {
+ var parts []string
+ for _, item := range res.JSON {
+ text, _ := item["text"].(string)
+ parts = append(parts, text)
+ }
+ return strings.Join(parts, "\n")
+}
+
+func TestEPUBParser_IntactBookReadsEveryChapter(t *testing.T) {
+ res := parseTestEPUB(t, []epubTestChapter{
+ {name: "ch1.xhtml", content: epubTestHTML("<p>ALPHA chapter</p>")},
+ {name: "ch2.xhtml", content: epubTestHTML("<p>BRAVO chapter</p>")},
+ })
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ text := epubTestText(res)
+ for _, want := range []string{"ALPHA", "BRAVO"} {
+ if !strings.Contains(text, want) {
+ t.Errorf("missing %q in %q", want, text)
+ }
+ }
+}
+
+func TestEPUBParser_OneUnreadableChapterKeepsTheRest(t *testing.T) {
+ res := parseTestEPUB(t, []epubTestChapter{
+ {name: "ch1.xhtml", content: epubTestHTML("<p>ALPHA chapter</p>")},
+ {name: "ch2.xhtml", damaged: true},
+ {name: "ch3.xhtml", missing: true},
+ })
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ if text := epubTestText(res); !strings.Contains(text, "ALPHA") {
+ t.Errorf("readable chapter lost: %q", text)
+ }
+}
+
+func TestEPUBParser_NoReadableChapterIsAnError(t *testing.T) {
+ cases := []struct {
+ name string
+ chapters []epubTestChapter
+ want string
+ }{
+ {
+ name: "every spine item missing",
+ chapters: []epubTestChapter{{name: "ch1.xhtml", missing: true}, {name: "ch2.xhtml", missing: true}},
+ want: "epub: no readable content: 2 of 2 content items could not be read (ch1.xhtml: ",
+ },
+ {
+ name: "every spine item damaged",
+ chapters: []epubTestChapter{{name: "ch1.xhtml", damaged: true}, {name: "ch2.xhtml", damaged: true}},
+ want: "epub: no readable content: 2 of 2 content items could not be read (ch1.xhtml: ",
+ },
+ {
+ // An empty item is neither read nor failed, so nothing readable is left.
+ name: "empty chapter next to an unreadable one",
+ chapters: []epubTestChapter{{name: "ch1.xhtml", content: ""}, {name: "ch2.xhtml", damaged: true}},
+ want: "epub: no readable content: 1 of 2 content items could not be read (ch2.xhtml: ",
+ },
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ res := parseTestEPUB(t, tc.chapters)
+ if res.Err == nil {
+ t.Fatalf("want an error, got items %v", res.JSON)
+ }
+ if !strings.Contains(res.Err.Error(), tc.want) {
+ t.Errorf("error = %q, want it to contain %q", res.Err, tc.want)
+ }
+ })
+ }
+}
+
+func TestEPUBParser_ImageOnlyChapterCountsAsRead(t *testing.T) {
+ // The image-only chapter was read and has no text; the book is not unreadable.
+ // No <title>: stripHTMLTags keeps its text, and the chapter would not be text-free.
+ imageOnly := `<?xml version="1.0" encoding="utf-8"?><html xmlns="http://www.w3.org/1999/xhtml"><body><img src="plate.png" alt=""/></body></html>`
+ res := parseTestEPUB(t, []epubTestChapter{
+ {name: "ch1.xhtml", content: imageOnly},
+ {name: "ch2.xhtml", damaged: true},
+ })
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ if text := epubTestText(res); text != "" {
+ t.Fatalf("image-only chapter produced text %q", text)
+ }
+}
+
+func TestEPUBParser_OnlyEmptyChaptersIsNotAnError(t *testing.T) {
+ res := parseTestEPUB(t, []epubTestChapter{{name: "ch1.xhtml", content: ""}})
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+}
diff --git a/test/unit_test/deepdoc/parser/test_epub_parser.py b/test/unit_test/deepdoc/parser/test_epub_parser.py
index e6fa872f3e..2da1067e45 100644
--- a/test/unit_test/deepdoc/parser/test_epub_parser.py
+++ b/test/unit_test/deepdoc/parser/test_epub_parser.py
@@ -26,12 +26,17 @@ Tests cover:
"""
import importlib.util
+import lzma
import os
import sys
import zipfile
+import zlib
from io import BytesIO
from unittest import mock
+import pytest
+from bs4 import ParserRejectedMarkup
+
# Import RAGFlowEpubParser directly by file path to avoid triggering
# deepdoc/parser/__init__.py which pulls in heavy dependencies
# (pdfplumber, xgboost, etc.) that may not be available in test environments.
@@ -86,7 +91,7 @@ _epub_spec.loader.exec_module(_epub_mod)
RAGFlowEpubParser = _epub_mod.RAGFlowEpubParser
-def _make_epub(chapters, include_container=True, spine_order=None):
+def _make_epub(chapters, include_container=True, spine_order=None, compression=zipfile.ZIP_DEFLATED):
"""Build a minimal EPUB ZIP in memory.
Args:
@@ -94,9 +99,10 @@ def _make_epub(chapters, include_container=True, spine_order=None):
include_container: whether to include META-INF/container.xml.
spine_order: optional list of filenames for spine ordering.
Defaults to the order of `chapters`.
+ compression: the zipfile compression method for every member.
"""
buf = BytesIO()
- with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
+ with zipfile.ZipFile(buf, "w", compression) as zf:
zf.writestr("mimetype", "application/epub+zip")
if include_container:
@@ -378,6 +384,37 @@ def _set_encrypted_flag(payload: bytes, member: bytes) -> bytes:
return bytes(data)
+def _damage_member_data(payload: bytes, member: str, position: int, value: int) -> bytes:
+ """Overwrite one byte of `member`'s compressed data, just past its local file header."""
+ import struct
+
+ with zipfile.ZipFile(BytesIO(payload)) as zf:
+ header_offset = zf.getinfo(member).header_offset
+ data = bytearray(payload)
+ name_len, extra_len = struct.unpack_from("<HH", data, header_offset + 26)
+ data[header_offset + 30 + name_len + extra_len + position] = value
+ return bytes(data)
+
+
+# Per compression method zipfile implements: a byte that breaks the stream, and the
+# error its decompressor raises for it before any CRC is checked.
+_DAMAGED_STREAMS = [
+ pytest.param(zipfile.ZIP_DEFLATED, 0, 0x07, zlib.error, id="deflate-reserved-block-type"),
+ pytest.param(zipfile.ZIP_BZIP2, 0, 0x00, OSError, id="bzip2-no-magic"),
+ pytest.param(zipfile.ZIP_LZMA, 4, 0xFF, lzma.LZMAError, id="lzma-invalid-properties"),
+]
+
+
+def _damaged_epub(chapters, damaged, compression, position, value, error):
+ """Build an EPUB whose `damaged` chapters fail to decompress, and check that they do."""
+ epub_bytes = _make_epub(chapters, compression=compression)
+ for name in damaged:
+ epub_bytes = _damage_member_data(epub_bytes, f"OEBPS/{name}", position, value)
+ with zipfile.ZipFile(BytesIO(epub_bytes)) as zf, pytest.raises(error):
+ zf.read(f"OEBPS/{name}")
+ return epub_bytes
+
+
class TestEpubParserUnreadableChapter:
"""One chapter the parser cannot read must not cost the whole book."""
@@ -388,9 +425,11 @@ class TestEpubParserUnreadableChapter:
]
def _parse(self, epub_bytes):
+ """Parse `epub_bytes` and join the sections into one string."""
return " ".join(RAGFlowEpubParser()(None, binary=epub_bytes, chunk_token_num=512))
def test_all_three_chapters_when_the_book_is_intact(self):
+ """The fixture book parses in full when nothing is broken."""
combined = self._parse(_make_epub(self._CHAPTERS))
assert "ALPHA" in combined
@@ -434,6 +473,17 @@ class TestEpubParserUnreadableChapter:
assert "CHARLIE" in combined
assert "BRAVO" not in combined
+ @pytest.mark.parametrize(("compression", "position", "value", "error"), _DAMAGED_STREAMS)
+ def test_a_chapter_whose_compressed_stream_is_damaged_is_skipped(self, compression, position, value, error):
+ """The decompressor fails before the CRC check, with an error of its own."""
+ epub_bytes = _damaged_epub(self._CHAPTERS, ["ch2.xhtml"], compression, position, value, error)
+
+ combined = self._parse(epub_bytes)
+
+ assert "ALPHA" in combined
+ assert "CHARLIE" in combined
+ assert "BRAVO" not in combined
+
def test_an_encrypted_chapter_is_skipped(self):
"""`zipfile.read` raises RuntimeError for an entry with the encrypted flag."""
epub_bytes = _set_encrypted_flag(_make_epub(self._CHAPTERS), b"OEBPS/ch2.xhtml")
@@ -455,3 +505,122 @@ class TestEpubParserUnreadableChapter:
assert "ALPHA" in combined
assert "CHARLIE" in combined
+
+ def test_a_chapter_with_rejected_markup_is_skipped(self):
+ """bs4 raises ParserRejectedMarkup when html.parser rejects the markup. Which markup that is depends on the CPython version, so the rejection is simulated."""
+ parser_txt = _epub_mod.RAGFlowHtmlParser.parser_txt
+
+ def reject_bravo(txt, chunk_token_num):
+ if "BRAVO" in txt:
+ raise ParserRejectedMarkup("rejected")
+ return parser_txt(txt, chunk_token_num)
+
+ with mock.patch.object(_epub_mod.RAGFlowHtmlParser, "parser_txt", side_effect=reject_bravo):
+ combined = self._parse(_make_epub(self._CHAPTERS))
+
+ assert "ALPHA" in combined
+ assert "CHARLIE" in combined
+ assert "BRAVO" not in combined
+
+ def test_a_chapter_nested_too_deep_to_walk_is_skipped(self):
+ """The HTML walker recurses once per element, so enough unclosed tags raise RecursionError."""
+ chapters = [
+ ("ch1.xhtml", _simple_html("ALPHA chapter")),
+ ("ch2.xhtml", _simple_html("<span>BRAVO " * sys.getrecursionlimit())),
+ ("ch3.xhtml", _simple_html("CHARLIE chapter")),
+ ]
+
+ combined = self._parse(_make_epub(chapters))
+
+ assert "ALPHA" in combined
+ assert "CHARLIE" in combined
+ assert "BRAVO" not in combined
+
+ def test_a_chapter_with_an_oversized_character_reference_is_skipped(self):
+ """A numeric character reference past Python's 4300-digit int limit raises ValueError."""
+ chapters = [
+ ("ch1.xhtml", _simple_html("ALPHA chapter")),
+ ("ch2.xhtml", _simple_html("BRAVO &#" + "1" * 5000 + "; chapter")),
+ ("ch3.xhtml", _simple_html("CHARLIE chapter")),
+ ]
+
+ combined = self._parse(_make_epub(chapters))
+
+ assert "ALPHA" in combined
+ assert "CHARLIE" in combined
+ assert "BRAVO" not in combined
+
+ def test_a_parser_bug_is_not_skipped_as_an_unreadable_chapter(self):
+ """Only failures caused by the chapter itself are skipped; any other error propagates."""
+ with mock.patch.object(_epub_mod.RAGFlowHtmlParser, "parser_txt", side_effect=TypeError("parser bug")), pytest.raises(TypeError, match="parser bug"):
+ self._parse(_make_epub(self._CHAPTERS))
+
+
+class TestEpubParserNothingReadable:
+ """A book in which no chapter could be read must fail instead of parsing to nothing."""
+
+ _CHAPTERS = TestEpubParserUnreadableChapter._CHAPTERS
+
+ def _parse(self, epub_bytes):
+ """Parse `epub_bytes` and return the sections."""
+ return RAGFlowEpubParser()(None, binary=epub_bytes, chunk_token_num=512)
+
+ def test_a_book_with_every_chapter_encrypted_raises(self):
+ """A DRM-protected book used to come back as zero sections."""
+ epub_bytes = _make_epub(self._CHAPTERS)
+ for name, _ in self._CHAPTERS:
+ epub_bytes = _set_encrypted_flag(epub_bytes, f"OEBPS/{name}".encode())
+
+ with pytest.raises(ValueError, match=r"No readable content in EPUB: 3 of 3 content items .* is encrypted"):
+ self._parse(epub_bytes)
+
+ @pytest.mark.parametrize(("compression", "position", "value", "error"), _DAMAGED_STREAMS)
+ def test_a_book_with_every_compressed_stream_damaged_raises(self, compression, position, value, error):
+ """Read failures from the decompressor count like any other."""
+ names = [name for name, _ in self._CHAPTERS]
+ epub_bytes = _damaged_epub(self._CHAPTERS, names, compression, position, value, error)
+
+ with pytest.raises(ValueError, match="No readable content in EPUB: 3 of 3 content items"):
+ self._parse(epub_bytes)
+
+ def test_a_book_with_every_chapter_undecodable_raises(self):
+ """Parse failures count as well as read failures."""
+ chapters = [
+ ("ch1.xhtml", bytes(range(256)) * 8),
+ ("ch2.xhtml", bytes(range(256)) * 8),
+ ]
+
+ with pytest.raises(ValueError, match="No readable content in EPUB: 2 of 2 content items"):
+ self._parse(_make_epub(chapters))
+
+ def test_a_book_whose_spine_points_only_at_missing_files_raises(self):
+ """A missing spine item is a failure too."""
+ # Rename the members in the ZIP headers, so every spine href points at nothing.
+ epub_bytes = _make_epub(self._CHAPTERS).replace(b"OEBPS/ch", b"OEBPS/xx")
+
+ with pytest.raises(ValueError, match=r"No readable content in EPUB: 3 of 3 content items .* no item named"):
+ self._parse(epub_bytes)
+
+ def test_an_empty_chapter_next_to_an_unreadable_one_raises(self):
+ """Nothing readable is left, even though only one of the two items failed."""
+ chapters = [
+ ("ch1.xhtml", b""),
+ ("ch2.xhtml", bytes(range(256)) * 8),
+ ]
+
+ with pytest.raises(ValueError, match="No readable content in EPUB: 1 of 2 content items"):
+ self._parse(_make_epub(chapters))
+
+ def test_an_image_only_chapter_next_to_an_unreadable_one_does_not_raise(self):
+ """A chapter that parses to no text was still read, so the book is not unreadable."""
+ image_only = "<?xml version='1.0' encoding='utf-8'?><html xmlns='http://www.w3.org/1999/xhtml'><head><title>Plate</title></head><body><img src='plate.png' alt=''/></body></html>"
+ chapters = [
+ ("ch1.xhtml", image_only),
+ ("ch2.xhtml", bytes(range(256)) * 8),
+ ]
+
+ assert self._parse(_make_epub(chapters)) == []
+
+ def test_a_book_whose_only_chapter_is_empty_still_returns_nothing(self):
+ """An empty chapter is not a failure, so there is nothing to report."""
+ assert self._parse(_make_epub([("ch1.xhtml", b"")])) == []