1414# limitations under the License.
1515# =============================================================================
1616
17+ from __future__ import print_function
1718from __future__ import with_statement
1819
1920import os
2223from StringIO import StringIO
2324import tempfile
2425import unittest
26+ from os .path import abspath , dirname , join
2527
2628from about_code_tool import about
2729
30+ TESTDATA_PATH = join (abspath (dirname (__file__ )), 'testdata' )
31+
2832
2933class 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\n this 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