Skip to content

Commit

Permalink
triggers modified_files on file deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Hernan Lopes committed Feb 5, 2024
1 parent 7a1cd71 commit 1fdfb69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/Mojo/Server/Morbo/Backend/Poll.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ use Mojo::File qw(path);
sub modified_files {
my $self = shift;

my $cache = $self->{cache} //= {};
my $cache = $self->{cache} //= {};
my $known_files = {map { $_ => 1 } keys %$cache};
my @files;
for my $file (map { -f $_ && -r _ ? $_ : _list($_) } @{$self->watch}) {
delete $known_files->{$file};
my ($size, $mtime) = (stat $file)[7, 9];
next unless defined $size and defined $mtime;
my $stats = $cache->{$file} ||= [$^T, $size];
next if $mtime <= $stats->[0] && $size == $stats->[1];
@$stats = ($mtime, $size);
push @files, $file;
}

for my $file (keys %$known_files) {

# any leftover file means they were deleted
push @files, $file;
delete $cache->{$file};
}

sleep $self->watch_timeout unless @files;

return \@files;
Expand Down
13 changes: 13 additions & 0 deletions t/mojo/morbo.t
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ EOF
is $tx->res->body, 'Hello!', 'right content';
};

subtest 'Watch for deleted files' => sub {
my $morbo = Mojo::Server::Morbo->new;
$morbo->backend->watch([$dir]);
$morbo->backend->modified_files;
my $tmp_file = $dir->child('tmp_file.txt');
$tmp_file->spew("some data");
is_deeply $morbo->backend->modified_files, [$tmp_file], 'file was created';
is_deeply $morbo->backend->modified_files, [], 'no files changed';
unlink $tmp_file;
is_deeply $morbo->backend->modified_files, [$tmp_file], 'file was deleted';
is_deeply $morbo->backend->modified_files, [], 'no files changed';
};

subtest 'New file(s)' => sub {
is_deeply $morbo->backend->modified_files, [], 'directory has not changed';
my @new = map { $subdir->child("$_.txt") } qw/test testing/;
Expand Down

0 comments on commit 1fdfb69

Please sign in to comment.