| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2023-2024 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.36; | ||
| 7 | use Object::Pad 0.807; | ||
| 8 | |||
| 9 | class App::perl::distrolint::Check::Test2 0.09; | ||
| 10 | |||
| 11 | apply App::perl::distrolint::CheckRole::EachFile; | ||
| 12 | apply App::perl::distrolint::CheckRole::TreeSitterPerl; | ||
| 13 | |||
| 14 | use constant DESC => "check that every unit-test uses Test2"; | ||
| 15 | use constant SORT => 20; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | C<App::perl::distrolint::Check::Test2> - check that unit tests are using C<Test2> | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This checks that every unit test (F<.t>) file contains a C<use Test2::V0> | ||
| 24 | line. | ||
| 25 | |||
| 26 | Additionally, this checks for several older modules, that have more modern | ||
| 27 | replacements within the C<Test2> system, to ensure that none of these modules | ||
| 28 | are found in C<use> statements: | ||
| 29 | |||
| 30 | =for highlighter | ||
| 31 | |||
| 32 | Test::More | ||
| 33 | |||
| 34 | Test::Deep | ||
| 35 | Test::Fatal | ||
| 36 | Test::Identity | ||
| 37 | Test::Refcount | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | my @BANNED_MODULES = qw( | ||
| 42 | Test::More Test::Deep Test::Fatal Test::Identity Test::Refcount | ||
| 43 | ); | ||
| 44 | |||
| 45 | method run ( $app ) | ||
| 46 | { | ||
| 47 | return $self->run_for_each_test_file( check_file => ); | ||
| 48 | } | ||
| 49 | |||
| 50 | my $QUERY = <<'EOF'; | ||
| 51 | (use_statement module: _ @module) @statement | ||
| 52 | EOF | ||
| 53 | |||
| 54 | method check_file ( $file ) | ||
| 55 | { | ||
| 56 | my $tree = $self->parse_perl_file( $file ); | ||
| 57 | |||
| 58 | my $found_Test2_V0; | ||
| 59 | |||
| 60 | return 0 unless $self->walk_each_query_match( $QUERY, $tree->root_node, method ( $captures ) { | ||
| 61 | my $module = $captures->{module}->text; | ||
| 62 | my $line = $captures->{statement}->start_row + 1; | ||
| 63 | |||
| 64 | $module eq $_ and | ||
| 65 | App->diag( App->format_file( $file, $line ), " uses ", App->format_literal( $module ) ), | ||
| 66 | return 0 | ||
| 67 | for @BANNED_MODULES; | ||
| 68 | |||
| 69 | $module eq "Test2::V0" and $found_Test2_V0++; | ||
| 70 | |||
| 71 | return 1; | ||
| 72 | } ); | ||
| 73 | |||
| 74 | return 1 if $found_Test2_V0; | ||
| 75 | |||
| 76 | App->diag( App->format_file( $file ), " missing ", App->format_literal( "use Test2::V0" ) ); | ||
| 77 | return 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | =head1 AUTHOR | ||
| 81 | |||
| 82 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | 0x55AA; |