Skip to content

Commit a677565

Browse files
committed
Fixed the tests and attribution
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent b147361 commit a677565

File tree

6 files changed

+119
-88
lines changed

6 files changed

+119
-88
lines changed

src/attributecode/model.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
from attributecode.util import add_unc
6666
from attributecode.util import copy_license_notice_files
6767
from attributecode.util import on_windows
68+
from attributecode.util import ungroup_licenses
6869
from attributecode.util import UNC_PREFIX
6970
from attributecode.util import UNC_PREFIX_POSIX
7071

@@ -289,6 +290,8 @@ def _validate(self, *args, **kwargs):
289290

290291
if isinstance(self.original_value, basestring):
291292
values = self.original_value.splitlines(False)
293+
elif isinstance(self.original_value, list):
294+
values = self.original_value
292295
else:
293296
values = [repr(self.original_value)]
294297

@@ -957,7 +960,6 @@ def process(self, fields, about_file_path, running_inventory=False,
957960
if license_notice_text_location:
958961
copy_license_notice_files(
959962
fields, base_dir, license_notice_text_location, afp)
960-
961963
# we validate all fields, not only these hydrated
962964
all_fields = self.all_fields()
963965
validation_errors = validate_fields(
@@ -1025,6 +1027,38 @@ def load_dict(self, fields_dict, base_dir, running_inventory=False,
10251027
about_file_path = self.about_file_path
10261028
if not with_empty:
10271029
fields = [(n, v) for n, v in fields_dict.items() if v]
1030+
for key, value in fields:
1031+
if key == u'licenses':
1032+
lic_key, lic_name, lic_file, lic_url = ungroup_licenses(value)
1033+
if lic_key:
1034+
fields.append(('license_key', lic_key))
1035+
if lic_name:
1036+
fields.append(('license_name', lic_name))
1037+
if lic_file:
1038+
fields.append(('license_file', lic_file))
1039+
if lic_url:
1040+
fields.append(('license_url', lic_url))
1041+
# The licenses field has been ungrouped and can be removed.
1042+
# Otherwise, it will gives the following INFO level error
1043+
# 'Field licenses is not a supported field and is ignored.'
1044+
licenses_field = (key, value)
1045+
fields.remove(licenses_field)
1046+
"""
1047+
# Generate about_resource_path if not present so that it can be validate
1048+
# the existence of the about_resource
1049+
arp_present = False
1050+
about_resource_value = u''
1051+
about_parent = u''
1052+
for n, v in fields:
1053+
about_parent = dirname(self.about_file_path)
1054+
if n == u'about_resource_path':
1055+
arp_present = True
1056+
elif n == u'about_resource':
1057+
about_resource_value = v
1058+
if not arp_present:
1059+
#about_parent = dirname(self.about_file_path)
1060+
fields.append((u'about_resource_path', u'.' + posixpath.join(about_parent, about_resource_value)))
1061+
"""
10281062
errors = self.process(
10291063
fields, about_file_path, running_inventory, base_dir,
10301064
license_notice_text_location, use_mapping, mapping_file)
@@ -1044,36 +1078,37 @@ def dumps(self, with_absent=False, with_empty=True):
10441078
license_name = []
10451079
license_file = []
10461080
license_url = []
1081+
file_fields = ['about_resource_path', 'notice_file', 'changelog_file', 'author_file']
10471082

10481083
for field in self.all_fields(with_absent, with_empty):
1049-
if field.name == 'license_key':
1084+
if field.name == 'license_key' and field.value:
10501085
license_key = field.value
1051-
elif field.name == 'license_name':
1086+
elif field.name == 'license_name' and field.value:
10521087
license_name = field.value
1053-
elif field.name == 'license_file':
1088+
elif field.name == 'license_file' and field.value:
10541089
license_file = field.value.keys()
1055-
elif field.name == 'license_url':
1090+
elif field.name == 'license_url' and field.value:
10561091
license_url = field.value
10571092
# No multiple 'about_resource' and 'about_resource_path' reference supported.
10581093
# Take the first element (should only be one) in the list for the
10591094
# value of 'about_resource' and 'about_resource_path'
1060-
elif field.name == 'about_resource':
1095+
elif field.name == 'about_resource' and field.value:
10611096
about_data[field.name] = field.value[0]
1062-
elif field.name == 'about_resource_path':
1097+
elif field.name in file_fields and field.value:
10631098
about_data[field.name] = field.value.keys()[0]
10641099
else:
10651100
about_data[field.name] = field.value
10661101
# Group the same license information in a list
10671102
license_group = list(zip_longest(license_key, license_name, license_file, license_url))
10681103
for lic_group in license_group:
10691104
lic_dict = {}
1070-
if lic_group[0] or with_empty:
1105+
if lic_group[0]:
10711106
lic_dict['key'] = lic_group[0]
1072-
if lic_group[1] or with_empty:
1107+
if lic_group[1]:
10731108
lic_dict['name'] = lic_group[1]
1074-
if lic_group[2] or with_empty:
1109+
if lic_group[2]:
10751110
lic_dict['file'] = lic_group[2]
1076-
if lic_group[3] or with_empty:
1111+
if lic_group[3]:
10771112
lic_dict['url'] = lic_group[3]
10781113
about_data.setdefault('licenses', []).append(lic_dict)
10791114
return saneyaml.dump(about_data)

src/attributecode/util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,25 @@ def update_about_dictionary_keys(about_dictionary_list, mapping_output):
647647
updated_dict_list.append(updated_ordered_dict)
648648
return updated_dict_list
649649

650+
def ungroup_licenses(licenses):
651+
"""
652+
Ungroup multiple licenses information
653+
"""
654+
lic_key = []
655+
lic_name = []
656+
lic_file = []
657+
lic_url = []
658+
for lic in licenses:
659+
if 'key' in lic:
660+
lic_key.append(lic['key'])
661+
if 'name' in lic:
662+
lic_name.append(lic['name'])
663+
if 'file' in lic:
664+
lic_file.append(lic['file'])
665+
if 'url' in lic:
666+
lic_url.append(lic['url'])
667+
return lic_key, lic_name, lic_file, lic_url
668+
650669
class NoDuplicateConstructor(Constructor):
651670
def construct_mapping(self, node, deep=False):
652671
if not isinstance(node, MappingNode):

tests/test_gen.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ def test_load_inventory(self):
6262
assert expected_errors == errors
6363

6464
expected = [u'about_resource: .\n'
65-
u'name: AboutCode\n'
66-
u'version: 0.11.0\n'
67-
u'description: |\n'
65+
u'description: |-\n'
6866
u' multi\n'
69-
u' line\n']
67+
u' line\n'
68+
u'name: AboutCode\n'
69+
u'version: 0.11.0\n']
7070
result = [a.dumps(with_absent=False, with_empty=False)
7171
for a in abouts]
7272
assert expected == result
@@ -86,13 +86,13 @@ def test_load_inventory_with_mapping(self):
8686
assert sorted(expected_errors) == sorted(errors)
8787

8888
expected = [u'about_resource: .\n'
89-
u'name: AboutCode\n'
90-
u'version: 0.11.0\n'
91-
u'description: |\n'
89+
u'copyright: Copyright (c) nexB, Inc.\n'
90+
u'description: |-\n'
9291
u' multi\n'
9392
u' line\n'
94-
u'copyright: Copyright (c) nexB, Inc.\n'
93+
u'name: AboutCode\n'
9594
u'resource: this.ABOUT\n'
95+
u'version: 0.11.0\n'
9696
]
9797
result = [a.dumps(with_absent=False, with_empty=False)
9898
for a in abouts]
@@ -151,12 +151,12 @@ def test_generate(self):
151151
in_mem_result = [a.dumps(with_absent=False, with_empty=False)
152152
for a in abouts][0]
153153
expected = (u'about_resource: .\n'
154-
u'name: AboutCode\n'
155154
u'about_resource_path: .\n'
156-
u'version: 0.11.0\n'
157-
u'description: |\n'
155+
u'description: |-\n'
158156
u' multi\n'
159-
u' line\n')
157+
u' line\n'
158+
u'name: AboutCode\n'
159+
u'version: 0.11.0\n')
160160
assert expected == in_mem_result
161161

162162
def test_generate_not_overwrite_original_license_file(self):
@@ -170,10 +170,11 @@ def test_generate_not_overwrite_original_license_file(self):
170170
in_mem_result = [a.dumps(with_absent=False, with_empty=False)
171171
for a in abouts][0]
172172
expected = (u'about_resource: .\n'
173-
u'name: AboutCode\n'
174173
u'about_resource_path: .\n'
175-
u'version: 0.11.0\n'
176-
u'license_file: this.LICENSE\n')
174+
u'licenses:\n'
175+
u' - file: this.LICENSE\n'
176+
u'name: AboutCode\n'
177+
u'version: 0.11.0\n')
177178
assert expected == in_mem_result
178179

