Chore: use pathlib in remaining tests

This commit is contained in:
shamoon
2025-06-03 11:47:54 -07:00
parent 4e082f997c
commit bc2facc87f
4 changed files with 86 additions and 105 deletions

View File

@@ -1,5 +1,4 @@
import datetime
import os
import shutil
import stat
import tempfile
@@ -66,7 +65,7 @@ class CopyParser(_BaseTestParser):
def parse(self, document_path, mime_type, file_name=None):
self.text = "The text"
self.archive_path = os.path.join(self.tempdir, "archive.pdf")
self.archive_path = Path(self.tempdir / "archive.pdf")
shutil.copy(document_path, self.archive_path)
@@ -96,15 +95,16 @@ class FaultyGenericExceptionParser(_BaseTestParser):
def fake_magic_from_file(file, *, mime=False):
if mime:
if file.name.startswith("invalid_pdf"):
filepath = Path(file)
if filepath.name.startswith("invalid_pdf"):
return "application/octet-stream"
if os.path.splitext(file)[1] == ".pdf":
if filepath.suffix == ".pdf":
return "application/pdf"
elif os.path.splitext(file)[1] == ".png":
elif filepath.suffix == ".png":
return "image/png"
elif os.path.splitext(file)[1] == ".webp":
elif filepath.suffix == ".webp":
return "image/webp"
elif os.path.splitext(file)[1] == ".eml":
elif filepath.suffix == ".eml":
return "message/rfc822"
else:
return "unknown"
@@ -225,7 +225,7 @@ class TestConsumer(
self.assertEqual(document.content, "The Text")
self.assertEqual(
document.title,
os.path.splitext(os.path.basename(filename))[0],
Path(filename).stem,
)
self.assertIsNone(document.correspondent)
self.assertIsNone(document.document_type)
@@ -254,7 +254,7 @@ class TestConsumer(
# https://github.com/jonaswinkler/paperless-ng/discussions/1037
filename = self.get_test_file()
shadow_file = os.path.join(self.dirs.scratch_dir, "._sample.pdf")
shadow_file = Path(self.dirs.scratch_dir / "._sample.pdf")
shutil.copy(filename, shadow_file)
@@ -1082,8 +1082,8 @@ class PreConsumeTestCase(DirectoriesMixin, GetConsumerMixin, TestCase):
outfile.write("echo This message goes to stderr >&2")
# Make the file executable
st = os.stat(script.name)
os.chmod(script.name, st.st_mode | stat.S_IEXEC)
st = Path(script.name).stat()
Path(script.name).chmod(st.st_mode | stat.S_IEXEC)
with override_settings(PRE_CONSUME_SCRIPT=script.name):
with self.assertLogs("paperless.consumer", level="INFO") as cm:
@@ -1114,8 +1114,8 @@ class PreConsumeTestCase(DirectoriesMixin, GetConsumerMixin, TestCase):
outfile.write("exit 100\n")
# Make the file executable
st = os.stat(script.name)
os.chmod(script.name, st.st_mode | stat.S_IEXEC)
st = Path(script.name).stat()
Path(script.name).chmod(st.st_mode | stat.S_IEXEC)
with override_settings(PRE_CONSUME_SCRIPT=script.name):
with self.get_consumer(self.test_file) as c:
@@ -1237,8 +1237,8 @@ class PostConsumeTestCase(DirectoriesMixin, GetConsumerMixin, TestCase):
outfile.write("exit -500\n")
# Make the file executable
st = os.stat(script.name)
os.chmod(script.name, st.st_mode | stat.S_IEXEC)
st = Path(script.name).stat()
Path(script.name).chmod(st.st_mode | stat.S_IEXEC)
with override_settings(POST_CONSUME_SCRIPT=script.name):
doc = Document.objects.create(title="Test", mime_type="application/pdf")