From 09c9fd0c1501b3f99f4956119706c66879404572 Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Tue, 7 Apr 2015 22:39:19 +0200 Subject: [PATCH] support module attributes for version number --- require_license/storage.py | 13 +++-- require_license/tests/settings.py | 6 +- require_license/tests/test_storage.py | 84 ++++++++++++++++++--------- 3 files changed, 69 insertions(+), 34 deletions(-) diff --git a/require_license/storage.py b/require_license/storage.py index d4d669d..803f4a7 100644 --- a/require_license/storage.py +++ b/require_license/storage.py @@ -14,10 +14,9 @@ from django.contrib.staticfiles.storage import StaticFilesStorage +from require.helpers import import_module_attr from require.storage import OptimizedFilesMixin -from require_license import version - class LicenseHeaderMixin(OptimizedFilesMixin): """ @@ -35,8 +34,14 @@ def post_process(self, *args, **kwargs): self.location, config.get('license_file')) - # use local version if 'version' key is missing - if 'version' not in config: + # get version number + if 'version' in config: + version = config.get('version') + try: + # grab version from module attribute + version = import_module_attr(version) + except ImportError: + pass config.update({'version': version}) # inject header diff --git a/require_license/tests/settings.py b/require_license/tests/settings.py index f33ae29..d8a2b18 100644 --- a/require_license/tests/settings.py +++ b/require_license/tests/settings.py @@ -21,8 +21,7 @@ 'django.contrib.auth', 'django.contrib.sessions', 'django.contrib.contenttypes', - 'django.contrib.staticfiles', - 'require_license' + 'django.contrib.staticfiles' ] SECRET_KEY = 'top_secret' @@ -95,10 +94,11 @@ } } -# A dictionary of output files that need a license header with configs. +# A dictionary of output files with a license header config. REQUIRE_LICENSE_HEADERS = { os.path.join(REQUIRE_BASE_URL, 'app.min.js'): { 'license_file': os.path.join(REQUIRE_BASE_URL, 'JS-LICENSE.txt'), + 'version': 'require_license.version', 'timestamp': date.today(), 'copyright_year': datetime.now().year, 'copyright_holder': 'MyCompany', diff --git a/require_license/tests/test_storage.py b/require_license/tests/test_storage.py index 0ceef4c..b8fba2d 100644 --- a/require_license/tests/test_storage.py +++ b/require_license/tests/test_storage.py @@ -14,39 +14,69 @@ from django.test import TestCase from django.core.management import call_command -from require_license import version - class StorageTestCase(TestCase): """ Tests for :py:mod:`~require_license.storage`. """ - def test_licenseHeaderMixin(self): + def tearDown(self): + if os.path.exists(self.file_path): + os.remove(self.file_path) + + def assertHeaderEqual(self, version, expectedVersion): + """ + Assert the header is correct. + """ + with self.settings(REQUIRE_LICENSE_HEADERS={ + os.path.join(settings.REQUIRE_BASE_URL, 'app.min.js'): { + 'license_file': os.path.join(settings.REQUIRE_BASE_URL, + 'JS-LICENSE.txt'), + 'version': version, + 'timestamp': date.today(), + 'copyright_year': datetime.now().year, + 'copyright_holder': 'MyCompany', + 'license_url': 'http://example.com/license' + } + }): + + # run collecstatic + call_command('collectstatic', interactive=False, dry_run=False, + clear=False, verbosity=0) + + # minified file created + self.file_path = os.path.join( + settings.STATIC_ROOT, settings.REQUIRE_BASE_URL, + settings.REQUIRE_STANDALONE_MODULES['app']['out'] + ) + self.assertTrue(os.path.exists(self.file_path)) + + # verify header + with codecs.open(self.file_path, 'rb', encoding='utf-8') as output_file: + lines = output_file.readlines()[:6] + + self.assertEqual(lines[0], + '/*! Copyright MyCompany {} - v{} ({})\n'.format( + datetime.now().year, + expectedVersion, + date.today())) + + self.assertEqual(lines[-2], + ' * For a list of these libraries and their licenses,' + ' visit http://example.com/license.\n') + + def test_basic(self): """ :py:class:`~require_license.storage.LicenseHeaderMixin` adds a license header to the minified JS module. """ - call_command('collectstatic', interactive=False, dry_run=False, - clear=False, verbosity=0) - - file_path = os.path.join( - settings.STATIC_ROOT, settings.REQUIRE_BASE_URL, - settings.REQUIRE_STANDALONE_MODULES['app']['out'] - ) - - # minified file created - self.assertTrue(os.path.exists(file_path)) - - # verify header - with codecs.open(file_path, 'rb', encoding='utf-8') as output_file: - lines = output_file.readlines()[:6] - - self.assertEqual(lines[0], - '/*! Copyright MyCompany {} - v{} ({})\n'.format( - datetime.now().year, - version, - date.today())) - - self.assertEqual(lines[-2], - ' * For a list of these libraries and their licenses,' - ' visit http://example.com/license.\n') + version = '1.0.1' + self.assertHeaderEqual(version, expectedVersion=version) + + def test_fqVersionAttribute(self): + """ + Use fully-qualified path to an existing attribute containing the + version number. + """ + version = 'require_license.version' + from require_license import version as expectedVersion + self.assertHeaderEqual(version, expectedVersion)