]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/docs/spdx-license-t
Use new support for SPDX check exclusions
[kerberos/krb5-strength.git] / tests / docs / spdx-license-t
1 #!/usr/bin/perl
2 #
3 # Check source files for SPDX-License-Identifier fields.
4 #
5 # Examine all source files in a distribution to check that they contain an
6 # SPDX-License-Identifier field.  This does not check the syntax or whether
7 # the identifiers are valid.
8 #
9 # The canonical version of this file is maintained in the rra-c-util package,
10 # which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
11 #
12 # Copyright 2018-2023 Russ Allbery <eagle@eyrie.org>
13 #
14 # Permission is hereby granted, free of charge, to any person obtaining a
15 # copy of this software and associated documentation files (the "Software"),
16 # to deal in the Software without restriction, including without limitation
17 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 # and/or sell copies of the Software, and to permit persons to whom the
19 # Software is furnished to do so, subject to the following conditions:
20 #
21 # The above copyright notice and this permission notice shall be included in
22 # all copies or substantial portions of the Software.
23 #
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 # DEALINGS IN THE SOFTWARE.
31 #
32 # SPDX-License-Identifier: MIT
33
34 use 5.010;
35 use strict;
36 use warnings;
37
38 use lib "$ENV{C_TAP_SOURCE}/tap/perl";
39
40 use Test::RRA qw(skip_unless_automated);
41 use Test::RRA::Automake qw(all_files automake_setup);
42 use Test::RRA::Config qw(@SPDX_IGNORE);
43
44 use File::Basename qw(basename);
45 use Test::More;
46
47 # File name (the file without any directory component) and path patterns to
48 # skip for this check.
49 ## no critic (RegularExpressions::ProhibitFixedStringMatches)
50 my @IGNORE = (
51     qr{ \A LICENSE \z }xms,             # Generated file with no license itself
52     qr{ \A (NEWS|THANKS|TODO) \z }xms,  # Package license should be fine
53     qr{ \A README ( [.] .* )? \z }xms,  # Package license should be fine
54     qr{ \A (Makefile|libtool) \z }xms,  # Generated file
55     qr{ [.] json \z }xms,               # Data files without comment support
56     qr{ ~ \z }xms,                      # Backup files
57     qr{ [.] l?a \z }xms,                # Created by libtool
58     qr{ [.] o \z }xms,                  # Compiler objects
59     qr{ [.] output \z }xms,             # Test data
60 );
61 my @IGNORE_PATHS = (
62     @SPDX_IGNORE,
63     qr{ \A debian/ }xms,                       # Found in debian/* branches
64     qr{ \A docs/metadata/ }xms,                # Package license should be fine
65     qr{ \A docs/protocol[.](html|txt) \z }xms, # Generated by xml2rfc
66     qr{ \A m4/ (libtool|lt.*) [.] m4 \z }xms,  # Files from Libtool
67     qr{ \A perl/Build \z }xms,                 # Perl build files
68     qr{ \A perl/MANIFEST \z }xms,              # Perl build files
69     qr{ \A perl/MYMETA [.] }xms,               # Perl build files
70     qr{ \A perl/blib/ }xms,                    # Perl build files
71     qr{ \A perl/cover_db/ }xms,                # Perl test files
72     qr{ \A perl/_build }xms,                   # Perl build files
73     qr{ \A php/Makefile [.] global \z }xms,    # Created by phpize
74     qr{ \A php/autom4te [.] cache/ }xms,       # Created by phpize
75     qr{ \A php/acinclude [.] m4 \z }xms,       # Created by phpize
76     qr{ \A php/build/ }xms,                    # Created by phpize
77     qr{ \A php/config [.] (guess|sub) \z }xms, # Created by phpize
78     qr{ \A php/configure [.] (ac|in) \z }xms,  # Created by phpize
79     qr{ \A php/ltmain [.] sh \z }xms,          # Created by phpize
80     qr{ \A php/run-tests [.] php \z }xms,      # Created by phpize
81     qr{ \A php/ .* [.] dep \z }xms,            # Created by phpize
82     qr{ \A python/ .* [.] egg-info/ }xms,      # Python build files
83     qr{ \A tests/config/ (?!README) }xms,      # Test configuration
84     qr{ \A tests/tmp/ }xms,                    # Temporary test files
85     qr{ [.] mypy_cache/ }xms,                  # mypy caches
86 );
87 ## use critic
88
89 # Only run this test during automated testing, since failure doesn't indicate
90 # any user-noticable flaw in the package itself.
91 skip_unless_automated('SPDX identifier tests');
92
93 # Set up Automake testing.
94 automake_setup();
95
96 # Check a single file for an occurrence of the string.
97 #
98 # $path - Path to the file
99 #
100 # Returns: undef
101 sub check_file {
102     my ($path) = @_;
103     my $filename = basename($path);
104
105     # Ignore files in the whitelist and binary files.
106     for my $pattern (@IGNORE) {
107         return if $filename =~ $pattern;
108     }
109     for my $pattern (@IGNORE_PATHS) {
110         return if $path =~ $pattern;
111     }
112     return if !-T $path;
113
114     # Scan the file.
115     my ($saw_legacy_notice, $saw_spdx, $skip_spdx);
116     open(my $file, '<', $path) or BAIL_OUT("Cannot open $path: $!");
117     while (defined(my $line = <$file>)) {
118         if ($line =~ m{ Generated [ ] by [ ] libtool [ ] }xms) {
119             close($file) or BAIL_OUT("Cannot close $path: $!");
120             return;
121         }
122         if ($line =~ m{ \b See \s+ LICENSE \s+ for \s+ licensing }xms) {
123             $saw_legacy_notice = 1;
124         }
125         if ($line =~ m{ \b SPDX-License-Identifier: \s+ \S+ }xms) {
126             $saw_spdx = 1;
127             last;
128         }
129         if ($line =~ m{ no \s SPDX-License-Identifier \s registered }xms) {
130             $skip_spdx = 1;
131             last;
132         }
133     }
134     close($file) or BAIL_OUT("Cannot close $path: $!");
135
136     # If there is a legacy license notice, report a failure regardless of file
137     # size.  Otherwise, skip files under 1KB.  They can be rolled up into the
138     # overall project license and the license notice may be a substantial
139     # portion of the file size.
140     if ($saw_legacy_notice) {
141         ok(!$saw_legacy_notice, "$path has legacy license notice");
142     } else {
143         ok($saw_spdx || $skip_spdx || -s $path < 1024, $path);
144     }
145     return;
146 }
147
148 # Scan every file.  We don't declare a plan since we skip a lot of files and
149 # don't want to precalculate the file list.
150 my @paths = all_files();
151 for my $path (@paths) {
152     check_file($path);
153 }
154 done_testing();