1#!/usr/bin/perl
2#
3# Check for perlcritic errors in all code.
4#
5# If author tests are enabled, check all Perl code in blib/lib, examples, usr,
6# t, and Build.PL for problems uncovered by perlcritic, ignoring template
7# files, junk, and any files explicitly configured to be ignored.
8#
9# Written by Russ Allbery <eagle@eyrie.org>
10# Copyright 2019-2022 Russ Allbery <eagle@eyrie.org>
11# Copyright 2013-2014
12#     The Board of Trustees of the Leland Stanford Junior University
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
34use 5.010;
35use strict;
36use warnings;
37
38use lib 't/lib';
39
40use Test::RRA qw(skip_unless_author use_prereq);
41use Test::RRA::Config qw(@CRITIC_IGNORE);
42
43use Test::More;
44
45# Skip tests unless we're running author tests since this test is too
46# sensitive to the exact version of Perl::Critic to be generally useful.
47skip_unless_author('Coding style tests');
48
49# Load prerequisite modules.
50use_prereq('Perl::Critic::Utils');
51use_prereq('Test::Perl::Critic');
52
53# Force the embedded Perl::Tidy check to use the correct configuration.
54local $ENV{PERLTIDY} = 't/data/perltidyrc';
55
56# Import the configuration file.
57Test::Perl::Critic->import(-profile => 't/data/perlcriticrc');
58
59# By default, Test::Perl::Critic only checks blib.  We also want to check t,
60# Build.PL, and examples.
61my @files = Perl::Critic::Utils::all_perl_files('blib');
62if (!@files) {
63    @files = Perl::Critic::Utils::all_perl_files('lib');
64}
65if (-e 'Build.PL') {
66    push(@files, 'Build.PL');
67}
68for my $dir (qw(examples usr t)) {
69    if (-d $dir) {
70        push(@files, Perl::Critic::Utils::all_perl_files($dir));
71    }
72}
73
74# Strip out Autoconf templates or left-over perltidy files.
75@files = grep { !m{ [.](?:in|tdy) }xms } @files;
76
77# Strip out ignored files.
78my %ignore = map { $_ => 1 } @CRITIC_IGNORE;
79@files = grep { !$ignore{$_} } @files;
80
81# Declare a plan now that we know what we're testing.
82plan tests => scalar @files;
83
84# Run the actual tests.
85for my $file (@files) {
86    critic_ok($file);
87}
88