From db5b39d9fd2119eadcf3916ac77069e3a70df921 Mon Sep 17 00:00:00 2001 From: Tyson Smith Date: Fri, 29 Sep 2023 11:02:28 -0700 Subject: [PATCH] [sapphire] Handle long file names --- sapphire/job.py | 6 ++++++ sapphire/test_job.py | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/sapphire/job.py b/sapphire/job.py index 658ccb8a..cc86ada5 100644 --- a/sapphire/job.py +++ b/sapphire/job.py @@ -6,6 +6,7 @@ """ from collections import namedtuple from enum import Enum, unique +from errno import ENAMETOOLONG from logging import getLogger from mimetypes import guess_type from os.path import splitext @@ -143,6 +144,11 @@ def lookup_resource(self, path): mime=self.lookup_mime(path), required=required, ) + except OSError as exc: + if exc.errno == ENAMETOOLONG: + # file name is too long to look up so ignore it + return None + raise # pragma: no cover except ValueError: # pragma: no cover # this is for compatibility with python versions < 3.8 # is_file() will raise if the path contains characters unsupported diff --git a/sapphire/test_job.py b/sapphire/test_job.py index ca472d88..3b37a46d 100644 --- a/sapphire/test_job.py +++ b/sapphire/test_job.py @@ -243,12 +243,21 @@ def test_job_08(tmp_path): def test_job_09(tmp_path): + """test Job.lookup_resource() with file name that is too long""" + (tmp_path / "test.txt").touch() + job = Job(tmp_path) + assert job.status == Served.NONE + assert job.pending == 1 + assert job.lookup_resource(f"/{'a' * 8192}.txt") is None + + +def test_job_10(tmp_path): """test Job with missing directory""" with raises(OSError): Job(tmp_path / "missing") -def test_job_10(tmp_path): +def test_job_11(tmp_path): """test Job.mark_served() and Job.served""" job = Job(tmp_path) assert not any(job.served) @@ -260,7 +269,7 @@ def test_job_10(tmp_path): assert "/some/include/path/inc.bin" in job.served -def test_job_11(): +def test_job_12(): """test Job.lookup_mime()""" assert Job.lookup_mime("unknown") == "application/octet-stream" # look up from Job.MIME_MAP