Skip to content

Commit

Permalink
support module attributes for version number
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Apr 7, 2015
1 parent b29a9f3 commit 09c9fd0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
13 changes: 9 additions & 4 deletions require_license/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions require_license/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
Expand Down
84 changes: 57 additions & 27 deletions require_license/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 09c9fd0

Please sign in to comment.