guard / listen

The Listen gem listens to file modifications and notifies you about the changes.
https://rubygems.org/gems/listen
MIT License
1.92k stars 245 forks source link

issue #450: linux driver: add :modify to default events listened to #502

Closed ColinDKelley closed 3 years ago

ColinDKelley commented 3 years ago

Fix for issue #450 .

ColinDKelley commented 3 years ago

@ms-ati brought up the risk that adding :modify might lead to duplicate events.

That PR is all using stubs, so it doesn't disprove the possibility of duplicate events. I can try running an interactive test to see if that happens.

ColinDKelley commented 3 years ago

@ms-ati I ran this test on a Linux pod. It worked perfectly--no dups!


# cat listen-test.rb
require 'listen'

target = ARGV[0] or raise "no target given"
puts "listening to #{target}"

Listen.to(target) do |*args|
    puts Hash[[:modified, :added, :removed].zip(args)].inspect
end.start

puts "sleeping..."
sleep

# bundle exec ruby listen-test.rb . &
[1] 489
listening to .
sleeping...

# touch a.dat
{:modified=>[], :added=>["/srv/app/listen-test/a.dat"], :removed=>[]}
# rm a.dat
{:modified=>[], :added=>[], :removed=>["/srv/app/listen-test/a.dat"]}
# touch b.dat
{:modified=>[], :added=>["/srv/app/listen-test/b.dat"], :removed=>[]}
# echo more >> b.dat
{:modified=>["/srv/app/listen-test/b.dat"], :added=>[], :removed=>[]}
# mv b.dat c.dat
{:modified=>[], :added=>["/srv/app/listen-test/c.dat"], :removed=>["/srv/app/listen-test/b.dat"]}
# kill %1
ms-ati commented 3 years ago

@ColinDKelley Awesome! Could you try editing a file and saving it, as opposed to running touch, as well?

ms-ati commented 3 years ago

(that is -- not simply appending -- but editing and saving)

ColinDKelley commented 3 years ago

@ms-ati I ran that test on a Linux pod and it worked exactly as expected. I tried both editing an existing file, and a new one.

# bundle exec ruby listen-test.rb . &
[1] 151
listening to .
sleeping...

# touch a.txt
{:modified=>[], :added=>["/srv/app/listen-test/a.txt"], :removed=>[]}

# vi a.txt
(added a line and pressed ZZ to save and exit)
{:modified=>["/srv/app/listen-test/a.txt"], :added=>[], :removed=>[]}

# vi b.txt
(added a line and pressed ZZ to save and exit)
{:modified=>[], :added=>["/srv/app/listen-test/b.txt"], :removed=>[]}

# rm a.txt b.txt
{:modified=>[], :added=>[], :removed=>["/srv/app/listen-test/a.txt", "/srv/app/listen-test/b.txt"]}
ColinDKelley commented 3 years ago

BTW, I've added a variant of the above tests to the README since they seem helpful to understand how it works overall. https://github.com/guard/listen/pull/503

ms-ati commented 3 years ago

cc @ioquatix looks like this PR is pending your formal review in Github?

ColinDKelley commented 3 years ago

I'm going to merge and release as v3.3.0.pre.3 so we can get some burn-in time. I'm still interested in your review though, @ioquatix!