Skip to content

Commit

Permalink
Add tests for holders to all copyright tests #930
Browse files Browse the repository at this point in the history
 * this allow to to setup a good base for improvements

Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
  • Loading branch information
pombredanne committed Mar 2, 2018
1 parent 32d6cee commit 158c503
Show file tree
Hide file tree
Showing 2,964 changed files with 11,204 additions and 126 deletions.
108 changes: 42 additions & 66 deletions tests/cluecode/cluecode_assert_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
from commoncode import saneyaml
from commoncode.testcase import FileDrivenTesting
from commoncode.text import python_safe_name
from commoncode.fileutils import parent_directory
from commoncode.fileutils import file_name

"""
Data-driven Copyright test utilities.
Expand All @@ -51,31 +49,6 @@
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')


def check_detection(expected, test_file,
what='copyrights',
notes=None,
expected_failure=False):
"""
Run detection of copyright on the `test_file`, checking the
results match the expected list of values.
`test_file` is either a path string or an iterable of text lines.
"""
test_file = test_env.get_test_loc(test_file)
parent = parent_directory(test_file)
nm = file_name(test_file)
data_file = join(parent, nm + '.yml')
tst = CopyrightTest()
tst.test_file = test_file
tst.data_file = data_file

tst.what = what
tst.expected = expected
tst.notes = notes
tst.expected_failure = expected_failure
tst.dump()


@attr.s(slots=True)
class CopyrightTest(object):
"""
Expand All @@ -101,13 +74,11 @@ class CopyrightTest(object):
# one of holders, copyrights, authors, years
what = attr.ib(default=attr.Factory(list))
copyrights = attr.ib(default=attr.Factory(list))
copyrights_expected_failure = attr.ib(default=False)
holders = attr.ib(default=attr.Factory(list))
holders_expected_failure = attr.ib(default=False)
years = attr.ib(default=attr.Factory(list))
years_expected_failure = attr.ib(default=False)
authors = attr.ib(default=attr.Factory(list))
authors_expected_failure = attr.ib(default=False)

expected_failures = attr.ib(default=attr.Factory(list))
notes = attr.ib(default=None)

def __attrs_post_init__(self, *args, **kwargs):
Expand Down Expand Up @@ -200,53 +171,58 @@ def collect(subdir):

def make_copyright_test_functions(test, test_data_dir=test_env.test_data_dir):
"""
Build and yield test functions closing on tests arguments and the function
name.
Build and return a test function closing on tests arguments and the function
name. Create only a single function for multiple tests (e.g. copyrights and
holders together).
"""

for what in test.what:

def closure_test_function(*args, **kwargs):
copyrights, authors, years, holders = cluecode.copyrights.detect(test_file)
results = OrderedDict([
('copyrights', copyrights),
('authors', authors),
('years', years),
('holders', holders),
])
result = results[what]
def closure_test_function(*args, **kwargs):
copyrights, authors, years, holders = cluecode.copyrights.detect(test_file)
results = dict(
copyrights=copyrights, authors=authors, years=years, holders=holders,)
failing = []
all_expected = []
all_results = []
for wht in what:
expected = getattr(test, wht, [])
result = results[wht]
try:
assert expected == result
except:
# On failure, we compare against more result data to get additional
# failure details, including the test_file and full results
# this assert will always fail and provide a more detailed failure trace
assert expected == results.items() + [
test_name,
'data file: file://' + data_file,
'test file: file://' + test_file,
]
all_expected.append(expected)
all_results.append(result)
failing.append(wht)

data_file = test.data_file
test_file = test.test_file
if all_expected:
all_expected += [
'failing tests: ' + ', '.join(failing),
'data file: file://' + data_file,
'test file: file://' + test_file
]

tfn = test_file.replace(test_data_dir, '').strip('\/\\')
test_name = 'test_%(what)s_%(tfn)s' % locals()
test_name = python_safe_name(test_name)
if isinstance(test_name, unicode):
test_name = test_name.encode('utf-8')
assert all_expected == all_results

