test_autoreload.py 32 KB

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