diff --git a/AUTHORS b/AUTHORS index 9629e00bcfb..0cd3d617fd6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -21,6 +21,7 @@ Alexander Johnson Alexander King Alexei Kozlenok Alice Purcell +Alison Day Allan Feldman Aly Sivji Amir Elkess diff --git a/changelog/13083.bugfix.rst b/changelog/13083.bugfix.rst new file mode 100644 index 00000000000..329cc70023e --- /dev/null +++ b/changelog/13083.bugfix.rst @@ -0,0 +1 @@ +Fixes an issue where the :func:`_pytest.pathlib.scandir` function crashes if the directory to scan does not exist by catching the ``FileNotFoundError`` and returning an empty array. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 25dc69b6349..0acb8291ec0 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -957,17 +957,21 @@ def scandir( The default is to sort by name. """ entries = [] - with os.scandir(path) as s: - # Skip entries with symlink loops and other brokenness, so the caller - # doesn't have to deal with it. - for entry in s: - try: - entry.is_file() - except OSError as err: - if _ignore_error(err): - continue - raise - entries.append(entry) + try: + with os.scandir(path) as s: + scanned = list(s) + except FileNotFoundError: + return [] + # Skip entries with symlink loops and other brokenness, so the caller + # doesn't have to deal with it. + for entry in scanned: + try: + entry.is_file() + except OSError as err: + if _ignore_error(err): + continue + raise + entries.append(entry) entries.sort(key=sort_key) # type: ignore[arg-type] return entries