Skip to content

Add unit test for scancode-evaluate.py and improve analysis of scancode results #13703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions tools/test/travis-ci/scancode_evaluate_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python
# Copyright (c) 2020 Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import importlib
import os
import json
from unittest import TestCase

# TODO: fix scancode to match python naming conventROOTi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix "conventROOTi" typo.

SCANCODE_EVALUATE = importlib.import_module("scancode-evaluate")
license_check = SCANCODE_EVALUATE.license_check

ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__))
)

# path to stub files
stub_path = ROOT + "/scancode_test/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stub_path = ROOT + "/scancode_test/"
stub_path = os.path.join(ROOT, "scancode_test")


# template copyright notices
invalid_header_1 = "/* Copyright (C) Arm Limited, Inc - All Rights Reserved\
* Unauthorized copying of this. file, via any medium is strictly prohibited\
* Proprietary and confidential\
*/"

invalid_header_2 = "/* mbed Microcontroller Library\
* Copyright (c) 2006-2013 ARM Limited\
*\
* Licensed under the Apache License, Version 2.0 (the \"License\");\
* you may not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an \"AS IS\" BASIS,\
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*/"


# implement test class
class TestScancodeEvaluate(TestCase):
""" Test scancode evaluation script """

def test_scancode_case_1(self):
""" Test Case 1 -- faulty json file
@inputs scancode_test_1.json
@outputs -1 if any error in file licenses found
"""
# generate scancode_test_1.json
with open(ROOT + "/scancode_test/scancode_test_1.json", 'w') as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use os.path or pathlib instead of raw strings for paths, this ensures cross-platform compatibility.

json.dump({"headers": {"tool name": "scancode test fail"}}, f)

self.assertEqual(-1, license_check(ROOT, ROOT + "/scancode_test/scancode_test_1.json"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we define some named constants for the return codes so it's easier to understand what "-1" or "7" means?

os.remove(ROOT + "/scancode_test/scancode_test_1.json")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an exception is raised and the program exits before we get here, the files will not be removed. Consider using tempfile.TemporaryDirectory as a context manager to ensure clean-up happens in an exception-safe way.


def test_scancode_case_2(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_scancode_case_2(self):
def test_scancode_no_errors_in_license_headers(self):

test_scancode_case_2 is not a useful test case name. Same comment for other test cases.

""" Test Case 2 -- no errors in license headers, try multiple types i.e Apache-2.0, BSD3
@inputs scancode_test_2.json [4 Apache-2.0, 4 BSD-3.0]
@outputs 0
"""
self.assertEqual(0, license_check(ROOT, ROOT + "/scancode_test/scancode_test_2.json"), "False Negative(s)")

def test_scancode_case_3(self):
""" Test Case 3 -- all files containing errors [2 files without a header, 2 files non-permissive + missing SPDX,
1 missing SPDX]
@inputs scancode_test_3.json
@output 7
"""
# create stub files with a non-permissive license and missing spdx
for i in range(3, 6):
with open(stub_path + "test" + str(i) + ".h", "w") as file:
if i == 5:
file.write(invalid_header_2)
else:
file.write(invalid_header_1)

self.assertEqual(7, license_check(ROOT, ROOT + "/scancode_test/scancode_test_3.json"), "False Positive(s)")
# delete stub files
os.remove(stub_path + "test3.h")
os.remove(stub_path + "test4.h")
os.remove(stub_path + "test5.h")

def test_scancode_case_4(self):
""" Test Case 4 -- license header permissive and "non-permissive license" false positive
@inputs scancode_test_4.json
@outputs 0
"""
self.assertEqual(0, license_check(ROOT, ROOT + "/scancode_test/scancode_test_4.json"), "Non-Permissive Header False Positive")
Loading