Debian Codegraph @

tests/utils_tests/test_autoreload.py

raw
1 import contextlib
2 import os
3 import py_compile
4 import shutil
5 import sys
6 import tempfile
7 import threading
8 import time
9 import types
10 import weakref
11 import zipfile
12 import zoneinfo
13 from importlib import import_module
14 from pathlib import Path
15 from subprocess import CompletedProcess
16 from unittest import mock, skip, skipIf
17
18 import django.__main__
19 from django.apps.registry import Apps
20 from django.test import SimpleTestCase
21 from django.test.utils import extend_sys_path
22 from django.utils import autoreload
23 from django.utils.autoreload import WatchmanUnavailable
24
25 from .test_module import __main__ as test_main
26 from .test_module import main_module as test_main_module
27 from .utils import on_macos_with_hfs
28
29
30 class TestIterModulesAndFiles(SimpleTestCase):
31 def import_and_cleanup(self, name):
32 import_module(name)
33 self.addCleanup(lambda: sys.path_importer_cache.clear())
34 self.addCleanup(lambda: sys.modules.pop(name, None))
35
36 def clear_autoreload_caches(self):
37 autoreload.iter_modules_and_files.cache_clear()
38
39 def assertFileFound(self, filename):
40 # Some temp directories are symlinks. Python resolves these fully while
41 # importing.
42 resolved_filename = filename.resolve(strict=True)
43 self.clear_autoreload_caches()
44 # Test uncached access
45 self.assertIn(
46 resolved_filename, list(autoreload.iter_all_python_module_files())
47 )
48 # Test cached access
49 self.assertIn(
50 resolved_filename, list(autoreload.iter_all_python_module_files())
51 )
52 self.assertEqual(autoreload.iter_modules_and_files.cache_info().hits, 1)
53
54 def assertFileNotFound(self, filename):
55 resolved_filename = filename.resolve(strict=True)
56 self.clear_autoreload_caches()
57 # Test uncached access
58 self.assertNotIn(
59 resolved_filename, list(autoreload.iter_all_python_module_files())
60 )
61 # Test cached access
62 self.assertNotIn(
63 resolved_filename, list(autoreload.iter_all_python_module_files())
64 )
65 self.assertEqual(autoreload.iter_modules_and_files.cache_info().hits, 1)
66
67 def temporary_file(self, filename):
68 dirname = tempfile.mkdtemp()
69 self.addCleanup(shutil.rmtree, dirname)
70 return Path(dirname) / filename
71
72 def test_paths_are_pathlib_instances(self):
73 for filename in autoreload.iter_all_python_module_files():
74 self.assertIsInstance(filename, Path)
75
76 def test_file_added(self):
77 """
78 When a file is added, it's returned by iter_all_python_module_files().
79 """
80 filename = self.temporary_file("test_deleted_removed_module.py")
81 filename.touch()
82
83 with extend_sys_path(str(filename.parent)):
84 self.import_and_cleanup("test_deleted_removed_module")
85
86 self.assertFileFound(filename.absolute())
87
88 def test_check_errors(self):
89 """
90 When a file containing an error is imported in a function wrapped by
91 check_errors(), gen_filenames() returns it.
92 """
93 filename = self.temporary_file("test_syntax_error.py")
94 filename.write_text("Ceci n'est pas du Python.")
95
96 with extend_sys_path(str(filename.parent)):
97 try:
98 with self.assertRaises(SyntaxError):
99 autoreload.check_errors(import_module)("test_syntax_error")
100 finally:
101 autoreload._exception = None
102 self.assertFileFound(filename)
103
104 def test_check_errors_catches_all_exceptions(self):
105 """
106 Since Python may raise arbitrary exceptions when importing code,
107 check_errors() must catch Exception, not just some subclasses.
108 """
109 filename = self.temporary_file("test_exception.py")
110 filename.write_text("raise Exception")
111 with extend_sys_path(str(filename.parent)):
112 try:
113 with self.assertRaises(Exception):
114 autoreload.check_errors(import_module)("test_exception")
115 finally:
116 autoreload._exception = None
117 self.assertFileFound(filename)
118
119 def test_zip_reload(self):
120 """
121 Modules imported from zipped files have their archive location included
122 in the result.
123 """
124 zip_file = self.temporary_file("zip_import.zip")
125 with zipfile.ZipFile(str(zip_file), "w", zipfile.ZIP_DEFLATED) as zipf:
126 zipf.writestr("test_zipped_file.py", "")
127
128 with extend_sys_path(str(zip_file)):
129 self.import_and_cleanup("test_zipped_file")
130 self.assertFileFound(zip_file)
131
132 def test_bytecode_conversion_to_source(self):
133 """.pyc and .pyo files are included in the files list."""
134 filename = self.temporary_file("test_compiled.py")
135 filename.touch()
136 compiled_file = Path(
137 py_compile.compile(str(filename), str(filename.with_suffix(".pyc")))
138 )
139 filename.unlink()
140 with extend_sys_path(str(compiled_file.parent)):
141 self.import_and_cleanup("test_compiled")
142 self.assertFileFound(compiled_file)
143
144 def test_weakref_in_sys_module(self):
145 """iter_all_python_module_file() ignores weakref modules."""
146 time_proxy = weakref.proxy(time)
147 sys.modules["time_proxy"] = time_proxy
148 self.addCleanup(lambda: sys.modules.pop("time_proxy", None))
149 list(autoreload.iter_all_python_module_files()) # No crash.
150
151 def test_module_without_spec(self):
152 module = types.ModuleType("test_module")
153 del module.__spec__
154 self.assertEqual(
155 autoreload.iter_modules_and_files((module,), frozenset()), frozenset()
156 )
157
158 def test_main_module_is_resolved(self):
159 main_module = sys.modules["__main__"]
160 self.assertFileFound(Path(main_module.__file__))
161
162 def test_main_module_without_file_is_not_resolved(self):
163 fake_main = types.ModuleType("__main__")
164 self.assertEqual(
165 autoreload.iter_modules_and_files((fake_main,), frozenset()), frozenset()
166 )
167
168 def test_path_with_embedded_null_bytes(self):
169 for path in (
170 "embedded_null_byte\x00.py",
171 "di\x00rectory/embedded_null_byte.py",
172 ):
173 with self.subTest(path=path):
174 self.assertEqual(
175 autoreload.iter_modules_and_files((), frozenset([path])),
176 frozenset(),
177 )
178
179
180 class TestChildArguments(SimpleTestCase):
181 @mock.patch.dict(sys.modules, {"__main__": django.__main__})
182 @mock.patch("sys.argv", [django.__main__.__file__, "runserver"])
183 @mock.patch("sys.warnoptions", [])
184 @mock.patch("sys._xoptions", {})
185 def test_run_as_module(self):
186 self.assertEqual(
187 autoreload.get_child_arguments(),
188 [sys.executable, "-m", "django", "runserver"],
189 )
190
191 @mock.patch.dict(sys.modules, {"__main__": test_main})
192 @mock.patch("sys.argv", [test_main.__file__, "runserver"])
193 @mock.patch("sys.warnoptions", [])
194 @mock.patch("sys._xoptions", {})
195 def test_run_as_non_django_module(self):
196 self.assertEqual(
197 autoreload.get_child_arguments(),
198 [sys.executable, "-m", "utils_tests.test_module", "runserver"],
199 )
200
201 @mock.patch.dict(sys.modules, {"__main__": test_main_module})
202 @mock.patch("sys.argv", [test_main.__file__, "runserver"])
203 @mock.patch("sys.warnoptions", [])
204 @mock.patch("sys._xoptions", {})
205 def test_run_as_non_django_module_non_package(self):
206 self.assertEqual(
207 autoreload.get_child_arguments(),
208 [sys.executable, "-m", "utils_tests.test_module.main_module", "runserver"],
209 )
210
211 @mock.patch("__main__.__spec__", None)
212 @mock.patch("sys.argv", [__file__, "runserver"])
213 @mock.patch("sys.warnoptions", ["error"])
214 @mock.patch("sys._xoptions", {})
215 def test_warnoptions(self):
216 self.assertEqual(
217 autoreload.get_child_arguments(),
218 [sys.executable, "-Werror", __file__, "runserver"],
219 )
220
221 @mock.patch("sys.argv", [__file__, "runserver"])
222 @mock.patch("sys.warnoptions", [])
223 @mock.patch("sys._xoptions", {"utf8": True, "a": "b"})
224 def test_xoptions(self):
225 self.assertEqual(
226 autoreload.get_child_arguments(),
227 [sys.executable, "-Xutf8", "-Xa=b", __file__, "runserver"],
228 )
229
230 @mock.patch("__main__.__spec__", None)
231 @mock.patch("sys.warnoptions", [])
232 def test_exe_fallback(self):
233 with tempfile.TemporaryDirectory() as tmpdir:
234 exe_path = Path(tmpdir) / "django-admin.exe"
235 exe_path.touch()
236 with mock.patch("sys.argv", [exe_path.with_suffix(""), "runserver"]):
237 self.assertEqual(
238 autoreload.get_child_arguments(), [exe_path, "runserver"]
239 )
240
241 @mock.patch("sys.warnoptions", [])
242 @mock.patch.dict(sys.modules, {"__main__": django.__main__})
243 def test_use_exe_when_main_spec(self):
244 with tempfile.TemporaryDirectory() as tmpdir:
245 exe_path = Path(tmpdir) / "django-admin.exe"
246 exe_path.touch()
247 with mock.patch("sys.argv", [exe_path.with_suffix(""), "runserver"]):
248 self.assertEqual(
249 autoreload.get_child_arguments(), [exe_path, "runserver"]
250 )
251
252 @mock.patch("__main__.__spec__", None)
253 @mock.patch("sys.warnoptions", [])
254 @mock.patch("sys._xoptions", {})
255 def test_entrypoint_fallback(self):
256 with tempfile.TemporaryDirectory() as tmpdir:
257 script_path = Path(tmpdir) / "django-admin-script.py"
258 script_path.touch()
259 with mock.patch(
260 "sys.argv", [script_path.with_name("django-admin"), "runserver"]
261 ):
262 self.assertEqual(
263 autoreload.get_child_arguments(),
264 [sys.executable, script_path, "runserver"],
265 )
266
267 @mock.patch("__main__.__spec__", None)
268 @mock.patch("sys.argv", ["does-not-exist", "runserver"])
269 @mock.patch("sys.warnoptions", [])
270 def test_raises_runtimeerror(self):
271 msg = "Script does-not-exist does not exist."
272 with self.assertRaisesMessage(RuntimeError, msg):
273 autoreload.get_child_arguments()
274
275 @mock.patch("sys.argv", [__file__, "runserver"])
276 @mock.patch("sys.warnoptions", [])
277 @mock.patch("sys._xoptions", {})
278 def test_module_no_spec(self):
279 module = types.ModuleType("test_module")
280 del module.__spec__
281 with mock.patch.dict(sys.modules, {"__main__": module}):
282 self.assertEqual(
283 autoreload.get_child_arguments(),
284 [sys.executable, __file__, "runserver"],
285 )
286
287
288 class TestUtilities(SimpleTestCase):
289 def test_is_django_module(self):
290 for module, expected in ((zoneinfo, False), (sys, False), (autoreload, True)):
291 with self.subTest(module=module):
292 self.assertIs(autoreload.is_django_module(module), expected)
293
294 def test_is_django_path(self):
295 for module, expected in (
296 (zoneinfo.__file__, False),
297 (contextlib.__file__, False),
298 (autoreload.__file__, True),
299 ):
300 with self.subTest(module=module):
301 self.assertIs(autoreload.is_django_path(module), expected)
302
303
304 class TestCommonRoots(SimpleTestCase):
305 def test_common_roots(self):
306 paths = (
307 Path("/first/second"),
308 Path("/first/second/third"),
309 Path("/first/"),
310 Path("/root/first/"),
311 )
312 results = autoreload.common_roots(paths)
313 self.assertCountEqual(results, [Path("/first/"), Path("/root/first/")])
314
315
316 class TestSysPathDirectories(SimpleTestCase):
317 def setUp(self):
318 _directory = tempfile.TemporaryDirectory()
319 self.addCleanup(_directory.cleanup)
320 self.directory = Path(_directory.name).resolve(strict=True).absolute()
321 self.file = self.directory / "test"
322 self.file.touch()
323
324 def test_sys_paths_with_directories(self):
325 with extend_sys_path(str(self.file)):
326 paths = list(autoreload.sys_path_directories())
327 self.assertIn(self.file.parent, paths)
328
329 def test_sys_paths_non_existing(self):
330 nonexistent_file = Path(self.directory.name) / "does_not_exist"
331 with extend_sys_path(str(nonexistent_file)):
332 paths = list(autoreload.sys_path_directories())
333 self.assertNotIn(nonexistent_file, paths)
334 self.assertNotIn(nonexistent_file.parent, paths)
335
336 def test_sys_paths_absolute(self):
337 paths = list(autoreload.sys_path_directories())
338 self.assertTrue(all(p.is_absolute() for p in paths))
339
340 def test_sys_paths_directories(self):
341 with extend_sys_path(str(self.directory)):
342 paths = list(autoreload.sys_path_directories())
343 self.assertIn(self.directory, paths)
344
345
346 class GetReloaderTests(SimpleTestCase):
347 @mock.patch("django.utils.autoreload.WatchmanReloader")
348 def test_watchman_unavailable(self, mocked_watchman):
349 mocked_watchman.check_availability.side_effect = WatchmanUnavailable
350 self.assertIsInstance(autoreload.get_reloader(), autoreload.StatReloader)
351
352 @mock.patch.object(autoreload.WatchmanReloader, "check_availability")
353 def test_watchman_available(self, mocked_available):
354 # If WatchmanUnavailable isn't raised, Watchman will be chosen.
355 mocked_available.return_value = None
356 result = autoreload.get_reloader()
357 self.assertIsInstance(result, autoreload.WatchmanReloader)
358
359
360 class RunWithReloaderTests(SimpleTestCase):
361 @mock.patch.dict(os.environ, {autoreload.DJANGO_AUTORELOAD_ENV: "true"})
362 @mock.patch("django.utils.autoreload.get_reloader")
363 def test_swallows_keyboard_interrupt(self, mocked_get_reloader):
364 mocked_get_reloader.side_effect = KeyboardInterrupt()
365 autoreload.run_with_reloader(lambda: None) # No exception
366
367 @mock.patch.dict(os.environ, {autoreload.DJANGO_AUTORELOAD_ENV: "false"})
368 @mock.patch("django.utils.autoreload.restart_with_reloader")
369 def test_calls_sys_exit(self, mocked_restart_reloader):
370 mocked_restart_reloader.return_value = 1
371 with self.assertRaises(SystemExit) as exc:
372 autoreload.run_with_reloader(lambda: None)
373 self.assertEqual(exc.exception.code, 1)
374
375 @mock.patch.dict(os.environ, {autoreload.DJANGO_AUTORELOAD_ENV: "true"})
376 @mock.patch("django.utils.autoreload.start_django")
377 @mock.patch("django.utils.autoreload.get_reloader")
378 def test_calls_start_django(self, mocked_reloader, mocked_start_django):
379 mocked_reloader.return_value = mock.sentinel.RELOADER
380 autoreload.run_with_reloader(mock.sentinel.METHOD)
381 self.assertEqual(mocked_start_django.call_count, 1)
382 self.assertSequenceEqual(
383 mocked_start_django.call_args[0],
384 [mock.sentinel.RELOADER, mock.sentinel.METHOD],
385 )
386
387
388 class StartDjangoTests(SimpleTestCase):
389 @mock.patch("django.utils.autoreload.ensure_echo_on")
390 def test_echo_on_called(self, mocked_echo):
391 fake_reloader = mock.MagicMock()
392 autoreload.start_django(fake_reloader, lambda: None)
393 self.assertEqual(mocked_echo.call_count, 1)
394
395 @mock.patch("django.utils.autoreload.check_errors")
396 def test_check_errors_called(self, mocked_check_errors):
397 fake_method = mock.MagicMock(return_value=None)
398 fake_reloader = mock.MagicMock()
399 autoreload.start_django(fake_reloader, fake_method)
400 self.assertCountEqual(mocked_check_errors.call_args[0], [fake_method])
401
402 @mock.patch("threading.Thread")
403 @mock.patch("django.utils.autoreload.check_errors")
404 def test_starts_thread_with_args(self, mocked_check_errors, mocked_thread):
405 fake_reloader = mock.MagicMock()
406 fake_main_func = mock.MagicMock()
407 fake_thread = mock.MagicMock()
408 mocked_check_errors.return_value = fake_main_func
409 mocked_thread.return_value = fake_thread
410 autoreload.start_django(fake_reloader, fake_main_func, 123, abc=123)
411 self.assertEqual(mocked_thread.call_count, 1)
412 self.assertEqual(
413 mocked_thread.call_args[1],
414 {
415 "target": fake_main_func,
416 "args": (123,),
417 "kwargs": {"abc": 123},
418 "name": "django-main-thread",
419 },
420 )
421 self.assertIs(fake_thread.daemon, True)
422 self.assertTrue(fake_thread.start.called)
423
424
425 class TestCheckErrors(SimpleTestCase):
426 def test_mutates_error_files(self):
427 fake_method = mock.MagicMock(side_effect=RuntimeError())
428 wrapped = autoreload.check_errors(fake_method)
429 with mock.patch.object(autoreload, "_error_files") as mocked_error_files:
430 try:
431 with self.assertRaises(RuntimeError):
432 wrapped()
433 finally:
434 autoreload._exception = None
435 self.assertEqual(mocked_error_files.append.call_count, 1)
436
437
438 class TestRaiseLastException(SimpleTestCase):
439 @mock.patch("django.utils.autoreload._exception", None)
440 def test_no_exception(self):
441 # Should raise no exception if _exception is None
442 autoreload.raise_last_exception()
443
444 def test_raises_exception(self):
445 class MyException(Exception):
446 pass
447
448 # Create an exception
449 try:
450 raise MyException("Test Message")
451 except MyException:
452 exc_info = sys.exc_info()
453
454 with mock.patch("django.utils.autoreload._exception", exc_info):
455 with self.assertRaisesMessage(MyException, "Test Message"):
456 autoreload.raise_last_exception()
457
458 def test_raises_custom_exception(self):
459 class MyException(Exception):
460 def __init__(self, msg, extra_context):
461 super().__init__(msg)
462 self.extra_context = extra_context
463
464 # Create an exception.
465 try:
466 raise MyException("Test Message", "extra context")
467 except MyException:
468 exc_info = sys.exc_info()
469
470 with mock.patch("django.utils.autoreload._exception", exc_info):
471 with self.assertRaisesMessage(MyException, "Test Message"):
472 autoreload.raise_last_exception()
473
474 def test_raises_exception_with_context(self):
475 try:
476 raise Exception(2)
477 except Exception as e:
478 try:
479 raise Exception(1) from e
480 except Exception:
481 exc_info = sys.exc_info()
482
483 with mock.patch("django.utils.autoreload._exception", exc_info):
484 with self.assertRaises(Exception) as cm:
485 autoreload.raise_last_exception()
486 self.assertEqual(cm.exception.args[0], 1)
487 self.assertEqual(cm.exception.__cause__.args[0], 2)
488
489
490 class RestartWithReloaderTests(SimpleTestCase):
491 executable = "/usr/bin/python"
492
493 def patch_autoreload(self, argv):
494 patch_call = mock.patch(
495 "django.utils.autoreload.subprocess.run",
496 return_value=CompletedProcess(argv, 0),
497 )
498 patches = [
499 mock.patch("django.utils.autoreload.sys.argv", argv),
500 mock.patch("django.utils.autoreload.sys.executable", self.executable),
501 mock.patch("django.utils.autoreload.sys.warnoptions", ["all"]),
502 mock.patch("django.utils.autoreload.sys._xoptions", {}),
503 ]
504 for p in patches:
505 p.start()
506 self.addCleanup(p.stop)
507 mock_call = patch_call.start()
508 self.addCleanup(patch_call.stop)
509 return mock_call
510
511 def test_manage_py(self):
512 with tempfile.TemporaryDirectory() as temp_dir:
513 script = Path(temp_dir) / "manage.py"
514 script.touch()
515 argv = [str(script), "runserver"]
516 mock_call = self.patch_autoreload(argv)
517 with mock.patch("__main__.__spec__", None):
518 autoreload.restart_with_reloader()
519 self.assertEqual(mock_call.call_count, 1)
520 self.assertEqual(
521 mock_call.call_args[0][0],
522 [self.executable, "-Wall"] + argv,
523 )
524
525 def test_python_m_django(self):
526 main = "/usr/lib/pythonX.Y/site-packages/django/__main__.py"
527 argv = [main, "runserver"]
528 mock_call = self.patch_autoreload(argv)
529 with mock.patch("django.__main__.__file__", main):
530 with mock.patch.dict(sys.modules, {"__main__": django.__main__}):
531 autoreload.restart_with_reloader()
532 self.assertEqual(mock_call.call_count, 1)
533 self.assertEqual(
534 mock_call.call_args[0][0],
535 [self.executable, "-Wall", "-m", "django"] + argv[1:],
536 )
537
538
539 class ReloaderTests(SimpleTestCase):
540 RELOADER_CLS = None
541
542 def setUp(self):
543 _tempdir = tempfile.TemporaryDirectory()
544 self.tempdir = Path(_tempdir.name).resolve(strict=True).absolute()
545 self.existing_file = self.ensure_file(self.tempdir / "test.py")
546 self.nonexistent_file = (self.tempdir / "does_not_exist.py").absolute()
547 self.reloader = self.RELOADER_CLS()
548 self.addCleanup(self.reloader.stop)
549 self.addCleanup(_tempdir.cleanup)
550
551 def ensure_file(self, path):
552 path.parent.mkdir(exist_ok=True, parents=True)
553 path.touch()
554 # On Linux and Windows updating the mtime of a file using touch() will
555 # set a timestamp value that is in the past, as the time value for the
556 # last kernel tick is used rather than getting the correct absolute
557 # time.
558 # To make testing simpler set the mtime to be the observed time when
559 # this function is called.
560 self.set_mtime(path, time.time())
561 return path.absolute()
562
563 def set_mtime(self, fp, value):
564 os.utime(str(fp), (value, value))
565
566 def increment_mtime(self, fp, by=1):
567 current_time = time.time()
568 self.set_mtime(fp, current_time + by)
569
570 @contextlib.contextmanager
571 def tick_twice(self):
572 ticker = self.reloader.tick()
573 next(ticker)
574 yield
575 next(ticker)
576
577
578 class IntegrationTests:
579 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
580 @mock.patch(
581 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
582 )
583 def test_glob(self, mocked_modules, notify_mock):
584 non_py_file = self.ensure_file(self.tempdir / "non_py_file")
585 self.reloader.watch_dir(self.tempdir, "*.py")
586 with self.tick_twice():
587 self.increment_mtime(non_py_file)
588 self.increment_mtime(self.existing_file)
589 self.assertEqual(notify_mock.call_count, 1)
590 self.assertCountEqual(notify_mock.call_args[0], [self.existing_file])
591
592 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
593 @mock.patch(
594 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
595 )
596 def test_multiple_globs(self, mocked_modules, notify_mock):
597 self.ensure_file(self.tempdir / "x.test")
598 self.reloader.watch_dir(self.tempdir, "*.py")
599 self.reloader.watch_dir(self.tempdir, "*.test")
600 with self.tick_twice():
601 self.increment_mtime(self.existing_file)
602 self.assertEqual(notify_mock.call_count, 1)
603 self.assertCountEqual(notify_mock.call_args[0], [self.existing_file])
604
605 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
606 @mock.patch(
607 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
608 )
609 def test_overlapping_globs(self, mocked_modules, notify_mock):
610 self.reloader.watch_dir(self.tempdir, "*.py")
611 self.reloader.watch_dir(self.tempdir, "*.p*")
612 with self.tick_twice():
613 self.increment_mtime(self.existing_file)
614 self.assertEqual(notify_mock.call_count, 1)
615 self.assertCountEqual(notify_mock.call_args[0], [self.existing_file])
616
617 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
618 @mock.patch(
619 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
620 )
621 def test_glob_recursive(self, mocked_modules, notify_mock):
622 non_py_file = self.ensure_file(self.tempdir / "dir" / "non_py_file")
623 py_file = self.ensure_file(self.tempdir / "dir" / "file.py")
624 self.reloader.watch_dir(self.tempdir, "**/*.py")
625 with self.tick_twice():
626 self.increment_mtime(non_py_file)
627 self.increment_mtime(py_file)
628 self.assertEqual(notify_mock.call_count, 1)
629 self.assertCountEqual(notify_mock.call_args[0], [py_file])
630
631 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
632 @mock.patch(
633 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
634 )
635 def test_multiple_recursive_globs(self, mocked_modules, notify_mock):
636 non_py_file = self.ensure_file(self.tempdir / "dir" / "test.txt")
637 py_file = self.ensure_file(self.tempdir / "dir" / "file.py")
638 self.reloader.watch_dir(self.tempdir, "**/*.txt")
639 self.reloader.watch_dir(self.tempdir, "**/*.py")
640 with self.tick_twice():
641 self.increment_mtime(non_py_file)
642 self.increment_mtime(py_file)
643 self.assertEqual(notify_mock.call_count, 2)
644 self.assertCountEqual(
645 notify_mock.call_args_list, [mock.call(py_file), mock.call(non_py_file)]
646 )
647
648 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
649 @mock.patch(
650 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
651 )
652 def test_nested_glob_recursive(self, mocked_modules, notify_mock):
653 inner_py_file = self.ensure_file(self.tempdir / "dir" / "file.py")
654 self.reloader.watch_dir(self.tempdir, "**/*.py")
655 self.reloader.watch_dir(inner_py_file.parent, "**/*.py")
656 with self.tick_twice():
657 self.increment_mtime(inner_py_file)
658 self.assertEqual(notify_mock.call_count, 1)
659 self.assertCountEqual(notify_mock.call_args[0], [inner_py_file])
660
661 @mock.patch("django.utils.autoreload.BaseReloader.notify_file_changed")
662 @mock.patch(
663 "django.utils.autoreload.iter_all_python_module_files", return_value=frozenset()
664 )
665 def test_overlapping_glob_recursive(self, mocked_modules, notify_mock):
666 py_file = self.ensure_file(self.tempdir / "dir" / "file.py")
667 self.reloader.watch_dir(self.tempdir, "**/*.p*")
668 self.reloader.watch_dir(self.tempdir, "**/*.py*")
669 with self.tick_twice():
670 self.increment_mtime(py_file)
671 self.assertEqual(notify_mock.call_count, 1)
672 self.assertCountEqual(notify_mock.call_args[0], [py_file])
673
674
675 class BaseReloaderTests(ReloaderTests):
676 RELOADER_CLS = autoreload.BaseReloader
677
678 def test_watch_dir_with_unresolvable_path(self):
679 path = Path("unresolvable_directory")
680 with mock.patch.object(Path, "absolute", side_effect=FileNotFoundError):
681 self.reloader.watch_dir(path, "**/*.mo")
682 self.assertEqual(list(self.reloader.directory_globs), [])
683
684 def test_watch_with_glob(self):
685 self.reloader.watch_dir(self.tempdir, "*.py")
686 watched_files = list(self.reloader.watched_files())
687 self.assertIn(self.existing_file, watched_files)
688
689 def test_watch_files_with_recursive_glob(self):
690 inner_file = self.ensure_file(self.tempdir / "test" / "test.py")
691 self.reloader.watch_dir(self.tempdir, "**/*.py")
692 watched_files = list(self.reloader.watched_files())
693 self.assertIn(self.existing_file, watched_files)
694 self.assertIn(inner_file, watched_files)
695
696 def test_run_loop_catches_stopiteration(self):
697 def mocked_tick():
698 yield
699
700 with mock.patch.object(self.reloader, "tick", side_effect=mocked_tick) as tick:
701 self.reloader.run_loop()
702 self.assertEqual(tick.call_count, 1)
703
704 def test_run_loop_stop_and_return(self):
705 def mocked_tick(*args):
706 yield
707 self.reloader.stop()
708 return # Raises StopIteration
709
710 with mock.patch.object(self.reloader, "tick", side_effect=mocked_tick) as tick:
711 self.reloader.run_loop()
712
713 self.assertEqual(tick.call_count, 1)
714
715 def test_wait_for_apps_ready_checks_for_exception(self):
716 app_reg = Apps()
717 app_reg.ready_event.set()
718 # thread.is_alive() is False if it's not started.
719 dead_thread = threading.Thread()
720 self.assertFalse(self.reloader.wait_for_apps_ready(app_reg, dead_thread))
721
722 def test_wait_for_apps_ready_without_exception(self):
723 app_reg = Apps()
724 app_reg.ready_event.set()
725 thread = mock.MagicMock()
726 thread.is_alive.return_value = True
727 self.assertTrue(self.reloader.wait_for_apps_ready(app_reg, thread))
728
729
730 def skip_unless_watchman_available():
731 try:
732 autoreload.WatchmanReloader.check_availability()
733 except WatchmanUnavailable as e:
734 return skip("Watchman unavailable: %s" % e)
735 return lambda func: func
736
737
738 @skip_unless_watchman_available()
739 class WatchmanReloaderTests(ReloaderTests, IntegrationTests):
740 RELOADER_CLS = autoreload.WatchmanReloader
741
742 def setUp(self):
743 super().setUp()
744 # Shorten the timeout to speed up tests.
745 self.reloader.client_timeout = int(os.environ.get("DJANGO_WATCHMAN_TIMEOUT", 2))
746
747 def test_watch_glob_ignores_non_existing_directories_two_levels(self):
748 with mock.patch.object(self.reloader, "_subscribe") as mocked_subscribe:
749 self.reloader._watch_glob(self.tempdir / "does_not_exist" / "more", ["*"])
750 self.assertFalse(mocked_subscribe.called)
751
752 def test_watch_glob_uses_existing_parent_directories(self):
753 with mock.patch.object(self.reloader, "_subscribe") as mocked_subscribe:
754 self.reloader._watch_glob(self.tempdir / "does_not_exist", ["*"])
755 self.assertSequenceEqual(
756 mocked_subscribe.call_args[0],
757 [
758 self.tempdir,
759 "glob-parent-does_not_exist:%s" % self.tempdir,
760 ["anyof", ["match", "does_not_exist/*", "wholename"]],
761 ],
762 )
763
764 def test_watch_glob_multiple_patterns(self):
765 with mock.patch.object(self.reloader, "_subscribe") as mocked_subscribe:
766 self.reloader._watch_glob(self.tempdir, ["*", "*.py"])
767 self.assertSequenceEqual(
768 mocked_subscribe.call_args[0],
769 [
770 self.tempdir,
771 "glob:%s" % self.tempdir,
772 ["anyof", ["match", "*", "wholename"], ["match", "*.py", "wholename"]],
773 ],
774 )
775
776 def test_watched_roots_contains_files(self):
777 paths = self.reloader.watched_roots([self.existing_file])
778 self.assertIn(self.existing_file.parent, paths)
779
780 def test_watched_roots_contains_directory_globs(self):
781 self.reloader.watch_dir(self.tempdir, "*.py")
782 paths = self.reloader.watched_roots([])
783 self.assertIn(self.tempdir, paths)
784
785 def test_watched_roots_contains_sys_path(self):
786 with extend_sys_path(str(self.tempdir)):
787 paths = self.reloader.watched_roots([])
788 self.assertIn(self.tempdir, paths)
789
790 def test_check_server_status(self):
791 self.assertTrue(self.reloader.check_server_status())
792
793 def test_check_server_status_raises_error(self):
794 with mock.patch.object(self.reloader.client, "query") as mocked_query:
795 mocked_query.side_effect = Exception()
796 with self.assertRaises(autoreload.WatchmanUnavailable):
797 self.reloader.check_server_status()
798
799 @mock.patch("pywatchman.client")
800 def test_check_availability(self, mocked_client):
801 mocked_client().capabilityCheck.side_effect = Exception()
802 with self.assertRaisesMessage(
803 WatchmanUnavailable, "Cannot connect to the watchman service"
804 ):
805 self.RELOADER_CLS.check_availability()
806
807 @mock.patch("pywatchman.client")
808 def test_check_availability_lower_version(self, mocked_client):
809 mocked_client().capabilityCheck.return_value = {"version": "4.8.10"}
810 with self.assertRaisesMessage(
811 WatchmanUnavailable, "Watchman 4.9 or later is required."
812 ):
813 self.RELOADER_CLS.check_availability()
814
815 def test_pywatchman_not_available(self):
816 with mock.patch.object(autoreload, "pywatchman") as mocked:
817 mocked.__bool__.return_value = False
818 with self.assertRaisesMessage(
819 WatchmanUnavailable, "pywatchman not installed."
820 ):
821 self.RELOADER_CLS.check_availability()
822
823 def test_update_watches_raises_exceptions(self):
824 class TestException(Exception):
825 pass
826
827 with mock.patch.object(self.reloader, "_update_watches") as mocked_watches:
828 with mock.patch.object(
829 self.reloader, "check_server_status"
830 ) as mocked_server_status:
831 mocked_watches.side_effect = TestException()
832 mocked_server_status.return_value = True
833 with self.assertRaises(TestException):
834 self.reloader.update_watches()
835 self.assertIsInstance(
836 mocked_server_status.call_args[0][0], TestException
837 )
838
839 @mock.patch.dict(os.environ, {"DJANGO_WATCHMAN_TIMEOUT": "10"})
840 def test_setting_timeout_from_environment_variable(self):
841 self.assertEqual(self.RELOADER_CLS().client_timeout, 10)
842
843
844 @skipIf(on_macos_with_hfs(), "These tests do not work with HFS+ as a filesystem")
845 class StatReloaderTests(ReloaderTests, IntegrationTests):
846 RELOADER_CLS = autoreload.StatReloader
847
848 def setUp(self):
849 super().setUp()
850 # Shorten the sleep time to speed up tests.
851 self.reloader.SLEEP_TIME = 0.01
852
853 @mock.patch("django.utils.autoreload.StatReloader.notify_file_changed")
854 def test_tick_does_not_trigger_twice(self, mock_notify_file_changed):
855 with mock.patch.object(
856 self.reloader, "watched_files", return_value=[self.existing_file]
857 ):
858 ticker = self.reloader.tick()
859 next(ticker)
860 self.increment_mtime(self.existing_file)
861 next(ticker)
862 next(ticker)
863 self.assertEqual(mock_notify_file_changed.call_count, 1)
864
865 def test_snapshot_files_ignores_missing_files(self):
866 with mock.patch.object(
867 self.reloader, "watched_files", return_value=[self.nonexistent_file]
868 ):
869 self.assertEqual(dict(self.reloader.snapshot_files()), {})
870
871 def test_snapshot_files_updates(self):
872 with mock.patch.object(
873 self.reloader, "watched_files", return_value=[self.existing_file]
874 ):
875 snapshot1 = dict(self.reloader.snapshot_files())
876 self.assertIn(self.existing_file, snapshot1)
877 self.increment_mtime(self.existing_file)
878 snapshot2 = dict(self.reloader.snapshot_files())
879 self.assertNotEqual(
880 snapshot1[self.existing_file], snapshot2[self.existing_file]
881 )
882
883 def test_snapshot_files_with_duplicates(self):
884 with mock.patch.object(
885 self.reloader,
886 "watched_files",
887 return_value=[self.existing_file, self.existing_file],
888 ):
889 snapshot = list(self.reloader.snapshot_files())
890 self.assertEqual(len(snapshot), 1)
891 self.assertEqual(snapshot[0][0], self.existing_file)