Skip to content

Commit a57cbaf

Browse files
committed
Add more tests for util. check_duplicate_keys_about_file
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 13f5fcd commit a57cbaf

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

src/attributecode/util.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,31 @@ def check_file_names(paths):
116116
return errors
117117

118118

119-
def check_duplicate_keys_about_file(context):
119+
def check_duplicate_keys_about_file(about_text):
120120
"""
121-
121+
Return a list of duplicated keys given a ABOUT text string.
122122
"""
123-
keys = []
124-
dup_keys = []
125-
for line in context.splitlines():
123+
seen = set()
124+
duplicates = set()
125+
for line in about_text.splitlines():
126126
"""
127-
Ignore all the continuation string, string block and empty line
127+
Ignore all the continuation string, mapping/list dahs, string block and empty line.
128128
"""
129-
if not line.startswith(' ') and not len(line.strip()) == 0 :
130-
# Get the key name
131-
key = line.partition(':')[0]
132-
if key in keys:
133-
dup_keys.append(key)
134-
else:
135-
keys.append(key)
136-
return dup_keys
129+
if not line.strip() :
130+
continue
131+
if line.startswith((' ', '\t')):
132+
continue
133+
if line.strip().startswith('-'):
134+
continue
135+
if ':' not in line:
136+
continue
137+
# Get the key name
138+
key, _, _val = line.partition(':')
139+
if key in seen:
140+
duplicates.add(key)
141+
else:
142+
seen.add(key)
143+
return sorted(duplicates)
137144

138145

139146
def wrap_boolean_value(context):

tests/test_util.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,44 @@ def test_format_about_dict_for_json_output(self):
582582

583583
class TestMiscUtils(unittest.TestCase):
584584

585-
def test_check_duplicate_keys_about_file(self):
585+
def test_check_duplicate_keys_about_file_with_no_dupe(self):
586+
test = '''
587+
name: test
588+
589+
license_expression: mit
590+
notes: dup key here
591+
'''
592+
expected = []
593+
assert expected == util.check_duplicate_keys_about_file(test)
594+
595+
596+
def test_check_duplicate_keys_about_file_returns_duplicate(self):
586597
test = '''
587598
name: test
588599
notes: some notes
600+
notes: dup key here
589601
602+
notes: dup key here
590603
license_expression: mit
591604
notes: dup key here
592605
'''
593606
expected = ['notes']
594607
assert expected == util.check_duplicate_keys_about_file(test)
595608

609+
def test_check_duplicate_keys_about_file_ignore_non_key_line(self):
610+
test = '''
611+
name: test
612+
- notes: some notes
613+
- notes: dup key here
614+
# some
615+
616+
notes: dup key here
617+
license_expression: mit
618+
notes dup key here
619+
'''
620+
expected = []
621+
assert expected == util.check_duplicate_keys_about_file(test)
622+
596623
def test_wrap_boolean_value(self):
597624
test = '''
598625
name: test
@@ -624,7 +651,8 @@ def test_check_duplicate_keys_about_file_with_multiline(self):
624651
line
625652
description: sample
626653
'''
627-
expected = ['owner', 'notes']
654+
# notes: the output IS sorted
655+
expected = ['notes', 'owner', ]
628656
assert expected == util.check_duplicate_keys_about_file(test)
629657

630658
def test_inventory_filter(self):

0 commit comments

Comments
 (0)