From 632ae2786d5f676664a83016cdfe8f5decc0832b Mon Sep 17 00:00:00 2001 From: Hernan Lopes Date: Tue, 6 Feb 2024 19:27:58 -0300 Subject: [PATCH] triggers modified_files on file deletion --- lib/Mojo/Server/Morbo/Backend/Poll.pm | 12 +++++++++++- t/mojo/morbo.t | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/Mojo/Server/Morbo/Backend/Poll.pm b/lib/Mojo/Server/Morbo/Backend/Poll.pm index 1230dc1de9..901ee04460 100644 --- a/lib/Mojo/Server/Morbo/Backend/Poll.pm +++ b/lib/Mojo/Server/Morbo/Backend/Poll.pm @@ -6,9 +6,11 @@ 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]; @@ -16,6 +18,14 @@ sub modified_files { @$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; diff --git a/t/mojo/morbo.t b/t/mojo/morbo.t index 0eeabcb580..83340de49f 100644 --- a/t/mojo/morbo.t +++ b/t/mojo/morbo.t @@ -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/;