Claude-Agent-Code-Review

 view release on metacpan or  search on metacpan

t/05-perlcritic.t  view on Meta::CPAN

    subtest 'category mapping' => sub {
        chdir($tempdir);

        my $test_file = path($tempdir, 'category_test.pm');
        $test_file->spew_utf8(<<'END');
package CategoryTest;
use strict;
use warnings;

# This might trigger various categories
sub complex_function {
    my $data = shift;
    if ($data) {
        if ($data->{foo}) {
            if ($data->{bar}) {
                return $data->{baz} && $data->{qux} ? 1 : 0;
            }
        }
    }
    return 0;
}

1;
END

        my $options = Claude::Agent::Code::Review::Options->new(
            perlcritic          => 1,
            perlcritic_severity => 1,
        );

        my @issues = Claude::Agent::Code::Review::Perlcritic->analyze(
            paths   => ['category_test.pm'],
            options => $options,
        );

        if (@issues) {
            my %valid_categories = map { $_ => 1 }
                qw(bugs security style performance maintainability);
            for my $issue (@issues) {
                ok($valid_categories{$issue->category},
                   "category '" . $issue->category . "' is valid");
            }
        }

        chdir($orig_dir);
    };

    subtest 'path traversal protection' => sub {
        chdir($tempdir);

        my $options = Claude::Agent::Code::Review::Options->new(
            perlcritic => 1,
        );

        # Try to analyze files outside project directory
        my @issues = Claude::Agent::Code::Review::Perlcritic->analyze(
            paths   => ['../../../etc/passwd'],
            options => $options,
        );

        is(scalar @issues, 0, 'path traversal blocked');

        chdir($orig_dir);
    };

    subtest 'handles parse errors gracefully' => sub {
        chdir($tempdir);

        my $broken_file = path($tempdir, 'broken.pm');
        $broken_file->spew_utf8(<<'END');
package Broken;
sub foo {
    # Unclosed brace
1;
END

        my $options = Claude::Agent::Code::Review::Options->new(
            perlcritic => 1,
        );

        # Should not die, may report error as an issue
        my @issues = eval {
            Claude::Agent::Code::Review::Perlcritic->analyze(
                paths   => ['broken.pm'],
                options => $options,
            );
        };

        ok(!$@, 'did not die on parse error');
        # May have an issue about the parse error
        diag("Parse error handling: " . scalar(@issues) . " issues returned");

        chdir($orig_dir);
    };

    subtest 'multiple file types' => sub {
        chdir($tempdir);

        # Create various Perl file types
        path($tempdir, 'script.pl')->spew_utf8("#!/usr/bin/perl\nuse strict;\nprint 1;\n");
        path($tempdir, 'module.pm')->spew_utf8("package Module;\nuse strict;\n1;\n");
        path($tempdir, 'test.t')->spew_utf8("use Test::More;\nok(1);\ndone_testing;\n");

        my $options = Claude::Agent::Code::Review::Options->new(
            perlcritic          => 1,
            perlcritic_severity => 5,
        );

        my @issues = Claude::Agent::Code::Review::Perlcritic->analyze(
            paths   => ['.'],
            options => $options,
        );

        ok(defined \@issues, 'analyzed multiple file types');
        diag("Found " . scalar(@issues) . " issues across .pl, .pm, .t files");

        chdir($orig_dir);
    };
}

done_testing();



( run in 1.517 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )