mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	[1.8.x] Accounted for error files in the autoreloader.
* When some old files contain errors, the second call to
  gen_filenames() should return them.
* When some new files contain errors, the first call to
  gen_filenames(only_new=True) should return them.
Backport of 23620cb8e0 from master
			
			
This commit is contained in:
		| @@ -95,7 +95,7 @@ def gen_filenames(only_new=False): | |||||||
|         if only_new: |         if only_new: | ||||||
|             return [] |             return [] | ||||||
|         else: |         else: | ||||||
|             return _cached_filenames |             return _cached_filenames + clean_files(_error_files) | ||||||
|  |  | ||||||
|     new_modules = module_values - _cached_modules |     new_modules = module_values - _cached_modules | ||||||
|     new_filenames = clean_files( |     new_filenames = clean_files( | ||||||
| @@ -122,7 +122,7 @@ def gen_filenames(only_new=False): | |||||||
|     _cached_modules = _cached_modules.union(new_modules) |     _cached_modules = _cached_modules.union(new_modules) | ||||||
|     _cached_filenames += new_filenames |     _cached_filenames += new_filenames | ||||||
|     if only_new: |     if only_new: | ||||||
|         return new_filenames |         return new_filenames + clean_files(_error_files) | ||||||
|     else: |     else: | ||||||
|         return _cached_filenames + clean_files(_error_files) |         return _cached_filenames + clean_files(_error_files) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| import os | import os | ||||||
|  | import shutil | ||||||
| import tempfile | import tempfile | ||||||
| from importlib import import_module | from importlib import import_module | ||||||
|  |  | ||||||
| @@ -6,6 +7,7 @@ from django import conf | |||||||
| from django.contrib import admin | from django.contrib import admin | ||||||
| from django.test import TestCase, override_settings | from django.test import TestCase, override_settings | ||||||
| from django.test.utils import extend_sys_path | from django.test.utils import extend_sys_path | ||||||
|  | from django.utils import autoreload | ||||||
| from django.utils._os import npath, upath | from django.utils._os import npath, upath | ||||||
| from django.utils.autoreload import gen_filenames | from django.utils.autoreload import gen_filenames | ||||||
|  |  | ||||||
| @@ -98,3 +100,41 @@ class TestFilenameGenerator(TestCase): | |||||||
|         self.assertIn(npath(filename), gen_filenames()) |         self.assertIn(npath(filename), gen_filenames()) | ||||||
|         os.unlink(filename) |         os.unlink(filename) | ||||||
|         self.assertNotIn(filename, gen_filenames()) |         self.assertNotIn(filename, gen_filenames()) | ||||||
|  |  | ||||||
|  |     def test_check_errors(self): | ||||||
|  |         """ | ||||||
|  |         When a file containing an error is imported in a function wrapped by | ||||||
|  |         check_errors(), gen_filenames() returns it. | ||||||
|  |  | ||||||
|  |         Run assertions twice to test uncached and cached access. | ||||||
|  |         """ | ||||||
|  |         dirname = tempfile.mkdtemp() | ||||||
|  |         filename = os.path.join(dirname, 'test_syntax_error.py') | ||||||
|  |         self.addCleanup(shutil.rmtree, dirname) | ||||||
|  |         with open(filename, 'w') as f: | ||||||
|  |             f.write("Ceci n'est pas du Python.") | ||||||
|  |  | ||||||
|  |         with extend_sys_path(dirname): | ||||||
|  |             with self.assertRaises(SyntaxError): | ||||||
|  |                 autoreload.check_errors(import_module)('test_syntax_error') | ||||||
|  |         self.assertIn(npath(filename), gen_filenames()) | ||||||
|  |         self.assertIn(npath(filename), gen_filenames()) | ||||||
|  |  | ||||||
|  |     def test_check_errors_only_new(self): | ||||||
|  |         """ | ||||||
|  |         When a file containing an error is imported in a function wrapped by | ||||||
|  |         check_errors(), gen_filenames(only_new=True) returns it. | ||||||
|  |  | ||||||
|  |         Run assertions twice to test uncached and cached access. | ||||||
|  |         """ | ||||||
|  |         dirname = tempfile.mkdtemp() | ||||||
|  |         filename = os.path.join(dirname, 'test_syntax_error.py') | ||||||
|  |         self.addCleanup(shutil.rmtree, dirname) | ||||||
|  |         with open(filename, 'w') as f: | ||||||
|  |             f.write("Ceci n'est pas du Python.") | ||||||
|  |  | ||||||
|  |         with extend_sys_path(dirname): | ||||||
|  |             with self.assertRaises(SyntaxError): | ||||||
|  |                 autoreload.check_errors(import_module)('test_syntax_error') | ||||||
|  |         self.assertIn(npath(filename), gen_filenames(only_new=True)) | ||||||
|  |         self.assertNotIn(npath(filename), gen_filenames(only_new=True)) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user