Skip to content

Commit

Permalink
Merge pull request #5087 from MetRonnie/scandir
Browse files Browse the repository at this point in the history
Fix `FileNotFoundError` when scanning path that doesn't exist
  • Loading branch information
hjoliver authored Aug 18, 2022
2 parents 38475b4 + fe793e3 commit 38c932e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
5 changes: 4 additions & 1 deletion cylc/flow/network/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ def _scan_subdirs(listing: List[Path], depth: int) -> None:
)

# perform the first directory listing
scan_dir_listing = await scandir(scan_dir)
try:
scan_dir_listing = await scandir(scan_dir)
except FileNotFoundError:
return
if scan_dir != cylc_run_dir and dir_is_flow(scan_dir_listing):
# If the scan_dir itself is a workflow run dir, yield nothing
return
Expand Down
12 changes: 9 additions & 3 deletions tests/functional/cylc-clean/00-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if ! command -v 'tree' >'/dev/null'; then
skip_all '"tree" command not available'
fi
set_test_number 8
set_test_number 10

# Generate random name for symlink dirs to avoid any clashes with other tests
SYM_NAME="$(mktemp -u)"
Expand Down Expand Up @@ -94,7 +94,7 @@ ${TEST_DIR}/${SYM_NAME}/work/cylc-run/${CYLC_TEST_REG_BASE}
\`-- work
__TREE__
# -----------------------------------------------------------------------------
TEST_NAME="cylc-clean"
TEST_NAME="clean"
run_ok "$TEST_NAME" cylc clean "$WORKFLOW_NAME"
dump_std "$TEST_NAME"
# -----------------------------------------------------------------------------
Expand All @@ -109,5 +109,11 @@ ${TEST_DIR}/${SYM_NAME}/log/cylc-run/${CYLC_TEST_REG_BASE}
\`-- leave-me-alone
__TREE__
# -----------------------------------------------------------------------------
TEST_NAME="clean-non-exist"
run_ok "$TEST_NAME" cylc clean "$WORKFLOW_NAME"
dump_std "$TEST_NAME"
cmp_ok "${TEST_NAME}.stdout" << __EOF__
INFO - No directory to clean at ${WORKFLOW_RUN_DIR}
__EOF__
# -----------------------------------------------------------------------------
purge
exit
41 changes: 0 additions & 41 deletions tests/integration/test_async_util.py

This file was deleted.

7 changes: 7 additions & 0 deletions tests/integration/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ async def test_scan_nasty_symlinks(run_dir_with_nasty_symlinks):
]


async def test_scan_non_exist(tmp_path: Path):
"""Calling scan() on a scan_dir that doesn't exist should not raise."""
assert await listify(
scan(scan_dir=(tmp_path / 'HORSE'))
) == []


async def test_is_active(sample_run_dir):
"""It should filter flows by presence of a contact file."""
# running flows
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/test_async_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

import asyncio
import logging
from pathlib import Path
from random import random

import pytest

from cylc.flow.async_util import (
pipe,
asyncqgen
asyncqgen,
scandir,
)

LOG = logging.getLogger('test')
Expand Down Expand Up @@ -238,3 +240,23 @@ async def test_asyncqgen():
ret.append(item)

assert ret == [1, 2, 3]


async def test_scandir(tmp_path: Path):
"""It should list directory contents (including symlinks)."""
(tmp_path / 'a').touch()
(tmp_path / 'b').touch()
(tmp_path / 'c').symlink_to(tmp_path / 'b')

assert sorted(await scandir(tmp_path)) == [
Path(tmp_path, 'a'),
Path(tmp_path, 'b'),
Path(tmp_path, 'c')
]


async def test_scandir_non_exist(tmp_path: Path):
"""scandir() should raise FileNotFoundError if called on a path that
doesn't exist."""
with pytest.raises(FileNotFoundError):
await scandir(tmp_path / 'HORSE')

0 comments on commit 38c932e

Please sign in to comment.