179180
def test_deduplicate(self):

tests/test_model.py

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -586,47 +586,6 @@ def FAILING_test_About_equals_with_small_text_differences(self):
586586
assert a.dumps(True) == b.dumps(True)
587587
assert a == b
588588

589-
def test_About_dumps_does_not_transform_strings_in_lists(self):
590-
test_file = get_test_loc('dumps/complete2/about.ABOUT')
591-
a = model.About(test_file, about_file_path='complete2/about.ABOUT')
592-
expected = u'''about_resource: .
593-
name: AboutCode
594-
about_resource_path:
595-
version: 0.11.0
596-
download_url:
597-
description: AboutCode is a tool to process ABOUT files. An ABOUT file is a file.
598-
homepage_url: http://dejacode.org
599-
notes:
600-
license_expression: apache-2.0
601-
license_key: apache-2.0
602-
license_name:
603-
license_file: apache-2.0.LICENSE
604-
license_url:
605-
copyright: Copyright (c) 2013-2014 nexB Inc.
606-
notice_file: NOTICE
607-
notice_url:
608-
redistribute:
609-
attribute:
610-
track_changes:
611-
modified:
612-
changelog_file:
613-
owner: nexB Inc.
614-
owner_url:
615-
contact:
616-
author: Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez
617-
author_file:
618-
vcs_tool: git
619-
vcs_repository: https://github.com/dejacode/about-code-tool.git
620-
vcs_path:
621-
vcs_tag:
622-
vcs_branch:
623-
vcs_revision:
624-
checksum_md5:
625-
checksum_sha1:
626-
checksum_sha256:
627-
spec_version:
628-
'''
629-
assert expected == a.dumps(with_absent=True)
630589

