Skip to content

Loading…

Fix linux support #7

Closed
wants to merge 5 commits into from

2 participants

@yannlugrin
Guard member

Linux support with inotify don't work fine because loop more one time on same event. Apply this patch to fix this problem.

@thibaudgg
Guard member

Patch applied, thx again. btw you can now directly push to guard repository :-)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 44 additions and 7 deletions.
  1. +12 −4 lib/guard/listeners/linux.rb
  2. +32 −3 spec/guard/listeners/linux_spec.rb
View
16 lib/guard/listeners/linux.rb
@@ -3,6 +3,8 @@ class Linux < Listener
attr_reader :inotify, :files, :latency, :callback
def initialize
+ super
+
@inotify = INotify::Notifier.new
@files = []
@latency = 0.5
@@ -10,7 +12,7 @@ def initialize
def on_change(&callback)
@callback = callback
- inotify.watch(Dir.pwd, :recursive, :attrib, :modify, :create) do |event|
+ inotify.watch(Dir.pwd, :recursive, :modify, :create, :delete, :move) do |event|
unless event.name == "" # Event on root directory
@files << event.absolute_name
end
@@ -20,12 +22,12 @@ def on_change(&callback)
def start
@stop = false
- watch_change
+ watch_change unless @watch_change
end
def stop
@stop = true
- inotify.stop
+ sleep latency
end
def self.usable?
@@ -44,18 +46,24 @@ def self.usable?
private
def watch_change
+ @watch_change = true
while !@stop
if Config::CONFIG['build'] =~ /java/ || IO.select([inotify.to_io], [], [], latency)
+ break if @stop
+
inotify.process
+ update_last_event
unless files.empty?
files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
callback.call(files)
files.clear
end
- sleep latency
+
+ sleep latency unless @stop
end
end
+ @watch_change = false
end
end
View
35 spec/guard/listeners/linux_spec.rb
@@ -14,6 +14,25 @@
it "should be usable on linux" do
subject.should be_usable
end
+
+ describe "start" do
+ before(:each) do
+ @listener = Guard::Linux.new
+ end
+
+ it "call watch_change" do
+ @listener.should_receive(:watch_change)
+ start
+ end
+
+ it "don't call watch_change if re start after stop" do
+ start
+ stop
+ @listener.should_not_receive(:watch_change)
+ start
+ end
+
+ end
describe "watch" do
before(:each) do
@@ -38,7 +57,7 @@
file = @fixture_path.join("folder1/file1.txt")
File.exists?(file).should be_true
start
- FileUtils.touch file
+ File.open(file, 'w') {|f| f.write('') }
stop
@results.should == ['spec/fixtures/folder1/file1.txt']
end
@@ -49,11 +68,20 @@
File.exists?(file1).should be_true
File.exists?(file2).should be_true
start
- FileUtils.touch file1
- FileUtils.touch file2
+ File.open(file1, 'w') {|f| f.write('') }
+ File.open(file2, 'w') {|f| f.write('') }
stop
@results.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
end
+
+ it "should not process change if stopped" do
+ file = @fixture_path.join("folder1/file1.txt")
+ File.exists?(file).should be_true
+ start
+ @listener.inotify.should_not_receive(:process)
+ stop
+ File.open(file, 'w') {|f| f.write('') }
+ end
end
end
@@ -68,6 +96,7 @@ def start
def stop
sleep 1
@listener.stop
+ sleep 1
end
end
Something went wrong with that request. Please try again.