Browse Source

Add tests for "oscar_fork_statics" management command

master
Joseph Wayodi 4 years ago
parent
commit
53b9949d19

+ 3
- 3
src/oscar/management/commands/oscar_fork_statics.py View File

@@ -36,10 +36,10 @@ class Command(BaseCommand):
36 36
 
37 37
         source = os.path.realpath(
38 38
             os.path.join(os.path.dirname(__file__), '../../static'))
39
-        print("Copying Oscar's static files to %s" % (destination,))
39
+        self.stdout.write("Copying Oscar's static files to %s" % destination)
40 40
         shutil.copytree(source, destination)
41 41
 
42 42
         # Check if this new folder is in STATICFILES_DIRS
43 43
         if destination not in settings.STATICFILES_DIRS:
44
-            print(("You need to add %s to STATICFILES_DIRS in order for your "
45
-                   "local overrides to be picked up") % destination)
44
+            self.stdout.write("You need to add %s to STATICFILES_DIRS in order for your local overrides to be picked "
45
+                              "up" % destination)

+ 0
- 0
tests/integration/management_commands/__init__.py View File


+ 89
- 0
tests/integration/management_commands/test_oscar_fork_statics.py View File

@@ -0,0 +1,89 @@
1
+import io
2
+import os
3
+import pathlib
4
+import tempfile
5
+
6
+from django.core.management import call_command
7
+from django.core.management.base import CommandError
8
+from django.test import TestCase
9
+
10
+import oscar
11
+from tests import _site
12
+
13
+
14
+class OscarForkStaticsTestCase(TestCase):
15
+
16
+    def setUp(self):
17
+        # Create dummy Oscar static-files directory (this exists in released
18
+        # Oscar packages)
19
+        self.oscar_static_dir_path = pathlib.Path(os.path.dirname(oscar.__file__), 'static')
20
+        self.oscar_static_dir_path.mkdir()
21
+        self.oscar_static_file_path = pathlib.Path(self.oscar_static_dir_path, 'name.css')
22
+        self.oscar_static_file_path.touch(exist_ok=False)
23
+
24
+        # Change to the project's base directory (where the static-files
25
+        # directory should be copied to)
26
+        self.project_base_dir_path = pathlib.Path(os.path.dirname(_site.__file__))
27
+        os.chdir(self.project_base_dir_path)
28
+
29
+    def tearDown(self):
30
+        # Delete dummy Oscar static-files directory
31
+        self.oscar_static_file_path.unlink()
32
+        self.oscar_static_dir_path.rmdir()
33
+
34
+    def test_command_with_already_existing_directory(self):
35
+        project_static_dir_path = pathlib.Path(self.project_base_dir_path, 'static')
36
+
37
+        project_static_dir_path.mkdir()
38
+        with self.assertRaises(CommandError) as e:
39
+            call_command('oscar_fork_statics')
40
+        self.assertEqual(e.exception.args[0], "The folder %s already exists - aborting!" % project_static_dir_path)
41
+
42
+        project_static_dir_path.rmdir()
43
+
44
+    def test_command_with_default_target_path(self):
45
+        project_static_dir_path = pathlib.Path(self.project_base_dir_path, 'static')
46
+        project_static_file_path = pathlib.Path(project_static_dir_path, 'name.css')
47
+
48
+        out = io.StringIO()
49
+        call_command('oscar_fork_statics', stdout=out)
50
+        self.assertTrue(project_static_file_path.exists())
51
+        messages = out.getvalue().split('\n')
52
+        self.assertEqual(messages[0], "Copying Oscar's static files to %s" % project_static_dir_path)
53
+        self.assertEqual(messages[1], "You need to add %s to STATICFILES_DIRS in order for your local overrides to be "
54
+                                      "picked up" % project_static_dir_path)
55
+
56
+        project_static_file_path.unlink()
57
+        project_static_dir_path.rmdir()
58
+
59
+    def test_command_with_relative_target_path(self):
60
+        project_static_dir_path = pathlib.Path(self.project_base_dir_path, 'relative/dir')
61
+        project_static_file_path = pathlib.Path(project_static_dir_path, 'name.css')
62
+
63
+        out = io.StringIO()
64
+        call_command('oscar_fork_statics', target_path='relative/dir', stdout=out)
65
+        self.assertTrue(project_static_file_path.exists())
66
+        messages = out.getvalue().split('\n')
67
+        self.assertEqual(messages[0], "Copying Oscar's static files to %s" % project_static_dir_path)
68
+        self.assertEqual(messages[1], "You need to add %s to STATICFILES_DIRS in order for your local overrides to be "
69
+                                      "picked up" % project_static_dir_path)
70
+
71
+        project_static_file_path.unlink()
72
+        project_static_dir_path.rmdir()
73
+        project_static_dir_path.parent.rmdir()
74
+
75
+    def test_command_with_absolute_target_path(self):
76
+        project_static_dir_path = pathlib.Path(tempfile.mkdtemp(), 'static')
77
+        project_static_file_path = pathlib.Path(project_static_dir_path, 'name.css')
78
+
79
+        out = io.StringIO()
80
+        call_command('oscar_fork_statics', target_path=str(project_static_dir_path), stdout=out)
81
+        self.assertTrue(project_static_file_path.exists())
82
+        messages = out.getvalue().split('\n')
83
+        self.assertEqual(messages[0], "Copying Oscar's static files to %s" % project_static_dir_path)
84
+        self.assertEqual(messages[1], "You need to add %s to STATICFILES_DIRS in order for your local overrides to be "
85
+                                      "picked up" % project_static_dir_path)
86
+
87
+        project_static_file_path.unlink()
88
+        project_static_dir_path.rmdir()
89
+        project_static_dir_path.parent.rmdir()

Loading…
Cancel
Save