Skip to content

Commit 1ad5474

Browse files
committed
Fixed the tests following restructuration of the code
1 parent fbba314 commit 1ad5474

File tree

5 files changed

+92
-100
lines changed

5 files changed

+92
-100
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ http://www.dejacode.org/about_spec_v0.8.0.html
2828

2929
REQUIREMENTS
3030
------------
31-
The ABOUT tool has been tested with Python 2.6 or 2.7 on Linux, Mac and Windows.
31+
The ABOUT tool is tested with Python 2.6 or 2.7 on Linux, Mac and Windows.
3232
You will need to install a Python interpreter if you do not have one already
3333
installed.
3434

@@ -63,7 +63,7 @@ https://github.com/dejacode/about-code-tool/archive/master.zip
6363

6464
Then open a terminal or command prompt, extract the download if needed and run::
6565

66-
python tests.py
66+
python setup.py test
6767

6868

6969
USAGE

about_code_tool/about.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def license_text(self):
553553
license_text_path = self.file_fields_locations["license_text_file"]
554554
with open(license_text_path, 'rU') as f:
555555
return f.read()
556-
except Exception as e:
556+
except Exception as e:
557557
pass
558558
#return empty string if the license file does not exist
559559
return ""
@@ -567,7 +567,7 @@ def notice_text(self):
567567
notice_text_path = self.file_fields_locations["notice_file"]
568568
with open(notice_text_path, 'rU') as f:
569569
return f.read()
570-
except Exception as e:
570+
except Exception as e:
571571
pass
572572
#return empty string if the notice file does not exist
573573
return ""
@@ -1015,21 +1015,21 @@ def generate_attribution(self, template_path='templates/default.html',
10151015
return
10161016

10171017
# We only need the fields names and values to render the template
1018-
about_validated_fields = [about_object.validated_fields
1019-
for about_object in self.about_objects
1020-
if not sublist
1018+
about_validated_fields = [about_object.validated_fields
1019+
for about_object in self.about_objects
1020+
if not sublist
10211021
or about_object.about_resource_path in sublist]
10221022

1023-
about_license_text = [about_object.license_text()
1024-
for about_object in self.about_objects
1025-
if not sublist
1023+
about_license_text = [about_object.license_text()
1024+
for about_object in self.about_objects
1025+
if not sublist
10261026
or about_object.about_resource_path in sublist]
10271027
about_notice_text = [about_object.notice_text()
10281028
for about_object in self.about_objects
10291029
if not sublist
10301030
or about_object.about_resource_path in sublist]
10311031

1032-
return template.render(about_objects = about_validated_fields,
1032+
return template.render(about_objects = about_validated_fields,
10331033
license_texts = about_license_text,
10341034
notice_texts = about_notice_text)
10351035

about_code_tool/genabout.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,11 @@ def copy_license_files(self, gen_location, license_list):
176176
makedirs(license_parent_dir)
177177
shutil.copy2(license_path, output_license_path)
178178

179-
"""
180-
def extract_licesen_from_url(self):
181-
# This function needs discussion
182-
test = urllib2.urlopen("https://enterprise.dejacode.com/license_library/Demo/gpl-1.0/#license-text")
183-
with open('testdata/test_file.txt', 'wb') as output_file:
184-
output_file.write(test.read())
185-
"""
179+
#def extract_license_from_url(self):
180+
# # This function needs discussion
181+
# test = urllib2.urlopen("https://enterprise.dejacode.com/license_library/Demo/gpl-1.0/#license-text")
182+
# with open('testdata/test_file.txt', 'wb') as output_file:
183+
# output_file.write(test.read())
186184

187185
def pre_generation(self, gen_location, input_list, action_num, all_in_one):
188186
"""
@@ -232,36 +230,30 @@ def pre_generation(self, gen_location, input_list, action_num, all_in_one):
232230
output_list.append(component_list)
233231
return output_list
234232

235-
def format_output(self, input_list):
233+
@staticmethod
234+
def format_output(input_list):
236235
"""
237-
process the input and covert to the specific strings format
236+
Process the input and convert to the specific strings format.
238237
"""
239238
components_list = []
240-
for items in input_list:
239+
for entry in input_list:
240+
about_file_location = entry[0]
241+
about_dict_list = entry[1]
242+
241243
component = []
242-
about_file_location = items[0]
243-
about_dict_list = items[1]
244-
context = ''
245-
if about_dict_list['name']:
246-
name = about_dict_list['name']
247-
else:
248-
name = ''
249-
if about_dict_list['version']:
250-
version = about_dict_list['version']
251-
else:
252-
version = ''
253-
context = 'about_resource: ' + about_dict_list['about_resource'] + '\n' \
254-
+ 'name: ' + name + '\n' \
255-
+ 'version: ' + version + '\n\n'
244+
component_name = about_dict_list.get('name', '')
245+
component_version = about_dict_list.get('version', '')
246+
context = 'about_resource: %s\nname: %s\nversion: %s\n\n' % (
247+
about_dict_list['about_resource'], component_name, component_version)
248+
256249
for item in sorted(about_dict_list.iterkeys()):
257-
if item == 'about_file':
258-
continue
259250
if not item in MANDATORY_FIELDS:
260251
# The purpose of the replace('\n', '\n ') is used to
261252
# format the continuation strings
262253
value = about_dict_list[item].replace('\n', '\n ')
263254
if (value or item in MANDATORY_FIELDS) and not item in SKIPPED_FIELDS:
264255
context += item + ': ' + value + '\n'
256+
265257
component.append(about_file_location)
266258
component.append(context)
267259
components_list.append(component)

about_code_tool/tests/test_about.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
# =============================================================================
1616

17+
from __future__ import print_function
1718
from __future__ import with_statement
1819

1920
import os
@@ -22,9 +23,12 @@
2223
from StringIO import StringIO
2324
import tempfile
2425
import unittest
26+
from os.path import abspath, dirname, join
2527

2628
from about_code_tool import about
2729

30+
TESTDATA_PATH = join(abspath(dirname(__file__)), 'testdata')
31+
2832

2933
class BasicTest(unittest.TestCase):
3034
def test_simple_about_command_line_can_run(self):
@@ -37,27 +41,29 @@ def test_simple_about_command_line_can_run(self):
3741
shutil.rmtree(tst_fn, ignore_errors=True)
3842

3943
def test_return_path_is_not_abspath_and_contains_subdirs_on_file(self):
40-
test_input = "testdata/thirdparty/django_snippets_2413.ABOUT"
41-
test_output = "testdata/output/test_return_path_is_not_abspath_on_file.csv"
44+
# Using a relative path for the purpose of this test
45+
test_input = "about_code_tool/tests/testdata/thirdparty/django_snippets_2413.ABOUT"
46+
test_output = join(TESTDATA_PATH, 'output/test_return_path_is_not_abspath_on_file.csv')
4247
try:
4348
os.remove(test_output)
4449
except OSError:
4550
pass
4651
collector = about.AboutCollector(test_input, test_output, '0')
4752
collector.extract_about_info()
48-
self.assertTrue(open(test_output).read().partition('\n')[2].startswith('testdata/thirdparty/django_snippets_2413.ABOUT'))
53+
self.assertTrue(open(test_output).read().partition('\n')[2].startswith('about_code_tool/tests/testdata/thirdparty/django_snippets_2413.ABOUT'))
4954
os.remove(test_output)
5055

5156
def test_return_path_is_not_abspath_and_contains_subdirs_on_dir(self):
52-
test_input = "testdata/basic"
53-
test_output = "testdata/output/test_return_path_is_not_abspath_on_dir.csv"
57+
# Using a relative path for the purpose of this test
58+
test_input = "about_code_tool/tests/testdata/basic"
59+
test_output = join(TESTDATA_PATH, 'output/test_return_path_is_not_abspath_on_dir.csv')
5460
try:
5561
os.remove(test_output)
5662
except OSError:
5763
pass
5864
collector = about.AboutCollector(test_input, test_output, '0')
5965
collector.extract_about_info()
60-
self.assertTrue(open(test_output).read().partition('\n')[2].startswith('testdata/basic'))
66+
self.assertTrue(open(test_output).read().partition('\n')[2].startswith('about_code_tool/tests/testdata/basic'))
6167
os.remove(test_output)
6268

6369
def test_isvalid_about_file(self):
@@ -221,29 +227,27 @@ def test_pre_process_with_spaces_left_of_colon(self):
221227
self.assertEqual(expected, result.read())
222228

223229
def test_handles_last_line_is_a_continuation_line(self):
224-
about_obj = about.AboutFile()
225230
warnings = []
226231
warn = about.AboutFile.check_line_continuation(" Last line is a continuation line.", True)
227232
warnings.append(warn)
228233
self.assertTrue(warnings == [""], "This should not throw any warning.")
229234

230235
def test_handles_last_line_is_not_a_continuation_line(self):
231-
about_obj = about.AboutFile()
232236
warnings = []
233237
warn = about.AboutFile.check_line_continuation(" Last line is NOT a continuation line.", False)
234238
warnings.append(warn)
235239
self.assertTrue(len(warnings) == 1, "This should throw ONLY 1 warning.")
236240

237241
def test_normalize_dupe_field_names(self):
238-
about_file = about.AboutFile('testdata/parser_tests/dupe_field_name.ABOUT')
242+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/dupe_field_name.ABOUT'))
239243
expected_warnings = [about.IGNORED, 'Apache HTTP Server']
240244
self.assertTrue(len(about_file.warnings) == 1, "This should throw one warning")
241245
for w in about_file.warnings:
242246
self.assertEqual(expected_warnings[0], w.code)
243247
self.assertEqual(expected_warnings[1], w.field_value)
244248

245249
def test_normalize_lowercase(self):
246-
about_file = about.AboutFile('testdata/parser_tests/upper_field_names.ABOUT')
250+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/upper_field_names.ABOUT'))
247251
expected = {'name': 'Apache HTTP Server\nthis is a continuation',
248252
'home_url': 'http://httpd.apache.org',
249253
'download_url': 'http://archive.apache.org/dist/httpd/httpd-2.4.3.tar.gz',
@@ -257,27 +261,27 @@ def test_normalize_lowercase(self):
257261
self.assertTrue(all(item in about_file.validated_fields.items() for item in expected.items()))
258262

259263
def test_validate_about_ref_testing_the_about_resource_field_is_present(self):
260-
about_file = about.AboutFile('testdata/parser_tests/about_resource_field_present.ABOUT')
264+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_resource_field_present.ABOUT'))
261265
self.assertEquals(about_file.about_resource_path, 'about_resource.c', 'the about_resource was not detected')
262266

263267
def test_validate_about_ref_no_about_ref_key(self):
264-
about_file = about.AboutFile('testdata/parser_tests/.ABOUT')
268+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/.ABOUT'))
265269
expected_errors = [about.VALUE, 'about_resource']
266270
self.assertTrue(len(about_file.errors) == 1, "This should throw 1 error")
267271
for w in about_file.errors:
268272
self.assertEqual(expected_errors[0], w.code)
269273
self.assertEqual(expected_errors[1], w.field_name)
270274

271275
def test_validate_about_resource_error_thrown_when_file_referenced_by_about_file_does_not_exist(self):
272-
about_file = about.AboutFile('testdata/parser_tests/missing_about_ref.ABOUT')
276+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/missing_about_ref.ABOUT'))
273277
expected_errors = [about.FILE, 'about_resource']
274278
self.assertTrue(len(about_file.errors) == 1, "This should throw 1 error")
275279
for w in about_file.errors:
276280
self.assertEqual(expected_errors[0], w.code)
277281
self.assertEqual(expected_errors[1], w.field_name)
278282

279283
def test_validate_mand_fields_name_and_version_and_about_resource_present(self):
280-
about_file = about.AboutFile('testdata/parser_tests/missing_mand.ABOUT')
284+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/missing_mand.ABOUT'))
281285
expected_errors = [(about.VALUE, 'about_resource'),
282286
(about.VALUE, 'name'),
283287
(about.VALUE, 'version'), ]
@@ -286,7 +290,7 @@ def test_validate_mand_fields_name_and_version_and_about_resource_present(self):
286290
self.assertEqual(expected_errors[i][0], w.code)
287291
self.assertEqual(expected_errors[i][1], w.field_name)
288292

289-
about_file = about.AboutFile('testdata/parser_tests/missing_mand_values.ABOUT')
293+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/missing_mand_values.ABOUT'))
290294
expected_errors = [(about.VALUE, 'name'),
291295
(about.VALUE, 'version'),
292296
(about.VALUE, 'about_resource')]
@@ -296,7 +300,7 @@ def test_validate_mand_fields_name_and_version_and_about_resource_present(self):
296300
self.assertEqual(expected_errors[i][1], w.field_name)
297301

298302
def test_validate_optional_file_field_value(self):
299-
about_file = about.AboutFile('testdata/parser_tests/about_file_ref.c.ABOUT')
303+
about_file = about.AboutFile(join(TESTDATA_PATH, 'parser_tests/about_file_ref.c.ABOUT'))
300304
expected_warnings = [about.VALUE, 'notice_file']
301305
self.assertTrue(len(about_file.warnings) == 1, "This should throw one warning")
302306
for w in about_file.warnings:
@@ -376,27 +380,27 @@ def test_validate_is_ascii_key(self):
376380
self.assertFalse(about_file.check_is_ascii(u'測試'))
377381

378382
def test_validate_is_ascii_value(self):
379-
about_file = about.AboutFile('testdata/filesfields/non_ascii_field.about')
383+
about_file = about.AboutFile(join(TESTDATA_PATH, 'filesfields/non_ascii_field.about'))
380384
expected_errors = [about.ASCII]
381385
self.assertTrue(len(about_file.errors) == 1, "This should throw 1 error")
382386
self.assertEqual(about_file.errors[0].code, expected_errors[0])
383387

384388
def test_validate_spdx_licenses(self):
385-
about_file = about.AboutFile('testdata/spdx_licenses/incorrect_spdx.about')
389+
about_file = about.AboutFile(join(TESTDATA_PATH, 'spdx_licenses/incorrect_spdx.about'))
386390
expected_errors = [about.SPDX]
387391
self.assertTrue(len(about_file.errors) == 1, "This should throw 1 error")
388392
for w in about_file.errors:
389393
self.assertEqual(expected_errors[0], w.code)
390394

391395
def test_validate_spdx_licenses1(self):
392-
about_file = about.AboutFile('testdata/spdx_licenses/invalid_multi_format_spdx.ABOUT')
396+
about_file = about.AboutFile(join(TESTDATA_PATH, 'spdx_licenses/invalid_multi_format_spdx.ABOUT'))
393397
expected_errors = [about.SPDX]
394398
self.assertTrue(len(about_file.errors) == 1, "This should throw 1 error")
395399
for w in about_file.errors:
396400
self.assertEqual(expected_errors[0], w.code)
397401

398402
def test_validate_spdx_licenses2(self):
399-
about_file = about.AboutFile('testdata/spdx_licenses/invalid_multi_name.ABOUT')
403+
about_file = about.AboutFile(join(TESTDATA_PATH, 'spdx_licenses/invalid_multi_name.ABOUT'))
400404
expected_errors = [about.SPDX]
401405
# The test case is: license_spdx: Something and SomeOtherThings
402406
# Thus, it should throw 2 errors: 'Something', 'SomeOtherThings'
@@ -405,21 +409,21 @@ def test_validate_spdx_licenses2(self):
405409
self.assertEqual(expected_errors[0], w.code)
406410

407411
def test_validate_spdx_licenses3(self):
408-
about_file = about.AboutFile('testdata/spdx_licenses/lower_case_spdx.ABOUT')
412+
about_file = about.AboutFile(join(TESTDATA_PATH, 'spdx_licenses/lower_case_spdx.ABOUT'))
409413
expected_warnings = [about.SPDX]
410414
self.assertTrue(len(about_file.warnings) == 1, "This should throw one warning")
411415
for w in about_file.warnings:
412416
self.assertEqual(expected_warnings[0], w.code)
413417

414418
def test_validate_not_supported_date_format(self):
415-
about_file = about.AboutFile('testdata/DateTest/non-supported_date_format.ABOUT')
419+
about_file = about.AboutFile(join(TESTDATA_PATH, 'DateTest/non-supported_date_format.ABOUT'))
416420
expected_warnings = [about.DATE]
417421
self.assertTrue(len(about_file.warnings) == 1, "This should throw one warning")
418422
for w in about_file.warnings:
419423
self.assertEqual(expected_warnings[0], w.code)
420424

421425
def test_validate_supported_date_format(self):
422-
about_file = about.AboutFile('testdata/DateTest/supported_date_format.ABOUT')
426+
about_file = about.AboutFile(join(TESTDATA_PATH, 'DateTest/supported_date_format.ABOUT'))
423427
self.assertTrue(len(about_file.warnings) == 0, "This should not throw warning.")
424428

425429
def test_remove_blank_lines_and_field_spaces(self):
@@ -468,8 +472,8 @@ def test_remove_blank_lines_and_no_colon_fields(self):
468472

469473
def test_generate_attribution(self):
470474
expected = u'version:2.4.3about_resource:httpd-2.4.3.tar.gzname:Apache HTTP Server'
471-
about_collector = about.AboutCollector('testdata/attrib/attrib.ABOUT', None, 0)
472-
result = about_collector.generate_attribution('testdata/attrib/test.template')
475+
about_collector = about.AboutCollector(join(TESTDATA_PATH, 'attrib/attrib.ABOUT'), None, 0)
476+
result = about_collector.generate_attribution(join(TESTDATA_PATH, 'attrib/test.template'))
473477
self.assertEqual(result, expected)
474478

475479
def test_license_text_extracted_from_license_text_file(self):
@@ -478,39 +482,36 @@ def test_license_text_extracted_from_license_text_file(self):
478482
479483
* Email [email protected] for any questions'''
480484

481-
about_file = about.AboutFile('testdata/attrib/license_text.ABOUT')
485+
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/license_text.ABOUT'))
482486
license_text = about_file.license_text()
483487
self.assertEqual(license_text, expected)
484488

485489
def test_notice_text_extacted_from_notice_text_file(self):
486490
expected ='''Test component is released to Public Domain.'''
487-
about_file = about.AboutFile('testdata/attrib/license_text.ABOUT')
491+
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/license_text.ABOUT'))
488492
notice_text = about_file.notice_text()
489493
self.assertEqual(notice_text, expected)
490494

491495
def test_license_text_returns_empty_string_when_no_field_present(self):
492496
expected = ''
493-
about_file = about.AboutFile('testdata/attrib/no_text_file_field.ABOUT')
497+
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/no_text_file_field.ABOUT'))
494498
license_text = about_file.license_text()
495499
self.assertEqual(license_text, expected)
496500

497501
def test_notice_text_returns_empty_string_when_no_field_present(self):
498502
expected = ''
499-
about_file = about.AboutFile('testdata/attrib/no_text_file_field.ABOUT')
503+
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/no_text_file_field.ABOUT'))
500504
notice_text = about_file.notice_text()
501505
self.assertEqual(notice_text, expected)
502506

503507
def test_license_text_returns_empty_string_when_ref_file_doesnt_exist(self):
504508
expected = ''
505-
about_file = about.AboutFile('testdata/attrib/missing_notice_license_files.ABOUT')
509+
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/missing_notice_license_files.ABOUT'))
506510
license_text = about_file.license_text()
507511
self.assertEqual(license_text, expected)
508512

509513
def test_notice_text_returns_empty_string_when_ref_file_doesnt_exist(self):
510514
expected = ''
511-
about_file = about.AboutFile('testdata/attrib/missing_notice_license_files.ABOUT')
515+
about_file = about.AboutFile(join(TESTDATA_PATH, 'attrib/missing_notice_license_files.ABOUT'))
512516
notice_text = about_file.notice_text()
513517
self.assertEqual(notice_text, expected)
514-
515-
if __name__ == "__main__":
516-
unittest.main()

0 commit comments

Comments
 (0)