Skip to content

Commit b39b80f

Browse files
authored
fix: test file existence using metadata (ietf-tools#8292)
* fix: test file existance using metadata * fix: use Path more * fix: don't read the file to see if it exists * fix: more conservative error handling * chore: remove unused import
1 parent f76137e commit b39b80f

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

ietf/doc/models.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import datetime
66
import logging
7-
import io
87
import os
98

109
import django.db
@@ -530,16 +529,27 @@ def replaces(self):
530529
def replaced_by(self):
531530
return set([ r.document for r in self.related_that("replaces") ])
532531

533-
def text(self, size = -1):
532+
def _text_path(self):
534533
path = self.get_file_name()
535534
root, ext = os.path.splitext(path)
536535
txtpath = root+'.txt'
537536
if ext != '.txt' and os.path.exists(txtpath):
538537
path = txtpath
538+
return path
539+
540+
def text_exists(self):
541+
path = Path(self._text_path())
542+
return path.exists()
543+
544+
def text(self, size = -1):
545+
path = Path(self._text_path())
546+
if not path.exists():
547+
return None
539548
try:
540-
with io.open(path, 'rb') as file:
549+
with path.open('rb') as file:
541550
raw = file.read(size)
542-
except IOError:
551+
except IOError as e:
552+
log.log(f"Error reading text for {path}: {e}")
543553
return None
544554
text = None
545555
try:

ietf/doc/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,3 +3318,18 @@ def test_investigate(self):
33183318
self.assertEqual(r.status_code, 200)
33193319
q = PyQuery(r.content)
33203320
self.assertEqual(len(q("#id_name_fragment.is-invalid")), 1)
3321+
3322+
class LogIOErrorTests(TestCase):
3323+
3324+
def test_doc_text_io_error(self):
3325+
3326+
d = IndividualDraftFactory()
3327+
3328+
with mock.patch("ietf.doc.models.Path") as path_cls_mock:
3329+
with mock.patch("ietf.doc.models.log.log") as log_mock:
3330+
path_cls_mock.return_value.exists.return_value = True
3331+
path_cls_mock.return_value.open.return_value.__enter__.return_value.read.side_effect = IOError("Bad things happened")
3332+
text = d.text()
3333+
self.assertIsNone(text)
3334+
self.assertTrue(log_mock.called)
3335+
self.assertIn("Bad things happened", log_mock.call_args[0][0])

ietf/doc/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ def build_file_urls(doc: Union[Document, DocHistory]):
10811081
label = "plain text" if t == "txt" else t
10821082
file_urls.append((label, base + doc.name + "-" + doc.rev + "." + t))
10831083

1084-
if doc.text():
1084+
if doc.text_exists():
10851085
file_urls.append(("htmlized", urlreverse('ietf.doc.views_doc.document_html', kwargs=dict(name=doc.name, rev=doc.rev))))
10861086
file_urls.append(("pdfized", urlreverse('ietf.doc.views_doc.document_pdfized', kwargs=dict(name=doc.name, rev=doc.rev))))
10871087
file_urls.append(("bibtex", urlreverse('ietf.doc.views_doc.document_bibtex',kwargs=dict(name=doc.name,rev=doc.rev))))

0 commit comments

Comments
 (0)