<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package App::Prove::State;

use strict;
use warnings;

use File::Find;
use File::Spec;
use Carp;

use App::Prove::State::Result;
use TAP::Parser::YAMLish::Reader ();
use TAP::Parser::YAMLish::Writer ();
use base 'TAP::Base';

BEGIN {
    __PACKAGE__-&gt;mk_methods('result_class');
}

use constant IS_WIN32 =&gt; ( $^O =~ /^(MS)?Win32$/ );
use constant NEED_GLOB =&gt; IS_WIN32;

=head1 NAME

App::Prove::State - State storage for the C&lt;prove&gt; command.

=head1 VERSION

Version 3.44

=cut

our $VERSION = '3.44';

=head1 DESCRIPTION

The C&lt;prove&gt; command supports a C&lt;--state&gt; option that instructs it to
store persistent state across runs. This module implements that state
and the operations that may be performed on it.

=head1 SYNOPSIS

    # Re-run failed tests
    $ prove --state=failed,save -rbv

=cut

=head1 METHODS

=head2 Class Methods

=head3 C&lt;new&gt;

Accepts a hashref with the following key/value pairs:

=over 4

=item * C&lt;store&gt;

The filename of the data store holding the data that App::Prove::State reads.

=item * C&lt;extensions&gt; (optional)

The test name extensions.  Defaults to C&lt;.t&gt;.

=item * C&lt;result_class&gt; (optional)

The name of the C&lt;result_class&gt;.  Defaults to C&lt;App::Prove::State::Result&gt;.

=back

=cut

# override TAP::Base::new:
sub new {
    my $class = shift;
    my %args = %{ shift || {} };

    my $self = bless {
        select     =&gt; [],
        seq        =&gt; 1,
        store      =&gt; delete $args{store},
        extensions =&gt; ( delete $args{extensions} || ['.t'] ),
        result_class =&gt;
          ( delete $args{result_class} || 'App::Prove::State::Result' ),
    }, $class;

    $self-&gt;{_} = $self-&gt;result_class-&gt;new(
        {   tests      =&gt; {},
            generation =&gt; 1,
        }
    );
    my $store = $self-&gt;{store};
    $self-&gt;load($store)
      if defined $store &amp;&amp; -f $store;

    return $self;
}

=head2 C&lt;result_class&gt;

Getter/setter for the name of the class used for tracking test results.  This
class should either subclass from C&lt;App::Prove::State::Result&gt; or provide an
identical interface.

=cut

=head2 C&lt;extensions&gt;

Get or set the list of extensions that files must have in order to be
considered tests. Defaults to ['.t'].

=cut

sub extensions {
    my $self = shift;
    $self-&gt;{extensions} = shift if @_;
    return $self-&gt;{extensions};
}

=head2 C&lt;results&gt;

Get the results of the last test run.  Returns a C&lt;result_class()&gt; instance.

=cut

sub results {
    my $self = shift;
    $self-&gt;{_} || $self-&gt;result_class-&gt;new;
}

=head2 C&lt;commit&gt;

Save the test results. Should be called after all tests have run.

=cut

sub commit {
    my $self = shift;
    if ( $self-&gt;{should_save} ) {
        $self-&gt;save;
    }
}

=head2 Instance Methods

=head3 C&lt;apply_switch&gt;

 $self-&gt;apply_switch('failed,save');

Apply a list of switch options to the state, updating the internal
object state as a result. Nothing is returned.

Diagnostics:
    - "Illegal state option: %s"

=over

=item C&lt;last&gt;

Run in the same order as last time

=item C&lt;failed&gt;

Run only the failed tests from last time

=item C&lt;passed&gt;

Run only the passed tests from last time

=item C&lt;all&gt;

Run all tests in normal order

=item C&lt;hot&gt;

Run the tests that most recently failed first

=item C&lt;todo&gt;

Run the tests ordered by number of todos.

=item C&lt;slow&gt;

Run the tests in slowest to fastest order.

=item C&lt;fast&gt;

Run test tests in fastest to slowest order.

=item C&lt;new&gt;

Run the tests in newest to oldest order.

=item C&lt;old&gt;

Run the tests in oldest to newest order.

=item C&lt;save&gt;

Save the state on exit.

=back

=cut

