Skip to content

Commit dcaf5db

Browse files
committed
Fixed #396
* Code enhancement * Added test code
1 parent 54820eb commit dcaf5db

File tree

6 files changed

+81
-10
lines changed

6 files changed

+81
-10
lines changed

src/attributecode/model.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33
# ============================================================================
4-
# Copyright (c) 2013-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
4+
# Copyright (c) 2013-2020 nexB Inc. http://www.nexb.com/ - All rights reserved.
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
77
# You may obtain a copy of the License at
@@ -1106,12 +1106,12 @@ def android_module_license(self, about_parent_path):
11061106
Create MODULE_LICENSE_XXX which the XXX is the value of license key.
11071107
"""
11081108
for lic_key in self.license_key.value:
1109-
# Make uppercase and with dash and spaces and dots replaced by underscore
1110-
# just to look similar and consistent.
1111-
name = 'MODULE_LICENSE_' + lic_key.replace('.', '_').replace('-', '_').replace(' ', '_').upper()
1112-
module_lic_path = os.path.join(about_parent_path, name)
1113-
# Create an empty MODULE_LICESE_XXX file
1114-
open(module_lic_path, 'a').close()
1109+
# Make uppercase and with dash and spaces and dots replaced by underscore
1110+
# just to look similar and consistent.
1111+
name = 'MODULE_LICENSE_' + lic_key.replace('.', '_').replace('-', '_').replace(' ', '_').upper()
1112+
module_lic_path = os.path.join(about_parent_path, name)
1113+
# Create an empty MODULE_LICESE_XXX file
1114+
open(module_lic_path, 'a').close()
11151115

11161116
def android_notice(self, about_parent_path):
11171117
"""
@@ -1128,12 +1128,14 @@ def android_notice(self, about_parent_path):
11281128
notice_file_dict = self.notice_file.value
11291129
notice_file_key = notice_file_dict.keys()
11301130
for key in notice_file_key:
1131-
notice_context += '\n' + notice_file_dict[key] + '\n'
1131+
if notice_file_dict[key]:
1132+
notice_context += '\n' + notice_file_dict[key] + '\n'
11321133
if self.license_file.value:
11331134
lic_file_dict = self.license_file.value
11341135
lic_file_key = lic_file_dict.keys()
11351136
for key in lic_file_key:
1136-
notice_context += '\n\n' + lic_file_dict[key] + '\n\n'
1137+
if lic_file_dict[key]:
1138+
notice_context += '\n\n' + lic_file_dict[key] + '\n\n'
11371139
return notice_path, notice_context
11381140

11391141
def dump_lic(self, location, license_dict):

tests/test_model.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2014-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2014-2020 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -21,6 +21,7 @@
2121
from collections import OrderedDict
2222
import io
2323
import json
24+
import os
2425
import posixpath
2526
import shutil
2627
import unittest
@@ -40,6 +41,7 @@
4041
from attributecode.util import replace_tab_with_spaces
4142

4243
from testing_utils import extract_test_loc
44+
from testing_utils import get_temp_dir
4345
from testing_utils import get_temp_file
4446
from testing_utils import get_test_loc
4547

@@ -940,6 +942,41 @@ def test_write_output_json(self):
940942
expected = get_test_loc('test_model/expected.json')
941943
check_json(expected, result)
942944

945+
def test_android_module_license(self):
946+
path = 'test_model/android/single_license.c.ABOUT'
947+
test_file = get_test_loc(path)
948+
abouts = model.About(location=test_file, about_file_path=path)
949+
950+
parent_dir = get_temp_dir()
951+
abouts.android_module_license(parent_dir)
952+
assert os.path.exists(os.path.join(parent_dir, 'MODULE_LICENSE_PUBLIC_DOMAIN'))
953+
954+
def test_android_module_multi_licenses(self):
955+
path = 'test_model/android/multi_license.c.ABOUT'
956+
test_file = get_test_loc(path)
957+
abouts = model.About(location=test_file, about_file_path=path)
958+
959+
parent_dir = get_temp_dir()
960+
abouts.android_module_license(parent_dir)
961+
assert os.path.exists(os.path.join(parent_dir, 'MODULE_LICENSE_BSD_NEW'))
962+
assert os.path.exists(os.path.join(parent_dir, 'MODULE_LICENSE_BSD_SIMPLIFIED'))
963+
964+
def test_android_notice(self):
965+
path = 'test_model/android/single_license.c.ABOUT'
966+
test_file = get_test_loc(path)
967+
abouts = model.About(location=test_file, about_file_path=path)
968+
969+
parent_dir = get_temp_dir()
970+
notice_path, notice_context = abouts.android_notice(parent_dir)
971+
expected_path = os.path.join(parent_dir, 'NOTICE')
972+
assert os.path.normpath(notice_path) == expected_path
973+
974+
expected_notice = '''Copyright (c) xyz
975+
976+
This component is released to the public domain by the author.
977+
978+
'''
979+
assert notice_context == expected_notice
943980

944981
class CollectorTest(unittest.TestCase):
945982

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Copyright (c) xyz
2+
3+
This component is released to the public domain by the author.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated with AboutCode Toolkit Version 4.0.1
2+
3+
about_resource: multi_license.c
4+
name: multi_license.c
5+
license_expression: bsd-new OR bsd-simplified
6+
copyright: |
7+
Copyright (c) xyz
8+
Copyright (c) Blah blah BlAh
9+
licenses:
10+
- key: bsd-new
11+
name: BSD-3-Clause
12+
file: bsd-new.LICENSE
13+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:bsd-new
14+
- key: bsd-simplified
15+
name: BSD-2-Clause
16+
file: bsd-simplified.LICENSE
17+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:bsd-simplified
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This component is released to the public domain by the author.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Generated with AboutCode Toolkit Version 4.0.1
2+
3+
about_resource: single_license.c
4+
name: single_license.c
5+
license_expression: public-domain
6+
copyright: Copyright (c) xyz
7+
licenses:
8+
- key: public-domain
9+
name: Public Domain
10+
file: public-domain.LICENSE
11+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:public-domain

0 commit comments

Comments
 (0)