[Rt-commit] r2900 - in Module-Refresh: lib/Module t
glasser at bestpractical.com
glasser at bestpractical.com
Fri May 20 19:06:39 EDT 2005
Author: glasser
Date: Fri May 20 19:06:38 2005
New Revision: 2900
Added:
Module-Refresh/t/2lateuse.t
- copied, changed from r1801, Module-Refresh/t/1api.t
Modified:
Module-Refresh/lib/Module/Refresh.pm
Module-Refresh/t/1api.t
Log:
Trade overrefreshing for underrefreshing in some cases; test and document
Modified: Module-Refresh/lib/Module/Refresh.pm
==============================================================================
--- Module-Refresh/lib/Module/Refresh.pm (original)
+++ Module-Refresh/lib/Module/Refresh.pm Fri May 20 19:06:38 2005
@@ -41,7 +41,7 @@
sub new {
my $proto = shift;
my $self = ref($proto) || $proto;
- $CACHE{$_} = $self->mtime($INC{$_}) for ( keys %INC );
+ $self->update_cache($_) for keys %INC;
return ($self);
};
@@ -50,6 +50,13 @@
Refresh all modules that have mtimes on disk newer than the newest ones we've got.
Calls C<new> to initialize the cache if it had not yet been called.
+Specifically, it will renew any module that was loaded before the previous call
+to C<refresh> (or C<new>) and has changed on disk since then. If a module was
+both loaded for the first time B<and> changed on disk between the previous call
+and this one, it will B<not> be reloaded by this call (or any future one); you
+will need to update the modification time again (by using the Unix C<touch> command or
+making a change to it) in order for it to be reloaded.
+
=cut
sub refresh {
@@ -58,7 +65,9 @@
return $self->new if !%CACHE;
foreach my $mod (sort keys %INC) {
- if ( !$CACHE{$mod} or ( $self->mtime($INC{$mod}) ne $CACHE{$mod} ) ) {
+ if ( !$CACHE{$mod} ) {
+ $self->update_cache($mod);
+ } elsif ( $self->mtime($INC{$mod}) ne $CACHE{$mod} ) {
$self->refresh_module($mod);
}
}
@@ -82,7 +91,7 @@
local $@;
eval { require $mod; 1 } or warn $@;
- $CACHE{$mod} = $self->mtime( $INC{$mod} );
+ $self->update_cache($mod);
return ($self);
};
@@ -115,6 +124,19 @@
return join ' ', ( stat($_[1]) )[1, 7, 9];
};
+=head2 update_cache $file
+
+Updates the cached "last modified" time for $file.
+
+=cut
+
+sub update_cache {
+ my $self = shift;
+ my $module_pm = shift;
+
+ $CACHE{$module_pm} = $self->mtime($INC{$module_pm});
+};
+
=head2 unload_subs $file
Wipe out subs defined in $file.
Modified: Module-Refresh/t/1api.t
==============================================================================
--- Module-Refresh/t/1api.t (original)
+++ Module-Refresh/t/1api.t Fri May 20 19:06:38 2005
@@ -17,15 +17,14 @@
use_ok('Module::Refresh');
-my $r = Module::Refresh->new();
-
use_ok('FooBar', "Required our dummy module");
+my $r = Module::Refresh->new();
+
# is our non-file-based method available?
can_ok('Foo::Bar', 'not_in_foobarpm');
-
is(Foo::Bar->foo, 'bar', "We got the right result");
write_out(<<".");
Copied: Module-Refresh/t/2lateuse.t (from r1801, Module-Refresh/t/1api.t)
==============================================================================
--- Module-Refresh/t/1api.t (original)
+++ Module-Refresh/t/2lateuse.t Fri May 20 19:06:38 2005
@@ -2,53 +2,34 @@
use strict;
-use Test::More qw/no_plan/;
+package FOOBAR;
+
+use Test::More qw/no_plan/;
+# can't really have a plan, since we're hoping the test will not be called
use File::Spec;
my $tmp = File::Spec->tmpdir;
-my $file = $tmp."/".'FooBar.pm';
+my $file = $tmp."/".'BeepBeep.pm';
push @INC, $tmp;
-write_out(<<".");
-package Foo::Bar;
-sub foo { 'bar' }
+write_out(<<'.');
+package BeepBeep;
+our $Beep;
+Test::More::is($Beep++, 0, "This line works exactly once!");
1;
.
use_ok('Module::Refresh');
-my $r = Module::Refresh->new();
-
-use_ok('FooBar', "Required our dummy module");
-
-# is our non-file-based method available?
-
-can_ok('Foo::Bar', 'not_in_foobarpm');
+Module::Refresh->refresh; # This does the first INC scan
+use_ok('BeepBeep', "Required our dummy module");
-is(Foo::Bar->foo, 'bar', "We got the right result");
-
-write_out(<<".");
-package Foo::Bar;
-sub foo { 'baz' }
-1;
-.
-
-is(Foo::Bar->foo, 'bar', "We got the right result, still");
+Module::Refresh->refresh;
-$r->refresh;
+# BeepBeep should *not* be refreshed here, because it has not
+# changed, even if M::R has not seen it before!
-is(Foo::Bar->foo, 'baz', "We got the right new result,");
-
-# After a refresh, did we blow away our non-file-based comp?
-can_ok('Foo::Bar', 'not_in_foobarpm');
-
-$r->unload_subs($file);
-ok(!defined(&Foo::Bar::foo), "We cleaned out the 'foo' method'");
-
-#ok(!UNIVERSAL::can('Foo::Bar', 'foo'), "We cleaned out the 'foo' method'");
-#require "FooBar.pm";
-#is(Foo::Bar->foo, 'baz', "We got the right new result,");
sub write_out {
local *FH;
@@ -57,11 +38,4 @@
close FH;
}
-
-package Foo::Bar;
-
-sub not_in_foobarpm {
- return "woo";
-}
-
1;
More information about the Rt-commit
mailing list