Skip to content

Commit df25a69

Browse files
committed
Fixed #89 to handle duplicated keys in the input.
update the wording from "doesn't" to "does not" Added test cases/code for #89
1 parent 2cff1ee commit df25a69

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

about_code_tool/genabout.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ def __init__(self):
6969
self.errors = []
7070
self.extract_dje_license_error = False
7171

72+
@staticmethod
73+
def get_duplicated_keys(input_file):
74+
csv_context = csv.reader(open(input_file))
75+
keys_row = csv_context.next()
76+
lower_case_keys_row = [k.lower() for k in keys_row]
77+
return ([key for key in keys_row if lower_case_keys_row.count(key.lower()) > 1])
78+
7279
@staticmethod
7380
def get_input_list(input_file):
7481
csvfile = csv.DictReader(open(input_file, 'rU'))
@@ -203,7 +210,7 @@ def verify_license_files(self, input_list, project_dir, license_in_project):
203210
if _exists(license_file_path):
204211
license_files_list.append((license_file_path, about_parent_dir))
205212
else:
206-
self.warnings.append(Warn('license_text_file', license_file_path, "License doesn't exist."))
213+
self.warnings.append(Warn('license_text_file', license_file_path, "License does not exist."))
207214
except Exception as e:
208215
print(repr(e))
209216
print("The input does not have the 'license_text_file' key which is required.")
@@ -301,7 +308,7 @@ def get_dje_license_list(self, gen_location, input_list, gen_license):
301308
about_parent_dir = dirname(file_location)
302309
license_file = gen_location.rpartition('/')[0] + join(about_parent_dir, line['license_text_file'])
303310
if not _exists(license_file):
304-
self.errors.append(Error('license_text_file', license_file, "The 'license_text_file' doesn't exist."))
311+
self.errors.append(Error('license_text_file', license_file, "The 'license_text_file' does not exist."))
305312
else:
306313
if gen_license:
307314
if line['dje_license_key']:
@@ -514,17 +521,17 @@ def main(parser, options, args):
514521

515522
if copy_license_path:
516523
if not _exists(copy_license_path):
517-
print("The project path doesn't exist.")
524+
print("The project path does not exist.")
518525
sys.exit(errno.EINVAL)
519526

520527
if license_text_path:
521528
if not _exists(license_text_path):
522-
print("The license text path doesn't exist.")
529+
print("The license text path does not exist.")
523530
sys.exit(errno.EINVAL)
524531

525532
if mapping_config:
526533
if not _exists('MAPPING.CONFIG'):
527-
print("The file 'MAPPING.CONFIG' doesn't exist.")
534+
print("The file 'MAPPING.CONFIG' does not exist.")
528535
sys.exit(errno.EINVAL)
529536

530537
if extract_license:
@@ -566,6 +573,13 @@ def main(parser, options, args):
566573

567574
gen = GenAbout()
568575

576+
dup_keys = gen.get_duplicated_keys(input_path)
577+
if dup_keys:
578+
print('The input file contains duplicated keys. Duplicated keys are not allowed.')
579+
print(dup_keys)
580+
print('\nPlease fix the input file and re-run the tool.')
581+
sys.exit(errno.EINVAL)
582+
569583
# Clear the log file
570584
with open(output_path + LOG_FILENAME, 'w'):
571585
pass

about_code_tool/tests/test_genabout.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def test_validate_duplication_no_dup(self):
113113
'name': 'ABOUT tool', 'version': ''}]
114114
self.assertFalse(gen.validate_duplication(input_list), "The list has no duplication.")
115115

116+
def test_get_duplicated_keys_have_dup(self):
117+
gen = genabout.GenAbout()
118+
test_input = join(TESTDATA_PATH, "test_files_for_genabout/dup_keys.csv")
119+
expected_list = ['copyright', 'copyright']
120+
self.assertTrue(gen.get_duplicated_keys(test_input))
121+
122+
def test_get_duplicated_keys_have_dup_diff_case(self):
123+
gen = genabout.GenAbout()
124+
test_input = join(TESTDATA_PATH, "test_files_for_genabout/dup_keys_with_diff_case.csv")
125+
expected_list = ['copyright', 'Copyright']
126+
self.assertTrue(gen.get_duplicated_keys(test_input))
127+
116128
def test_validate_mandatory_fields_no_missing(self):
117129
gen = genabout.GenAbout()
118130
input_list = [{'about_file': '/about.ABOUT', 'about_resource': '.',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
about_file,about_resource,copyright,name,version,copyright
2+
about.ABOUT,.,nexB,ABOUT tool,0.8.1,someone
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
about_file,about_resource,copyright,name,version,Copyright
2+
about.ABOUT,.,nexB,ABOUT tool,0.8.1,someone

0 commit comments

Comments
 (0)