From e035db1bc3bbeb4282a177ad106add3b07d97b09 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 18 Nov 2024 14:39:55 +0100 Subject: [PATCH] Fixed #35882 -- Made migration questioner loop on all errors. --- django/db/migrations/questioner.py | 4 ++-- tests/migrations/test_questioner.py | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/django/db/migrations/questioner.py b/django/db/migrations/questioner.py index e1081ab70a..2e61195581 100644 --- a/django/db/migrations/questioner.py +++ b/django/db/migrations/questioner.py @@ -160,8 +160,8 @@ class InteractiveMigrationQuestioner(MigrationQuestioner): else: try: return eval(code, {}, {"datetime": datetime, "timezone": timezone}) - except (SyntaxError, NameError) as e: - self.prompt_output.write("Invalid input: %s" % e) + except Exception as e: + self.prompt_output.write(f"{e.__class__.__name__}: {e}") def ask_not_null_addition(self, field_name, model_name): """Adding a NOT NULL field to a model.""" diff --git a/tests/migrations/test_questioner.py b/tests/migrations/test_questioner.py index a212fbf65f..ec1013923b 100644 --- a/tests/migrations/test_questioner.py +++ b/tests/migrations/test_questioner.py @@ -61,10 +61,27 @@ class QuestionerHelperMethodsTests(SimpleTestCase): ) @mock.patch("builtins.input", side_effect=["bad code", "exit"]) - def test_questioner_no_default_bad_user_entry_code(self, mock_input): + def test_questioner_no_default_syntax_error(self, mock_input): with self.assertRaises(SystemExit): self.questioner._ask_default() - self.assertIn("Invalid input: ", self.prompt.getvalue()) + self.assertIn("SyntaxError: invalid syntax", self.prompt.getvalue()) + + @mock.patch("builtins.input", side_effect=["datetim", "exit"]) + def test_questioner_no_default_name_error(self, mock_input): + with self.assertRaises(SystemExit): + self.questioner._ask_default() + self.assertIn( + "NameError: name 'datetim' is not defined", self.prompt.getvalue() + ) + + @mock.patch("builtins.input", side_effect=["datetime.dat", "exit"]) + def test_questioner_no_default_attribute_error(self, mock_input): + with self.assertRaises(SystemExit): + self.questioner._ask_default() + self.assertIn( + "AttributeError: module 'datetime' has no attribute 'dat'", + self.prompt.getvalue(), + ) @mock.patch("builtins.input", side_effect=[KeyboardInterrupt()]) def test_questioner_no_default_keyboard_interrupt(self, mock_input):