Skip to content

Commit 6acaf27

Browse files
authored
Move licence check into pytest (#1442)
* Add licence check inside pytest * Remove licence check from improver-tests shell script * Expand comment * Simplify licence finding code
1 parent b56379f commit 6acaf27

File tree

2 files changed

+68
-67
lines changed

2 files changed

+68
-67
lines changed

bin/improver-tests

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -56,68 +56,6 @@ function get_python_files {
5656
! -name '*~' \)`
5757
}
5858

59-
function licence_check {
60-
# Iterate through files to check whether they contain the expected
61-
# licence information. If any files do not contain the expected licence
62-
# information, then this test will fail.
63-
read -d '' expected <<'__TEXT__' || true
64-
# -*- coding: utf-8 -*-
65-
# -----------------------------------------------------------------------------
66-
# (C) British Crown Copyright 2017-2021 Met Office.
67-
# All rights reserved.
68-
#
69-
# Redistribution and use in source and binary forms, with or without
70-
# modification, are permitted provided that the following conditions are met:
71-
#
72-
# * Redistributions of source code must retain the above copyright notice, this
73-
# list of conditions and the following disclaimer.
74-
#
75-
# * Redistributions in binary form must reproduce the above copyright notice,
76-
# this list of conditions and the following disclaimer in the documentation
77-
# and/or other materials provided with the distribution.
78-
#
79-
# * Neither the name of the copyright holder nor the names of its
80-
# contributors may be used to endorse or promote products derived from
81-
# this software without specific prior written permission.
82-
#
83-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
84-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86-
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
87-
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
88-
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
89-
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
90-
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
91-
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
92-
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
93-
# POSSIBILITY OF SUCH DAMAGE.
94-
__TEXT__
95-
96-
FILES_TO_TEST=$@
97-
98-
count=0
99-
for FILE in $FILES_TO_TEST; do
100-
# Check whether file has a size greater than zero.
101-
if [[ -s $FILE ]]; then
102-
file_contents=$(<$FILE)
103-
if [[ "$file_contents" != *"$expected"* ]]; then
104-
echo "Unexpected licence information: ${FILE#$IMPROVER_DIR}"
105-
count=$((count+1))
106-
fi
107-
fi
108-
done
109-
if (( $count > 0 )); then
110-
echo_fail "IMPROVER licences"
111-
exit 1
112-
fi
113-
}
114-
115-
function improver_test_licence {
116-
# utf8 and BSD 3-clause licence testing.
117-
licence_check $FILES_TO_TEST
118-
echo_ok "IMPROVER licences"
119-
}
120-
12159
function improver_test_black {
12260
${BLACK:-black} $FILES_TO_TEST
12361
echo_ok "black"
@@ -128,7 +66,6 @@ function improver_test_isort {
12866
echo_ok "isort"
12967
}
13068

131-
13269
function improver_test_pylint {
13370
# Pylint score generation.
13471
${PYLINT:-pylint} --rcfile=etc/pylintrc $FILES_TO_TEST
@@ -208,9 +145,9 @@ Optional arguments:
208145
209146
Arguments:
210147
SUBTEST Name(s) of a subtest to run without running the rest.
211-
Valid names are: black, isort, pylint, pylintE, licence,
148+
Valid names are: black, isort, pylint, pylintE,
212149
doc, unit, cli, and recreate_checksums. The default tests
213-
are black, isort, pylintE, licence, doc, unit,
150+
are black, isort, pylintE, doc, unit,
214151
and cli.
215152
Using recreate_checksums will regenerate the
216153
test data checksum file which is used as part of the cli
@@ -255,7 +192,7 @@ for arg in "$@"; do
255192
print_usage
256193
exit 0
257194
;;
258-
black|isort|pylint|pylintE|licence|doc|unit|cli|recreate_checksums)
195+
black|isort|pylint|pylintE|doc|unit|cli|recreate_checksums)
259196
SUBTESTS="$SUBTESTS $arg"
260197
;;
261198
$cli_tasks)
@@ -273,7 +210,7 @@ if [[ -n "$SUBTESTS" ]]; then
273210
TESTS="$SUBTESTS"
274211
else
275212
# Default tests.
276-
TESTS="isort black pylintE licence doc unit cli"
213+
TESTS="isort black pylintE doc unit cli"
277214
fi
278215

279216
# If cli sub test is not specified by user, do all cli tests.

improver_tests/test_licence.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
# -----------------------------------------------------------------------------
3+
# (C) British Crown Copyright 2017-2021 Met Office.
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this
10+
# list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of the copyright holder nor the names of its
17+
# contributors may be used to endorse or promote products derived from
18+
# this software without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
# POSSIBILITY OF SUCH DAMAGE.
31+
"""Licence checks"""
32+
33+
from pathlib import Path
34+
35+
36+
def self_licence():
37+
"""Collect licence text from this file"""
38+
self_lines = Path(__file__).read_text().splitlines()
39+
licence_lines = list()
40+
for line in self_lines:
41+
if not line.startswith("#"):
42+
break
43+
licence_lines.append(line)
44+
licence = "\n".join(licence_lines)
45+
return licence
46+
47+
48+
def test_py_licence():
49+
"""
50+
Check that non-empty python files contain the utf8 header and
51+
3-clause BSD licence text
52+
"""
53+
top_level = (Path(__file__).parent / "..").resolve()
54+
directories_covered = [top_level / "improver", top_level / "improver_tests"]
55+
failed_files = []
56+
licence_text = self_licence()
57+
for directory in directories_covered:
58+
python_files = list(directory.glob("**/*.py"))
59+
for file in python_files:
60+
contents = file.read_text()
61+
# skip zero-byte empty files such as __init__.py
62+
if len(contents) > 0 and licence_text not in contents:
63+
failed_files.append(str(file))
64+
assert len(failed_files) == 0, "\n".join(failed_files)

0 commit comments

Comments
 (0)