]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/style/obsolete-strings-t
New upstream version 3.2
[kerberos/krb5-strength.git] / tests / style / obsolete-strings-t
1 #!/usr/bin/perl
2 #
3 # Check for obsolete strings in source files.
4 #
5 # Examine all source files in a distribution for obsolete strings and report
6 # on files that fail this check.  This catches various transitions I want to
7 # do globally in all my packages, like changing my personal URLs to https.
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 2016, 2018-2019 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.008;
35 use strict;
36 use warnings;
37
38 use lib "$ENV{C_TAP_SOURCE}/tap/perl";
39
40 use Test::RRA qw(skip_unless_author);
41 use Test::RRA::Automake qw(all_files automake_setup);
42
43 use File::Basename qw(basename);
44 use Test::More;
45
46 # Bad patterns to search for.
47 my @BAD_REGEXES = (qr{ http:// \S+ [.]eyrie[.]org }xms);
48 my @BAD_STRINGS = qw(rra@stanford.edu RRA_MAINTAINER_TESTS);
49
50 # File names to exclude from this check.
51 my %EXCLUDE = map { $_ => 1 } qw(NEWS obsolete-strings.t obsolete-strings-t);
52
53 # Only run this test for the package author, since it doesn't indicate any
54 # user-noticable flaw in the package itself.
55 skip_unless_author('Obsolete strings tests');
56
57 # Set up Automake testing.
58 automake_setup();
59
60 # Check a single file for one of the bad patterns.
61 #
62 # $path - Path to the file
63 #
64 # Returns: undef
65 sub check_file {
66     my ($path) = @_;
67     my $filename = basename($path);
68
69     # Ignore excluded and binary files.
70     return if $EXCLUDE{$filename};
71     return if !-T $path;
72
73     # Scan the file.
74     open(my $fh, '<', $path) or BAIL_OUT("Cannot open $path");
75     while (defined(my $line = <$fh>)) {
76         for my $regex (@BAD_REGEXES) {
77             if ($line =~ $regex) {
78                 ok(0, "$path contains $regex");
79                 close($fh) or BAIL_OUT("Cannot close $path");
80                 return;
81             }
82         }
83         for my $string (@BAD_STRINGS) {
84             if (index($line, $string) != -1) {
85                 ok(0, "$path contains $string");
86                 close($fh) or BAIL_OUT("Cannot close $path");
87                 return;
88             }
89         }
90     }
91     close($fh) or BAIL_OUT("Cannot close $path");
92     ok(1, $path);
93     return;
94 }
95
96 # Scan every file for any of the bad patterns or strings.  We don't declare a
97 # plan since we skip a lot of files and don't want to precalculate the file
98 # list.
99 my @paths = all_files();
100 for my $path (@paths) {
101     check_file($path);
102 }
103 done_testing();