From 23620cb8e09aef575c6c5b63cb33f0cda8f5038f Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Wed, 5 Aug 2015 11:06:26 +0200 Subject: [PATCH] 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. --- django/utils/autoreload.py | 4 +-- tests/utils_tests/test_autoreload.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index 6efd6c4f58..ad4b0db6fb 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -92,7 +92,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( @@ -119,7 +119,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) diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py index 11034c1c59..f9298a1545 100644 --- a/tests/utils_tests/test_autoreload.py +++ b/tests/utils_tests/test_autoreload.py @@ -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 SimpleTestCase, 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(SimpleTestCase): 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))