From 3f2c945257e3d9015252c801d124bd70768f2d0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Fri, 26 Aug 2016 17:46:46 -0700 Subject: [PATCH] Added tests for collectstatic interactivity. --- tests/staticfiles_tests/test_management.py | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/staticfiles_tests/test_management.py b/tests/staticfiles_tests/test_management.py index d2ae7e2ed5e..4cca3444909 100644 --- a/tests/staticfiles_tests/test_management.py +++ b/tests/staticfiles_tests/test_management.py @@ -13,7 +13,7 @@ from django.contrib.staticfiles import storage from django.contrib.staticfiles.management.commands import collectstatic from django.core.exceptions import ImproperlyConfigured from django.core.management import call_command -from django.test import override_settings +from django.test import mock, override_settings from django.test.utils import extend_sys_path from django.utils import six from django.utils._os import symlinks_supported @@ -176,6 +176,40 @@ class TestCollectionClear(CollectionTestCase): self.assertFileNotFound('cleared.txt') +class TestInteractiveMessages(CollectionTestCase): + overwrite_warning_msg = "This will overwrite existing files!" + delete_warning_msg = "This will DELETE ALL FILES in this location!" + + @staticmethod + def mock_input(stdout): + def _input(msg): + # Python 2 reads bytes from the console output, use bytes for the StringIO + stdout.write(msg.encode('utf-8') if six.PY2 else msg) + return 'yes' + return _input + + def test_warning_when_clearing_staticdir(self): + stdout = six.StringIO() + self.run_collectstatic() + with mock.patch('django.contrib.staticfiles.management.commands.collectstatic.input', + side_effect=self.mock_input(stdout)): + call_command('collectstatic', interactive=True, clear=True, stdout=stdout) + + output = force_text(stdout.getvalue()) + self.assertNotIn(self.overwrite_warning_msg, output) + self.assertIn(self.delete_warning_msg, output) + + def test_warning_when_overwriting_files_in_staticdir(self): + stdout = six.StringIO() + self.run_collectstatic() + with mock.patch('django.contrib.staticfiles.management.commands.collectstatic.input', + side_effect=self.mock_input(stdout)): + call_command('collectstatic', interactive=True, stdout=stdout) + output = force_text(stdout.getvalue()) + self.assertIn(self.overwrite_warning_msg, output) + self.assertNotIn(self.delete_warning_msg, output) + + class TestCollectionExcludeNoDefaultIgnore(TestDefaults, CollectionTestCase): """ Test ``--exclude-dirs`` and ``--no-default-ignore`` options of the