Skip to content

Commit

Permalink
fix: test file existence using metadata (#8292)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rjsparks authored Dec 5, 2024
1 parent f76137e commit b39b80f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
18 changes: 14 additions & 4 deletions ietf/doc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import datetime
import logging
import io
import os

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

def text(self, size = -1):
def _text_path(self):
path = self.get_file_name()
root, ext = os.path.splitext(path)
txtpath = root+'.txt'
if ext != '.txt' and os.path.exists(txtpath):
path = txtpath
return path

def text_exists(self):
path = Path(self._text_path())
return path.exists()

def text(self, size = -1):
path = Path(self._text_path())
if not path.exists():
return None
try:
with io.open(path, 'rb') as file:
with path.open('rb') as file:
raw = file.read(size)
except IOError:
except IOError as e:
log.log(f"Error reading text for {path}: {e}")
return None
text = None
try:
Expand Down
15 changes: 15 additions & 0 deletions ietf/doc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3318,3 +3318,18 @@ def test_investigate(self):
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
self.assertEqual(len(q("#id_name_fragment.is-invalid")), 1)

class LogIOErrorTests(TestCase):

def test_doc_text_io_error(self):

d = IndividualDraftFactory()

with mock.patch("ietf.doc.models.Path") as path_cls_mock:
with mock.patch("ietf.doc.models.log.log") as log_mock:
path_cls_mock.return_value.exists.return_value = True
path_cls_mock.return_value.open.return_value.__enter__.return_value.read.side_effect = IOError("Bad things happened")
text = d.text()
self.assertIsNone(text)
self.assertTrue(log_mock.called)
self.assertIn("Bad things happened", log_mock.call_args[0][0])
2 changes: 1 addition & 1 deletion ietf/doc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ def build_file_urls(doc: Union[Document, DocHistory]):
label = "plain text" if t == "txt" else t
file_urls.append((label, base + doc.name + "-" + doc.rev + "." + t))

if doc.text():
if doc.text_exists():
file_urls.append(("htmlized", urlreverse('ietf.doc.views_doc.document_html', kwargs=dict(name=doc.name, rev=doc.rev))))
file_urls.append(("pdfized", urlreverse('ietf.doc.views_doc.document_pdfized', kwargs=dict(name=doc.name, rev=doc.rev))))
file_urls.append(("bibtex", urlreverse('ietf.doc.views_doc.document_bibtex',kwargs=dict(name=doc.name,rev=doc.rev))))
Expand Down

0 comments on commit b39b80f

Please sign in to comment.