|
14 | 14 | from django.test import TestCase |
15 | 15 | from django.core.management import call_command |
16 | 16 |
|
17 | | -from require_license import version |
18 | | - |
19 | 17 |
|
20 | 18 | class StorageTestCase(TestCase): |
21 | 19 | """ |
22 | 20 | Tests for :py:mod:`~require_license.storage`. |
23 | 21 | """ |
24 | | - def test_licenseHeaderMixin(self): |
| 22 | + def tearDown(self): |
| 23 | + if os.path.exists(self.file_path): |
| 24 | + os.remove(self.file_path) |
| 25 | + |
| 26 | + def assertHeaderEqual(self, version, expectedVersion): |
| 27 | + """ |
| 28 | + Assert the header is correct. |
| 29 | + """ |
| 30 | + with self.settings(REQUIRE_LICENSE_HEADERS={ |
| 31 | + os.path.join(settings.REQUIRE_BASE_URL, 'app.min.js'): { |
| 32 | + 'license_file': os.path.join(settings.REQUIRE_BASE_URL, |
| 33 | + 'JS-LICENSE.txt'), |
| 34 | + 'version': version, |
| 35 | + 'timestamp': date.today(), |
| 36 | + 'copyright_year': datetime.now().year, |
| 37 | + 'copyright_holder': 'MyCompany', |
| 38 | + 'license_url': 'http://example.com/license' |
| 39 | + } |
| 40 | + }): |
| 41 | + |
| 42 | + # run collecstatic |
| 43 | + call_command('collectstatic', interactive=False, dry_run=False, |
| 44 | + clear=False, verbosity=0) |
| 45 | + |
| 46 | + # minified file created |
| 47 | + self.file_path = os.path.join( |
| 48 | + settings.STATIC_ROOT, settings.REQUIRE_BASE_URL, |
| 49 | + settings.REQUIRE_STANDALONE_MODULES['app']['out'] |
| 50 | + ) |
| 51 | + self.assertTrue(os.path.exists(self.file_path)) |
| 52 | + |
| 53 | + # verify header |
| 54 | + with codecs.open(self.file_path, 'rb', encoding='utf-8') as output_file: |
| 55 | + lines = output_file.readlines()[:6] |
| 56 | + |
| 57 | + self.assertEqual(lines[0], |
| 58 | + '/*! Copyright MyCompany {} - v{} ({})\n'.format( |
| 59 | + datetime.now().year, |
| 60 | + expectedVersion, |
| 61 | + date.today())) |
| 62 | + |
| 63 | + self.assertEqual(lines[-2], |
| 64 | + ' * For a list of these libraries and their licenses,' |
| 65 | + ' visit http://example.com/license.\n') |
| 66 | + |
| 67 | + def test_basic(self): |
25 | 68 | """ |
26 | 69 | :py:class:`~require_license.storage.LicenseHeaderMixin` adds a license |
27 | 70 | header to the minified JS module. |
28 | 71 | """ |
29 | | - call_command('collectstatic', interactive=False, dry_run=False, |
30 | | - clear=False, verbosity=0) |
31 | | - |
32 | | - file_path = os.path.join( |
33 | | - settings.STATIC_ROOT, settings.REQUIRE_BASE_URL, |
34 | | - settings.REQUIRE_STANDALONE_MODULES['app']['out'] |
35 | | - ) |
36 | | - |
37 | | - # minified file created |
38 | | - self.assertTrue(os.path.exists(file_path)) |
39 | | - |
40 | | - # verify header |
41 | | - with codecs.open(file_path, 'rb', encoding='utf-8') as output_file: |
42 | | - lines = output_file.readlines()[:6] |
43 | | - |
44 | | - self.assertEqual(lines[0], |
45 | | - '/*! Copyright MyCompany {} - v{} ({})\n'.format( |
46 | | - datetime.now().year, |
47 | | - version, |
48 | | - date.today())) |
49 | | - |
50 | | - self.assertEqual(lines[-2], |
51 | | - ' * For a list of these libraries and their licenses,' |
52 | | - ' visit http://example.com/license.\n') |
| 72 | + version = '1.0.1' |
| 73 | + self.assertHeaderEqual(version, expectedVersion=version) |
| 74 | + |
| 75 | + def test_fqVersionAttribute(self): |
| 76 | + """ |
| 77 | + Use fully-qualified path to an existing attribute containing the |
| 78 | + version number. |
| 79 | + """ |
| 80 | + version = 'require_license.version' |
| 81 | + from require_license import version as expectedVersion |
| 82 | + self.assertHeaderEqual(version, expectedVersion) |
0 commit comments