[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:
Aymeric Augustin 2015-08-05 11:06:26 +02:00
parent 0d368bfb47
commit b18bb65fd2
2 changed files with 42 additions and 2 deletions

View File

@ -95,7 +95,7 @@ def gen_filenames(only_new=False):
if only_new:
return []
else:
return _cached_filenames
return _cached_filenames + clean_files(_error_files)
new_modules = module_values - _cached_modules
new_filenames = clean_files(
@ -122,7 +122,7 @@ def gen_filenames(only_new=False):
_cached_modules = _cached_modules.union(new_modules)
_cached_filenames += new_filenames
if only_new:
return new_filenames
return new_filenames + clean_files(_error_files)
else:
return _cached_filenames + clean_files(_error_files)

View File

@ -1,4 +1,5 @@
import os
import shutil
import tempfile
from importlib import import_module
@ -6,6 +7,7 @@ from django import conf
from django.contrib import admin
from django.test import TestCase, override_settings
from django.test.utils import extend_sys_path
from django.utils import autoreload
from django.utils._os import npath, upath
from django.utils.autoreload import gen_filenames
@ -98,3 +100,41 @@ class TestFilenameGenerator(TestCase):
self.assertIn(npath(filename), gen_filenames())
os.unlink(filename)
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))