expected = getattr(test, what, [])
data_file = test.data_file
test_file = test.test_file
what = test.what

closure_test_function.__name__ = test_name
closure_test_function.funcname = test_name
tfn = test_file.replace(test_data_dir, '').strip('\/\\')
whats = '_'.join(what)
test_name = 'test_%(whats)s_%(tfn)s' % locals()
test_name = python_safe_name(test_name)
if isinstance(test_name, unicode):
test_name = test_name.encode('utf-8')

expected_failure = getattr(test, what + '_expected_failure', False)
closure_test_function.__name__ = test_name
closure_test_function.funcname = test_name

if expected_failure:
closure_test_function = expectedFailure(closure_test_function)
if test.expected_failures:
closure_test_function = expectedFailure(closure_test_function)

yield closure_test_function, test_name
return closure_test_function, test_name


def build_tests(copyright_tests, clazz, test_data_dir=test_env.test_data_dir):
Expand All @@ -256,6 +232,6 @@ def build_tests(copyright_tests, clazz, test_data_dir=test_env.test_data_dir):
"""
for test in copyright_tests:
# closure on the test params
for test_method, test_name in make_copyright_test_functions(test, test_data_dir):
# attach that method to our test class
setattr(clazz, test_name, test_method)
method, name = make_copyright_test_functions(test, test_data_dir)
# attach that method to our test class
setattr(clazz, name, method)
8 changes: 8 additions & 0 deletions tests/cluecode/data/copyrights/03e16f6c_0-e_f_c.0.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 1997 Microsoft Corp., OU Microsoft Corporation, CN Microsoft Root
- Copyright (c) 1997 Microsoft Corp., OU Microsoft Corporation, CN Microsoft Root
- (c) 1997 Microsoft
holders:
- Microsoft Corp.
- OU Microsoft Corporation
- CN Microsoft Root
- Microsoft Corp.
- OU Microsoft Corporation
- CN Microsoft Root
8 changes: 8 additions & 0 deletions tests/cluecode/data/copyrights/3a3b02ce_0-a_b_ce.0.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2005, OU OISTE Foundation Endorsed, CN OISTE WISeKey Global Root
- Copyright (c) 2005, OU OISTE Foundation Endorsed, CN OISTE WISeKey Global Root
holders:
- OU OISTE Foundation
- Endorsed, CN
- OISTE WISeKey Global Root
- OU OISTE Foundation
- Endorsed, CN
- OISTE WISeKey Global Root
notes: |-
this is a certificate and the actual copyright holder is not clear:
could be either Wisekey or OISTE Foundation.
3 changes: 3 additions & 0 deletions tests/cluecode/data/copyrights/ABC_cpp-Case_cpp.cpp.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) ABC Company
holders:
- ABC Company
3 changes: 3 additions & 0 deletions tests/cluecode/data/copyrights/ABC_file_cpp-File_cpp.cpp.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) ABC Company
holders:
- ABC Company
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 1989, 1991 Free Software Foundation, Inc.
- copyrighted by the Free Software Foundation
holders:
- Free Software Foundation, Inc.
- the Free Software Foundation
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2002-2006 Sam Leffler, Errno Consulting, Atheros Communications, Inc.
holders:
- Sam Leffler, Errno Consulting, Atheros Communications, Inc.
4 changes: 2 additions & 2 deletions tests/cluecode/data/copyrights/ChangeLog.old.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
what:
what:
- copyrights

- holders
4 changes: 2 additions & 2 deletions tests/cluecode/data/copyrights/ChangeLog.other.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
what:
what:
- copyrights

- holders
4 changes: 2 additions & 2 deletions tests/cluecode/data/copyrights/ChangeLog.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
what:
what:
- copyrights

- holders
4 changes: 2 additions & 2 deletions tests/cluecode/data/copyrights/Markdownddoc.text.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
what:
what:
- copyrights

- holders
4 changes: 4 additions & 0 deletions tests/cluecode/data/copyrights/README-README.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2002-2006, Jouni Malinen <jkmaline@cc.hut.fi> and contributors
holders:
- Jouni Malinen
- and contributors
3 changes: 2 additions & 1 deletion tests/cluecode/data/copyrights/Yocto-SPDX.pdf.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
what:
what:
- copyrights
- holders
notes: from https://github.com/ttgurney/yocto-spdx/blob/master/doc/Yocto-SPDX_Manual_Install_Walkthrough.pdf
3 changes: 3 additions & 0 deletions tests/cluecode/data/copyrights/abc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2006 abc.org
holders:
- abc.org
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
what:
- copyrights
- holders
copyrights:
- copyright abc 2001
5 changes: 5 additions & 0 deletions tests/cluecode/data/copyrights/abiword_common.copyright.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 1998- AbiSource, Inc. & Co.
- Copyright (c) 2009 Masayuki Hatta
- Copyright (c) 2009 Patrik Fimml <patrik@fimml.at>
holders:
- AbiSource, Inc. & Co.
- Masayuki Hatta
- Patrik Fimml
3 changes: 2 additions & 1 deletion tests/cluecode/data/copyrights/access_strings.txt.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
what:
what:
- copyrights
- holders
3 changes: 3 additions & 0 deletions tests/cluecode/data/copyrights/acme_c-c.c.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2000 ACME, Inc.
holders:
- ACME, Inc.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
what:
- copyrights
- holders
copyrights:
- Web Applications Copyright 2009 - Thomas Hansen thomas@ra-ajax.org.
holders:
- Web Applications
- Thomas Hansen
4 changes: 4 additions & 0 deletions tests/cluecode/data/copyrights/adaptive_v1_0-Adaptive v.0.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
what:
- copyrights
- holders
copyrights:
- (c) Any Recipient
- (c) Each Recipient
holders:
- Any Recipient
- Each Recipient
4 changes: 4 additions & 0 deletions tests/cluecode/data/copyrights/addr_c-addr_c.c.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
what:
- copyrights
- holders
copyrights:
- Copyright 1999 Cornell University.
- Copyright 2000 Jon Doe.
holders:
- Cornell University.
- Jon Doe.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Not copyrighted 1992 by Mark Adler
holders:
- Mark Adler
3 changes: 3 additions & 0 deletions tests/cluecode/data/copyrights/adobe-Adobe.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2006 Adobe Systems Incorporated.
holders:
- Adobe Systems Incorporated.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 1996 - 2008. Adobe Systems Incorporated
- (c) 2001-2009, Takuo KITAME, Bart Martens, and Canonical, LTD
holders:
- Adobe Systems Incorporated
- Takuo KITAME, Bart Martens, and Canonical, LTD
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- (c) Adobe AIR
holders:
- Adobe AIR
5 changes: 5 additions & 0 deletions tests/cluecode/data/copyrights/afferogplv1-AfferoGPLv.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2002 Affero Inc.
- copyright (c) 1989, 1991 Free Software Foundation, Inc.
- copyrighted by Affero, Inc.
holders:
- Affero Inc.
- Free Software Foundation, Inc.
- Affero, Inc.
3 changes: 3 additions & 0 deletions tests/cluecode/data/copyrights/afferogplv2-AfferoGPLv.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2007 Affero Inc.
holders:
- Affero Inc.
4 changes: 4 additions & 0 deletions tests/cluecode/data/copyrights/afferogplv3-AfferoGPLv.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
what:
- copyrights
- holders
copyrights:
- Copyright (c) 2007 Free Software Foundation, Inc. <http://fsf.org/>
holders:
- Free Software Foundation, Inc.
- <http://fsf.org/>
Loading

0 comments on commit 158c503

Please sign in to comment.