Skip to content

Commit 648a0ce

Browse files
Add unit test for script that checks copyright and license notice
Add unit test and dependent stubs for testing of scancode-evaluate.py. license_check takes a JSON as argument and checks for source missing copyright or license notices. Each JSON represents a separate test case, and stubs are added for test case 3 and 4 where license_check looks for an spdx notice in the source file.
1 parent f3d91fd commit 648a0ce

File tree

5 files changed

+1212
-0
lines changed

5 files changed

+1212
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2020 Arm Limited and Contributors. All rights reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
import importlib
6+
import os
7+
import sys
8+
from unittest import TestCase
9+
10+
# TODO: fix scancode to match python naming conventROOTi
11+
SCANCODE_EVALUATE = importlib.import_module("scancode-evaluate")
12+
license_check = SCANCODE_EVALUATE.license_check
13+
14+
ROOT = os.path.abspath(
15+
os.path.join(os.path.dirname(__file__))
16+
)
17+
18+
# path to stub files
19+
stub_path = ROOT + "/scancode_test/"
20+
21+
# template copyright notices
22+
invalid_header_1 = "/* Copyright (C) Arm Limited, Inc - All Rights Reserved\
23+
* Unauthorized copying of this. file, via any medium is strictly prohibited\
24+
* Proprietary and confidential\
25+
*/"
26+
27+
invalid_header_2 = "/* mbed Microcontroller Library\
28+
* Copyright (c) 2006-2013 ARM Limited\
29+
*\
30+
* Licensed under the Apache License, Version 2.0 (the \"License\");\
31+
* you may not use this file except in compliance with the License.\
32+
* You may obtain a copy of the License at\
33+
*\
34+
* http://www.apache.org/licenses/LICENSE-2.0\
35+
*\
36+
* Unless required by applicable law or agreed to in writing, software\
37+
* distributed under the License is distributed on an \"AS IS\" BASIS,\
38+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
39+
* See the License for the specific language governing permissions and\
40+
* limitations under the License.\
41+
*/"
42+
43+
44+
# implement test class
45+
class TestScancodeEvaluate(TestCase):
46+
""" Test scancode evaluation script """
47+
48+
def test_scancode_case_1(self):
49+
""" Test Case 1 -- faulty json file
50+
@inputs scancode_test_1.json
51+
@outputs -1 if any error in file licenses found
52+
"""
53+
expected_result = -1
54+
test_json = ROOT + "/scancode_test/scancode_test_1.json"
55+
56+
# pass json path to test function
57+
result = license_check(ROOT, test_json)
58+
59+
self.assertEqual(expected_result, result)
60+
61+
def test_scancode_case_2(self):
62+
""" Test Case 2 -- no errors in license headers, try multiple types i.e Apache-2.0, BSD3
63+
@inputs scancode_test_2.json [4 Apache-2.0, 4 BSD-3.0]
64+
@outputs 0
65+
"""
66+
expected_result = 0
67+
test_json = ROOT + "/scancode_test/scancode_test_2.json"
68+
69+
result = license_check(ROOT, test_json)
70+
self.assertEqual(expected_result, result, "False Negative(s)")
71+
72+
def test_scancode_case_3(self):
73+
""" Test Case 3 -- all files containing errors
74+
@inputs scancode_test_3.json [2 no header, 2 non-permissive + spdx, 1 missing SPDX]
75+
@output 5
76+
"""
77+
# create stub files with a non-permissive license and missing spdx
78+
for i in range(3, 6):
79+
with open(stub_path + "test" + str(i) + ".h", "w") as file:
80+
if i == 5:
81+
file.write(invalid_header_2)
82+
else:
83+
file.write(invalid_header_1)
84+
85+
expected_result = 7
86+
test_json = ROOT + "/scancode_test/scancode_test_3.json"
87+
88+
result = license_check(ROOT, test_json)
89+
90+
self.assertEqual(expected_result, result, "False Positive(s)")
91+
# delete stub files
92+
os.remove(stub_path + "test3.h")
93+
os.remove(stub_path + "test4.h")
94+
os.remove(stub_path + "test5.h")
95+
96+
def test_scancode_case_4(self):
97+
""" Test Case 4 -- license header permissive and non-permissive 'license' [FP]
98+
@inputs scancode_test_4.json
99+
@outputs 0
100+
"""
101+
expected_result = 0
102+
test_json = ROOT + "/scancode_test/scancode_test_4.json"
103+
104+
result = license_check(ROOT, test_json)
105+
106+
self.assertEqual(expected_result, result, "Non-Permissive Header False Positive")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"headers": [
3+
{
4+
"tool_name": "scancode test fail"
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)