Merge lp:~danilo/linaro-license-protection/bug-1084013 into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Данило Шеган
Status: Merged
Approved by: James Tunnicliffe
Approved revision: 153
Merged at revision: 151
Proposed branch: lp:~danilo/linaro-license-protection/bug-1084013
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 260 lines (+95/-62)
5 files modified
license_protected_downloads/buildinfo.py (+2/-5)
license_protected_downloads/tests/__init__.py (+13/-7)
license_protected_downloads/tests/helpers.py (+42/-0)
license_protected_downloads/tests/test_buildinfo.py (+17/-0)
license_protected_downloads/tests/test_views.py (+21/-50)
To merge this branch: bzr merge lp:~danilo/linaro-license-protection/bug-1084013
Reviewer Review Type Date Requested Status
James Tunnicliffe (community) Approve
Review via email: mp+136639@code.launchpad.net

Description of the change

This ensures files like MD5SUM (all uppercase) are still recognized in BUILD-INFO patterns: problem was that we used to lowercase the key, but then didn't match against it.

I don't see any reason to try to match the globs in a case-sensitive manner, so I am instead keeping the file patterns as they are.

This branch introduces tests on both the level of BUILD-INFO parsing, and an integration test to ensure we can access a file with all uppercase filename.

Some minor cleanups are done along the way:
 - temporary_directory helper has been moved into tests/helpers.py since it's used by both test_buildinfo.py and test_views.py now
 - a few imports have been reordered, and I extract the shared test-case setup in test_views.py

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

pep8 is even stricter in quantal, I'll work on fixing that in the follow-up branch.

