diff --git a/lib/Perl/Critic/Role/Cacheable.pm b/lib/Perl/Critic/Role/Cacheable.pm index f496439..789f641 100644 --- a/lib/Perl/Critic/Role/Cacheable.pm +++ b/lib/Perl/Critic/Role/Cacheable.pm @@ -92,7 +92,7 @@ The name of the file where the cache will be stored. } sub _cacheable_config_digest ($self) { - return $self->{_cacheable}->{rc_digest}; + return $self->{_cacheable}->{config_digest}; } sub _cacheable_digest_ok ($self) { @@ -156,7 +156,8 @@ critiqued and had no violations will not be checked again. around critique => sub ($orig, $self, $source_code) { - my $filename = !is_ref $source_code ? Path::Tiny->new($source_code)->absolute->stringify : undef; + $DB::single = 1; + my $filename = !Ref::Util::is_ref $source_code ? Path::Tiny->new($source_code)->absolute->stringify : undef; if($filename) { return () if $self->_cacheable_check_cache_ok($filename); } diff --git a/t/perl_critic_role_cacheable.t b/t/perl_critic_role_cacheable.t index 28c7567..6deb4eb 100644 --- a/t/perl_critic_role_cacheable.t +++ b/t/perl_critic_role_cacheable.t @@ -1,22 +1,177 @@ use Test2::V0 -no_srand => 1; +use experimental qw( signatures postderef ); use Perl::Critic::Role::Cacheable; +use Path::Tiny; -package Perl::Critic::Cacheable { +my @faux_violations; +my $call_count; + +package MyCritic { use parent 'Perl::Critic'; use Role::Tiny::With (); Role::Tiny::With::with 'Perl::Critic::Role::Cacheable'; + + sub critique ($self, $source_code ) { + $call_count++; + my @old = @faux_violations; + @faux_violations = (); + return @old; + } + + sub new ($self, @args) { + $call_count = 0; + return $self->SUPER::new(@args); + } } subtest 'very basic' => sub { - my $critic = Perl::Critic::Cacheable->new; + my $critic = MyCritic->new; isa_ok $critic, 'Perl::Critic'; ok $critic->can('new'), 'has new method'; ok !$critic->can('around'), 'does not have around method'; }; -done_testing; +subtest 'cache' => sub { + my $root = Path::Tiny->tempdir; + + my @perl_source = (map { $root->child($_) } qw( source1.pl source2.pm)); + $perl_source[0]->spew('foo'); + $perl_source[1]->spew('bar'); + + my $profile = $root->child('profile'); + $profile->spew(''); + + my $cache = $root->child('cache'); + + subtest 'first run (no cache)' => sub { + my $critic = MyCritic->new( + -profile => "$profile", + '-cacheable-filename' => "$cache" + ); + + is( + [$critic->critique("$perl_source[0]")], + [], + "call \$critic->critique(\"$perl_source[0]\") = []", + ); + + @faux_violations = ('a','b','x'); + + is( + [$critic->critique("$perl_source[1]")], + ['a','b','x'], + "call \$critic->critique(\"$perl_source[1]\") = [a,b,x]", + ); + + is $call_count, 2, 'expected call count'; + $critic->cacheable_save; + + ok -r $cache, 'created cache file'; + }; + + subtest 'second run (with cache)' => sub { + my $critic = MyCritic->new( + -profile => "$profile", + '-cacheable-filename' => "$cache" + ); + + is( + [$critic->critique("$perl_source[0]")], + [], + "call \$critic->critique(\"$perl_source[0]\") = []", + ); + + @faux_violations = ('a','b','x'); + + is( + [$critic->critique("$perl_source[1]")], + ['a','b','x'], + "call \$critic->critique(\"$perl_source[1]\") = [a,b,x]", + ); + + is $call_count, 1, 'expected call count'; + }; + + subtest 'third run with change to source file' => sub { + $perl_source[0]->spew('baz'); + my $critic = MyCritic->new( + -profile => "$profile", + '-cacheable-filename' => "$cache" + ); + is( + [$critic->critique("$perl_source[0]")], + [], + "call \$critic->critique(\"$perl_source[0]\") = []", + ); + + @faux_violations = ('a','b','x'); + + is( + [$critic->critique("$perl_source[1]")], + ['a','b','x'], + "call \$critic->critique(\"$perl_source[1]\") = [a,b,x]", + ); + + is $call_count, 2, 'expected call count'; + }; + + subtest 'forth run with change to profile' => sub { + $perl_source[0]->spew('foo'); + $profile->spew('; just a comment'); + + my $critic = MyCritic->new( + -profile => "$profile", + '-cacheable-filename' => "$cache" + ); + + is( + [$critic->critique("$perl_source[0]")], + [], + "call \$critic->critique(\"$perl_source[0]\") = []", + ); + + @faux_violations = ('a','b','x'); + + is( + [$critic->critique("$perl_source[1]")], + ['a','b','x'], + "call \$critic->critique(\"$perl_source[1]\") = [a,b,x]", + ); + + is $call_count, 2, 'expected call count'; + }; + + subtest 'forth run with change to arguments' => sub { + my $profile = $root->child('profile2'); + $profile->spew(''); + + my $critic = MyCritic->new( + -profile => "$profile", + '-cacheable-filename' => "$cache" + ); + + is( + [$critic->critique("$perl_source[0]")], + [], + "call \$critic->critique(\"$perl_source[0]\") = []", + ); + + @faux_violations = ('a','b','x'); + + is( + [$critic->critique("$perl_source[1]")], + ['a','b','x'], + "call \$critic->critique(\"$perl_source[1]\") = [a,b,x]", + ); + + is $call_count, 2, 'expected call count'; + }; + +}; + +done_testing;