From 33ea645f1dd1a75ecc8e3220e82a33edac825e7f Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 15 Sep 2022 15:45:20 -0700 Subject: [PATCH] make sure we iterate through possible returns from rglob --- ui/medashare/btv/__init__.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/ui/medashare/btv/__init__.py b/ui/medashare/btv/__init__.py index 208ba7c..8c1b9ae 100644 --- a/ui/medashare/btv/__init__.py +++ b/ui/medashare/btv/__init__.py @@ -144,11 +144,15 @@ def validate_file(fname): finddname = glob_escape(torrent['info']['name'].decode(_encoding)) - dirname = list(fname.parent.rglob(finddname))[0] + for dirname in fname.parent.rglob(finddname): + tordir = dirname.parent - tordir = dirname.parent - - return validate(torrent, tordir) + try: + return validate(torrent, tordir) + except FileNotFoundError as e: + continue + else: + raise FileNotFoundError('unable to find directory for %s' % (repr(fname.name))) def validate(torrent, basedir): '''Take a decode torrent file, where it was stored in basedir, @@ -256,7 +260,25 @@ class _TestCases(unittest.TestCase): self.make_files(sd, self.origfiledata) - good, bad = validate_file(tf) + origrglob = pathlib.PosixPath.rglob + + def _rglob_patch(b): + return () + + with unittest.mock.patch.object(pathlib.PosixPath, 'rglob', + side_effect=_rglob_patch): + self.assertRaisesRegex(FileNotFoundError, + 'unable to find directory for \'a.torrent\'', + validate_file, tf) + + def _rglob_patch(b): + return list(pathlib.PosixPath(x) for x in + [ 'randomdir', 'somedir' ]) + \ + list(origrglob(pathlib.PosixPath('.'), 'somedir')) + + with unittest.mock.patch.object(pathlib.PosixPath, 'rglob', + side_effect=_rglob_patch): + good, bad = validate_file(tf) self.assertFalse(bad)