Revision history for this message
James Tunnicliffe (dooferlad) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'license_protected_downloads/buildinfo.py'
2--- license_protected_downloads/buildinfo.py 2012-07-11 11:56:10 +0000
3+++ license_protected_downloads/buildinfo.py 2012-11-28 12:32:21 +0000
4@@ -40,7 +40,6 @@
5 os.path.join(cls.get_search_path(path), "BUILD-INFO.txt"))
6
7 def _set(self, key, value):
8- key = key.lower()
9 if key in self.build_info_array[self.index]:
10 # A repeated key indicates we have found another chunk of
11 # build-info
12@@ -175,9 +174,7 @@
13 self.file_info_array.pop(index)
14
15 if __name__ == "__main__":
16- bi = BuildInfo("/var/www/build-info/origen-blob.txt")
17-# print bi.build_info_array
18-# print "file_info_array: %s" % bi.file_info_array
19-# print bi.getFormatVersion()
20+ import sys
21+ bi = BuildInfo(sys.argv[1])
22 for field in bi.fields_defined:
23 print field + " = " + str(bi.get(field))
24
25=== modified file 'license_protected_downloads/tests/__init__.py'
26--- license_protected_downloads/tests/__init__.py 2012-11-07 18:13:07 +0000
27+++ license_protected_downloads/tests/__init__.py 2012-11-28 12:32:21 +0000
28@@ -1,8 +1,12 @@
29-from license_protected_downloads.tests.test_buildinfo import BuildInfoTests
30+from license_protected_downloads.tests.test_buildinfo import (
31+ BuildInfoTests,
32+ FileNameMatchingTests,
33+ )
34 from license_protected_downloads.tests.test_models import LicenseTestCase
35 from license_protected_downloads.tests.test_pep8 import TestPep8
36 from license_protected_downloads.tests.test_pyflakes import TestPyflakes
37 from license_protected_downloads.tests.test_views import (
38+ FileViewTests,
39 HowtoViewTests,
40 ViewTests,
41 )
42@@ -14,13 +18,15 @@
43
44 #starts the test suite
45 __test__ = {
46+ 'BuildInfoTests': BuildInfoTests,
47+ 'FileNameMatchingTests': FileNameMatchingTests,
48+ 'FileViewTests': FileViewTests,
49+ 'HowtoViewTests': HowtoViewTests,
50 'LicenseTestCase': LicenseTestCase,
51- 'ViewTests': ViewTests,
52- 'HowtoViewTests': HowtoViewTests,
53- 'BuildInfoTests': BuildInfoTests,
54+ 'RenderTextFilesTests': RenderTextFilesTests,
55+ 'SetsuperuserCommandTest': SetsuperuserCommandTest,
56+ 'TestOpenIDAuth': TestOpenIDAuth,
57 'TestPep8': TestPep8,
58 'TestPyflakes': TestPyflakes,
59- 'TestOpenIDAuth': TestOpenIDAuth,
60- 'SetsuperuserCommandTest': SetsuperuserCommandTest,
61- 'RenderTextFilesTests': RenderTextFilesTests,
62+ 'ViewTests': ViewTests,
63 }
64
65=== added file 'license_protected_downloads/tests/helpers.py'
66--- license_protected_downloads/tests/helpers.py 1970-01-01 00:00:00 +0000
67+++ license_protected_downloads/tests/helpers.py 2012-11-28 12:32:21 +0000
68@@ -0,0 +1,42 @@
69+# Copyright (c)2012 Linaro.
70+# Test helpers.
71+
72+import os
73+import shutil
74+import tempfile
75+
76+
77+class temporary_directory(object):
78+ """Creates a context manager for a temporary directory."""
79+
80+ def __enter__(self):
81+ self.root = tempfile.mkdtemp()
82+ return self
83+
84+ def __exit__(self, *args):
85+ shutil.rmtree(self.root)
86+
87+ def make_file(self, name, data=None, with_buildinfo=False):
88+ """Creates a file in this temporary directory."""
89+ full_path = os.path.join(self.root, name)
90+ dir_name = os.path.dirname(full_path)
91+ try:
92+ os.makedirs(dir_name)
93+ except os.error:
94+ pass
95+ if with_buildinfo:
96+ buildinfo_name = os.path.join(dir_name, 'BUILD-INFO.txt')
97+ base_name = os.path.basename(full_path)
98+ with open(buildinfo_name, 'w') as buildinfo_file:
99+ buildinfo_file.write(
100+ 'Format-Version: 0.1\n\n'
101+ 'Files-Pattern: %s\n'
102+ 'License-Type: open\n' % base_name)
103+ target = open(full_path, "w")
104+ if data is None:
105+ return target
106+ else:
107+ target.write(data)
108+ target.close()
109+ return full_path
110+
111
112=== modified file 'license_protected_downloads/tests/test_buildinfo.py'
113--- license_protected_downloads/tests/test_buildinfo.py 2012-07-20 12:26:22 +0000
114+++ license_protected_downloads/tests/test_buildinfo.py 2012-11-28 12:32:21 +0000
115@@ -4,6 +4,7 @@
116 import unittest
117 from license_protected_downloads.buildinfo import BuildInfo
118 from license_protected_downloads.buildinfo import IncorrectDataFormatException
119+from license_protected_downloads.tests.helpers import temporary_directory
120
121 THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
122
123@@ -275,5 +276,21 @@
124 self.assertEquals(build_info.file_info_array, [{}])
125
126
127+class FileNameMatchingTests(unittest.TestCase):
128+ def test_buildinfo_simple_filename(self):
129+ with temporary_directory() as serve_root:
130+ sample_file = serve_root.make_file("MD5SUM", data="blah")
131+ serve_root.make_file(
132+ "BUILD-INFO.txt",
133+ data=(
134+ "Format-Version: 2.0\n\n"
135+ "Files-Pattern: MD5SUM\n"
136+ "License-Type: open\n"
137+ ))
138+ build_info = BuildInfo(sample_file)
139+ file_info = build_info.getInfoForFile()
140+ self.assertEqual('open', file_info[0]['license-type'])
141+
142+
143 if __name__ == '__main__':
144 unittest.main()
145
146=== modified file 'license_protected_downloads/tests/test_views.py'
147--- license_protected_downloads/tests/test_views.py 2012-11-21 17:57:53 +0000
148+++ license_protected_downloads/tests/test_views.py 2012-11-28 12:32:21 +0000
149@@ -4,60 +4,25 @@
150 from django.test import Client, TestCase
151 import hashlib
152 import os
153+import tempfile
154 import unittest
155 import urlparse
156-import shutil
157-import tempfile
158
159 from license_protected_downloads import bzr_version
160 from license_protected_downloads.buildinfo import BuildInfo
161+from license_protected_downloads.config import INTERNAL_HOSTS
162+from license_protected_downloads.tests.helpers import temporary_directory
163 from license_protected_downloads.views import _insert_license_into_db
164+from license_protected_downloads.views import _process_include_tags
165 from license_protected_downloads.views import _sizeof_fmt
166-from license_protected_downloads.views import _process_include_tags
167 from license_protected_downloads.views import is_same_parent_dir
168-from license_protected_downloads.config import INTERNAL_HOSTS
169
170
171 THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
172 TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
173
174
175-class temporary_directory(object):
176- """Creates a context manager for a temporary directory."""
177-
178- def __enter__(self):
179- self.root = tempfile.mkdtemp()
180- return self
181-
182- def __exit__(self, *args):
183- shutil.rmtree(self.root)
184-
185- def make_file(self, name, data=None, with_buildinfo=False):
186- """Creates a file in this temporary directory."""
187- full_path = os.path.join(self.root, name)
188- dir_name = os.path.dirname(full_path)
189- try:
190- os.makedirs(dir_name)
191- except os.error:
192- pass
193- if with_buildinfo:
194- buildinfo_name = os.path.join(dir_name, 'BUILD-INFO.txt')
195- base_name = os.path.basename(full_path)
196- with open(buildinfo_name, 'w') as buildinfo_file:
197- buildinfo_file.write(
198- 'Format-Version: 0.1\n\n'
199- 'Files-Pattern: %s\n'
200- 'License-Type: open\n' % base_name)
201- target = open(full_path, "w")
202- if data is None:
203- return target
204- else:
205- target.write(data)
206- target.close()
207- return full_path
208-
209-
210-class ViewTests(TestCase):
211+class BaseServeViewTest(TestCase):
212 def setUp(self):
213 self.client = Client()
214 self.old_served_paths = settings.SERVED_PATHS
215@@ -67,6 +32,7 @@
216 def tearDown(self):
217 settings.SERVED_PATHS = self.old_served_paths
218
219+class ViewTests(BaseServeViewTest):
220 def test_license_directly(self):
221 response = self.client.get('/licenses/license.html', follow=True)
222 self.assertEqual(response.status_code, 200)
223@@ -674,16 +640,7 @@
224 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))
225
226
227-class HowtoViewTests(TestCase):
228- def setUp(self):
229- self.client = Client()
230- self.old_served_paths = settings.SERVED_PATHS
231- settings.SERVED_PATHS = [os.path.join(THIS_DIRECTORY,
232- "testserver_root")]
233-
234- def tearDown(self):
235- settings.SERVED_PATHS = self.old_served_paths
236-
237+class HowtoViewTests(BaseServeViewTest):
238 def test_no_howtos(self):
239 with temporary_directory() as serve_root:
240 settings.SERVED_PATHS = [serve_root.root]
241@@ -754,5 +711,19 @@
242 self.assertContains(response, 'HowTo Test')
243
244
245+class FileViewTests(BaseServeViewTest):
246+ def test_static_file(self):
247+ with temporary_directory() as serve_root:
248+ settings.SERVED_PATHS = [serve_root.root]
249+ serve_root.make_file("MD5SUM")
250+ serve_root.make_file(
251+ "BUILD-INFO.txt",
252+ data=("Format-Version: 2.0\n\n"
253+ "Files-Pattern: MD5SUM\n"
254+ "License-Type: open\n"))
255+ response = self.client.get('/MD5SUM')
256+ self.assertEqual(response.status_code, 200)
257+
258+
259 if __name__ == '__main__':
260 unittest.main()

Subscribers

People subscribed via source and target branches