Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override default --reload-dir if passing sub-directories of cwd #1700

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions tests/supervisors/test_reload.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import logging
import signal
import socket
Expand Down Expand Up @@ -208,26 +209,28 @@ def test_should_reload_when_directories_have_same_prefix(self, touch_soon) -> No
@pytest.mark.parametrize(
"reloader_class", [StatReload, WatchGodReload, WatchFilesReload]
)
@pytest.mark.parametrize("cwd_context", [as_cwd, contextlib.nullcontext])
def test_should_not_reload_when_only_subdirectory_is_watched(
self, touch_soon
self, touch_soon, cwd_context
) -> None:
app_dir = self.reload_path / "app"
app_dir_file = self.reload_path / "app" / "src" / "main.py"
root_file = self.reload_path / "main.py"
with cwd_context(self.reload_path):
app_dir = self.reload_path / "app"
app_dir_file = self.reload_path / "app" / "src" / "main.py"
root_file = self.reload_path / "main.py"

config = Config(
app="tests.test_config:asgi_app",
reload=True,
reload_dirs=[str(app_dir)],
)
reloader = self._setup_reloader(config)
config = Config(
app="tests.test_config:asgi_app",
reload=True,
reload_dirs=[str(app_dir)],
)
reloader = self._setup_reloader(config)

assert self._reload_tester(touch_soon, reloader, app_dir_file)
assert not self._reload_tester(
touch_soon, reloader, root_file, app_dir / "~ignored"
)
assert self._reload_tester(touch_soon, reloader, app_dir_file)
assert not self._reload_tester(
touch_soon, reloader, root_file, app_dir / "~ignored"
)

reloader.shutdown()
reloader.shutdown()

@pytest.mark.parametrize("reloader_class", [WatchFilesReload, WatchGodReload])
def test_override_defaults(self, touch_soon) -> None:
Expand Down Expand Up @@ -309,39 +312,40 @@ def test_should_detect_new_reload_dirs(


@pytest.mark.skipif(WatchFilesReload is None, reason="watchfiles not available")
def test_should_watch_one_dir_cwd(mocker, reload_directory_structure):
@pytest.mark.parametrize("cwd_context", [as_cwd, contextlib.nullcontext])
@pytest.mark.parametrize("reload_dirs", [False, True])
def test_default_watch_dir(
mocker, reload_directory_structure, cwd_context, reload_dirs
):
mock_watch = mocker.patch("uvicorn.supervisors.watchfilesreload.watch")
app_dir = reload_directory_structure / "app"
app_first_dir = reload_directory_structure / "app_first"

with as_cwd(reload_directory_structure):
with cwd_context(reload_directory_structure):
config = Config(
app="tests.test_config:asgi_app",
reload=True,
reload_dirs=[str(app_dir), str(app_first_dir)],
reload_dirs=[Path.cwd()] if reload_dirs else [],
)
WatchFilesReload(config, target=run, sockets=[])
mock_watch.assert_called_once()
assert mock_watch.call_args[0] == (Path.cwd(),)


@pytest.mark.skipif(WatchFilesReload is None, reason="watchfiles not available")
def test_should_watch_separate_dirs_outside_cwd(mocker, reload_directory_structure):
@pytest.mark.parametrize("cwd_context", [as_cwd, contextlib.nullcontext])
def test_should_watch_separate_dirs(mocker, reload_directory_structure, cwd_context):
mock_watch = mocker.patch("uvicorn.supervisors.watchfilesreload.watch")
app_dir = reload_directory_structure / "app"
app_first_dir = reload_directory_structure / "app_first"
config = Config(
app="tests.test_config:asgi_app",
reload=True,
reload_dirs=[str(app_dir), str(app_first_dir)],
)
WatchFilesReload(config, target=run, sockets=[])
mock_watch.assert_called_once()
assert set(mock_watch.call_args[0]) == {
app_dir,
app_first_dir,
Path.cwd(),
}

with cwd_context(reload_directory_structure):
config = Config(
app="tests.test_config:asgi_app",
reload=True,
reload_dirs=[str(app_dir), str(app_first_dir)],
)
WatchFilesReload(config, target=run, sockets=[])
mock_watch.assert_called_once()
assert set(mock_watch.call_args[0]) == {app_dir, app_first_dir}


def test_display_path_relative(tmp_path):
Expand Down
7 changes: 1 addition & 6 deletions uvicorn/supervisors/watchfilesreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ def __init__(
) -> None:
super().__init__(config, target, sockets)
self.reloader_name = "WatchFiles"
self.reload_dirs = []
for directory in config.reload_dirs:
if Path.cwd() not in directory.parents:
self.reload_dirs.append(directory)
if Path.cwd() not in self.reload_dirs:
self.reload_dirs.append(Path.cwd())
self.reload_dirs = list(config.reload_dirs)

self.watch_filter = FileFilter(config)
self.watcher = watch(
Expand Down