Skip to content

Commit 09c9fd0

Browse files
support module attributes for version number
1 parent b29a9f3 commit 09c9fd0

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

require_license/storage.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
from django.contrib.staticfiles.storage import StaticFilesStorage
1616

17+
from require.helpers import import_module_attr
1718
from require.storage import OptimizedFilesMixin
1819

19-
from require_license import version
20-
2120

2221
class LicenseHeaderMixin(OptimizedFilesMixin):
2322
"""
@@ -35,8 +34,14 @@ def post_process(self, *args, **kwargs):
3534
self.location,
3635
config.get('license_file'))
3736

38-
# use local version if 'version' key is missing
39-
if 'version' not in config:
37+
# get version number
38+
if 'version' in config:
39+
version = config.get('version')
40+
try:
41+
# grab version from module attribute
42+
version = import_module_attr(version)
43+
except ImportError:
44+
pass
4045
config.update({'version': version})
4146

4247
# inject header

require_license/tests/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
'django.contrib.auth',
2222
'django.contrib.sessions',
2323
'django.contrib.contenttypes',
24-
'django.contrib.staticfiles',
25-
'require_license'
24+
'django.contrib.staticfiles'
2625
]
2726

2827
SECRET_KEY = 'top_secret'
@@ -95,10 +94,11 @@
9594
}
9695
}
9796

98-
# A dictionary of output files that need a license header with configs.
97+
# A dictionary of output files with a license header config.
9998
REQUIRE_LICENSE_HEADERS = {
10099
os.path.join(REQUIRE_BASE_URL, 'app.min.js'): {
101100
'license_file': os.path.join(REQUIRE_BASE_URL, 'JS-LICENSE.txt'),
101+
'version': 'require_license.version',
102102
'timestamp': date.today(),
103103
'copyright_year': datetime.now().year,
104104
'copyright_holder': 'MyCompany',

require_license/tests/test_storage.py

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,69 @@
1414
from django.test import TestCase
1515
from django.core.management import call_command
1616

17-
from require_license import version
18-
1917

2018
class StorageTestCase(TestCase):
2119
"""
2220
Tests for :py:mod:`~require_license.storage`.
2321
"""
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):
2568
"""
2669
:py:class:`~require_license.storage.LicenseHeaderMixin` adds a license
2770
header to the minified JS module.
2871
"""
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

Comments
 (0)