sub apply_switch {
    my $self = shift;
    my @opts = @_;

    my $last_gen      = $self-&gt;results-&gt;generation - 1;
    my $last_run_time = $self-&gt;results-&gt;last_run_time;
    my $now           = $self-&gt;get_time;

    my @switches = map { split /,/ } @opts;

    my %handler = (
        last =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                where =&gt; sub { $_-&gt;generation &gt;= $last_gen },
                order =&gt; sub { $_-&gt;sequence }
            );
        },
        failed =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                where =&gt; sub { $_-&gt;result != 0 },
                order =&gt; sub { -$_-&gt;result }
            );
        },
        passed =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                where =&gt; sub { $_-&gt;result == 0 }
            );
        },
        all =&gt; sub {
            $self-&gt;_select( limit =&gt; shift );
        },
        todo =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                where =&gt; sub { $_-&gt;num_todo != 0 },
                order =&gt; sub { -$_-&gt;num_todo; }
            );
        },
        hot =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                where =&gt; sub { defined $_-&gt;last_fail_time },
                order =&gt; sub { $now - $_-&gt;last_fail_time }
            );
        },
        slow =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                order =&gt; sub { -$_-&gt;elapsed }
            );
        },
        fast =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                order =&gt; sub { $_-&gt;elapsed }
            );
        },
        new =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                order =&gt; sub { -$_-&gt;mtime }
            );
        },
        old =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                order =&gt; sub { $_-&gt;mtime }
            );
        },
        fresh =&gt; sub {
            $self-&gt;_select(
                limit =&gt; shift,
                where =&gt; sub { $_-&gt;mtime &gt;= $last_run_time }
            );
        },
        save =&gt; sub {
            $self-&gt;{should_save}++;
        },
        adrian =&gt; sub {
            unshift @switches, qw( hot all save );
        },
    );

    while ( defined( my $ele = shift @switches ) ) {
        my ( $opt, $arg )
          = ( $ele =~ /^([^:]+):(.*)/ )
          ? ( $1, $2 )
          : ( $ele, undef );
        my $code = $handler{$opt}
          || croak "Illegal state option: $opt";
        $code-&gt;($arg);
    }
    return;
}

sub _select {
    my ( $self, %spec ) = @_;
    push @{ $self-&gt;{select} }, \%spec;
}

=head3 C&lt;get_tests&gt;

Given a list of args get the names of tests that should run

=cut

sub get_tests {
    my $self    = shift;
    my $recurse = shift;
    my @argv    = @_;
    my %seen;

    my @selected = $self-&gt;_query;

    unless ( @argv || @{ $self-&gt;{select} } ) {
        @argv = $recurse ? '.' : 't';
        croak qq{No tests named and '@argv' directory not found}
          unless -d $argv[0];
    }

    push @selected, $self-&gt;_get_raw_tests( $recurse, @argv ) if @argv;
    return grep { !$seen{$_}++ } @selected;
}

sub _query {
    my $self = shift;
    if ( my @sel = @{ $self-&gt;{select} } ) {
        warn "No saved state, selection will be empty\n"
          unless $self-&gt;results-&gt;num_tests;
        return map { $self-&gt;_query_clause($_) } @sel;
    }
    return;
}

sub _query_clause {
    my ( $self, $clause ) = @_;
    my @got;
    my $results = $self-&gt;results;
    my $where = $clause-&gt;{where} || sub {1};

    # Select
    for my $name ( $results-&gt;test_names ) {
        next unless -f $name;
        local $_ = $results-&gt;test($name);
        push @got, $name if $where-&gt;();
    }

    # Sort
    if ( my $order = $clause-&gt;{order} ) {
        @got = map { $_-&gt;[0] }
          sort {
                 ( defined $b-&gt;[1] &lt;=&gt; defined $a-&gt;[1] )
              || ( ( $a-&gt;[1] || 0 ) &lt;=&gt; ( $b-&gt;[1] || 0 ) )
          } map {
            [   $_,
                do { local $_ = $results-&gt;test($_); $order-&gt;() }
            ]
          } @got;
    }

    if ( my $limit = $clause-&gt;{limit} ) {
        @got = splice @got, 0, $limit if @got &gt; $limit;
    }

    return @got;
}

