Skip to content

Commit

Permalink
bug: allow explicit hidden file paths in --reload-include (#2176)
Browse files Browse the repository at this point in the history
* bug: allow explicit hidden file paths in `--reload-include`

* remove modifying `default_excludes` array

* modify `should_watch_file` to handle explicit patching path

* modify watchfiles filter to handle explicit paths

---------

Co-authored-by: Michael Oliver <michael.oliver@biorelate.com>
  • Loading branch information
michaeloliverx and michaeloliverx committed Dec 8, 2023
1 parent 2c55f20 commit c55af77
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def reload_directory_structure(tmp_path_factory: pytest.TempPathFactory):
│   └── sub.py
├── ext
│   └── ext.jpg
├── .dotted
├── .dotted_dir
│   └── file.txt
└── main.py
"""
root = tmp_path_factory.mktemp("reload_directory")
Expand Down
26 changes: 26 additions & 0 deletions tests/supervisors/test_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,32 @@ def test_override_defaults(self, touch_soon) -> None:

reloader.shutdown()

@pytest.mark.parametrize(
"reloader_class",
[
pytest.param(WatchFilesReload, marks=skip_if_m1),
WatchGodReload,
],
)
def test_explicit_paths(self, touch_soon) -> None:
dotted_file = self.reload_path / ".dotted"
non_dotted_file = self.reload_path / "ext" / "ext.jpg"
python_file = self.reload_path / "main.py"

with as_cwd(self.reload_path):
config = Config(
app="tests.test_config:asgi_app",
reload=True,
reload_includes=[".dotted", "ext/ext.jpg"],
)
reloader = self._setup_reloader(config)

assert self._reload_tester(touch_soon, reloader, dotted_file)
assert self._reload_tester(touch_soon, reloader, non_dotted_file)
assert self._reload_tester(touch_soon, reloader, python_file)

reloader.shutdown()

@pytest.mark.skipif(WatchFilesReload is None, reason="watchfiles not available")
@pytest.mark.parametrize("reloader_class", [WatchFilesReload])
def test_watchfiles_no_changes(self) -> None:
Expand Down
3 changes: 3 additions & 0 deletions uvicorn/supervisors/watchfilesreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __init__(self, config: Config):
def __call__(self, path: Path) -> bool:
for include_pattern in self.includes:
if path.match(include_pattern):
if str(path).endswith(include_pattern):
return True

for exclude_dir in self.exclude_dirs:
if exclude_dir in path.parents:
return False
Expand Down
3 changes: 3 additions & 0 deletions uvicorn/supervisors/watchgodreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def should_watch_file(self, entry: "DirEntry") -> bool:
self.watched_files[entry.path] = False
return False
for include_pattern in self.includes:
if str(entry_path).endswith(include_pattern):
self.watched_files[entry.path] = True
return True
if entry_path.match(include_pattern):
for exclude_pattern in self.excludes:
if entry_path.match(exclude_pattern):
Expand Down

0 comments on commit c55af77

Please sign in to comment.