631590
def test_About_same_attribution(self):
632591
base_dir = 'some_dir'
@@ -740,28 +699,37 @@ def test_About_dumps(self):
740699
a = model.About(test_file)
741700
assert [] == a.errors
742701

702+
# Since the licenses group was not processed,
743703
expected = u'''about_resource: .
744-
name: AboutCode
745-
version: 0.11.0
746-
description: |
704+
author:
705+
- Jillian Daguil
706+
- Chin Yeung Li
707+
- Philippe Ombredanne
708+
- Thomas Druez
709+
copyright: Copyright (c) 2013-2014 nexB Inc.
710+
description: |-
747711
AboutCode is a tool
748712
to process ABOUT files.
749713
An ABOUT file is a file.
750-
homepage_url: http://dejacode.org
714+
homepage_url:
715+
- http://dejacode.org
751716
license_expression: apache-2.0
752-
license_key: apache-2.0
753-
license_file: apache-2.0.LICENSE
754-
copyright: Copyright (c) 2013-2014 nexB Inc.
717+
licenses:
718+
- file: apache-2.0.LICENSE
719+
key: apache-2.0
720+
name: AboutCode
755721
notice_file: NOTICE
756-
owner: nexB Inc.
757-
author: Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez
758-
vcs_tool: git
722+
owner:
723+
- nexB Inc.
759724
vcs_repository: https://github.com/dejacode/about-code-tool.git
725+
vcs_tool: git
726+
version: 0.11.0
760727
'''
761728
result = a.dumps()
762729
assert expected == result
763730

764-
def test_About_dumps_all_fields_if_not_present_with_absent_True(self):
731+
# We do not support with_absent and with_empty staring in version 3.2.0.
732+
def FAILING_test_About_dumps_all_fields_if_not_present_with_absent_True(self):
765733
test_file = get_test_loc('parse/complete2/about.ABOUT')
766734
a = model.About(test_file)
767735
expected_error = [

tests/testdata/dumps/complete2/about.ABOUT

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ name: AboutCode
44
version: 0.11.0
55

66
owner: nexB Inc.
7-
author: Jillian Daguil,
8-
Chin Yeung Li,
9-
Philippe Ombredanne,
10-
Thomas Druez
7+
author:
8+
- Jillian Daguil,
9+
- Chin Yeung Li,
10+
- Philippe Ombredanne,
11+
- Thomas Druez
1112

1213
homepage_url: http://dejacode.org
1314

1415
vcs_tool: git
1516
vcs_repository: https://github.com/dejacode/about-code-tool.git
1617

17-
description: AboutCode is a tool to process ABOUT files.
18-
An ABOUT file is a file.
18+
description: |-
19+
AboutCode is a tool to process ABOUT files.
20+
An ABOUT file is a file.
1921

20-
license_key: apache-2.0
2122
license_expression: apache-2.0
22-
license_file: apache-2.0.LICENSE
23+
licenses:
24+
- key: apache-2.0
25+
file: apache-2.0.LICENSE
26+
2327
copyright: Copyright (c) 2013-2014 nexB Inc.
2428

2529
notice_file: NOTICE

tests/testdata/parse/complete/about.ABOUT

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ name: AboutCode
44
version: 0.11.0
55

66
owner: nexB Inc.
7-
author: Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez
7+
author:
8+
- Jillian Daguil
9+
- Chin Yeung Li
10+
- Philippe Ombredanne
11+
- Thomas Druez
812

913
homepage_url: http://dejacode.org
1014

1115
vcs_tool: git
1216
vcs_repository: https://github.com/dejacode/about-code-tool.git
1317

14-
description: |
18+
description: |-
1519
AboutCode is a tool
1620
to process ABOUT files.
1721
An ABOUT file is a file.

0 commit comments

Comments
 (0)