sub _get_raw_tests {
    my $self    = shift;
    my $recurse = shift;
    my @argv    = @_;
    my @tests;

    # Do globbing on Win32.
    if (NEED_GLOB) {
        eval "use File::Glob::Windows";    # [49732]
        @argv = map { glob "$_" } @argv;
    }
    my $extensions = $self-&gt;{extensions};

    for my $arg (@argv) {
        if ( '-' eq $arg ) {
            push @argv =&gt; &lt;STDIN&gt;;
            chomp(@argv);
            next;
        }

        push @tests,
            sort -d $arg
          ? $recurse
              ? $self-&gt;_expand_dir_recursive( $arg, $extensions )
              : map { glob( File::Spec-&gt;catfile( $arg, "*$_" ) ) }
              @{$extensions}
          : $arg;
    }
    return @tests;
}

sub _expand_dir_recursive {
    my ( $self, $dir, $extensions ) = @_;

    my @tests;
    my $ext_string = join( '|', map {quotemeta} @{$extensions} );

    find(
        {   follow      =&gt; 1,      #21938
            follow_skip =&gt; 2,
            wanted      =&gt; sub {
                -f 
                  &amp;&amp; /(?:$ext_string)$/
                  &amp;&amp; push @tests =&gt; $File::Find::name;
              }
        },
        $dir
    );
    return @tests;
}

=head3 C&lt;observe_test&gt;

Store the results of a test.

=cut

# Store:
#     last fail time
#     last pass time
#     last run time
#     most recent result
#     most recent todos
#     total failures
#     total passes
#     state generation
#     parser

sub observe_test {

    my ( $self, $test_info, $parser ) = @_;
    my $name = $test_info-&gt;[0];
    my $fail = scalar( $parser-&gt;failed ) + ( $parser-&gt;has_problems ? 1 : 0 );
    my $todo = scalar( $parser-&gt;todo );
    my $start_time = $parser-&gt;start_time;
    my $end_time   = $parser-&gt;end_time,

      my $test = $self-&gt;results-&gt;test($name);

    $test-&gt;sequence( $self-&gt;{seq}++ );
    $test-&gt;generation( $self-&gt;results-&gt;generation );

    $test-&gt;run_time($end_time);
    $test-&gt;result($fail);
    $test-&gt;num_todo($todo);
    $test-&gt;elapsed( $end_time - $start_time );

    $test-&gt;parser($parser);

    if ($fail) {
        $test-&gt;total_failures( $test-&gt;total_failures + 1 );
        $test-&gt;last_fail_time($end_time);
    }
    else {
        $test-&gt;total_passes( $test-&gt;total_passes + 1 );
        $test-&gt;last_pass_time($end_time);
    }
}

=head3 C&lt;save&gt;

Write the state to a file.

=cut

sub save {
    my ($self) = @_;

    my $store = $self-&gt;{store} or return;
    $self-&gt;results-&gt;last_run_time( $self-&gt;get_time );

    my $writer = TAP::Parser::YAMLish::Writer-&gt;new;
    local *FH;
    open FH, "&gt;$store" or croak "Can't write $store ($!)";
    $writer-&gt;write( $self-&gt;results-&gt;raw, \*FH );
    close FH;
}

=head3 C&lt;load&gt;

Load the state from a file

=cut

sub load {
    my ( $self, $name ) = @_;
    my $reader = TAP::Parser::YAMLish::Reader-&gt;new;
    local *FH;
    open FH, "&lt;$name" or croak "Can't read $name ($!)";

    # XXX this is temporary
    $self-&gt;{_} = $self-&gt;result_class-&gt;new(
        $reader-&gt;read(
            sub {
                my $line = &lt;FH&gt;;
                defined $line &amp;&amp; chomp $line;
                return $line;
            }
        )
    );

    # $writer-&gt;write( $self-&gt;{tests} || {}, \*FH );
    close FH;
    $self-&gt;_regen_seq;
    $self-&gt;_prune_and_stamp;
    $self-&gt;results-&gt;generation( $self-&gt;results-&gt;generation + 1 );
}

sub _prune_and_stamp {
    my $self = shift;

    my $results = $self-&gt;results;
    my @tests   = $self-&gt;results-&gt;tests;
    for my $test (@tests) {
        my $name = $test-&gt;name;
        if ( my @stat = stat $name ) {
            $test-&gt;mtime( $stat[9] );
        }
        else {
            $results-&gt;remove($name);
        }
    }
}

sub _regen_seq {
    my $self = shift;
    for my $test ( $self-&gt;results-&gt;tests ) {
        $self-&gt;{seq} = $test-&gt;sequence + 1
          if defined $test-&gt;sequence &amp;&amp; $test-&gt;sequence &gt;= $self-&gt;{seq};
    }
}

1;
</pre></body></html>