Skip to content
This repository
Browse code

This implements #241.

Added a signal handler spec which may not be correct.
  • Loading branch information...
commit 5c60354b2c645ea290b107df0f681d333ffe93fd 1 parent 43b8733
authored March 05, 2012
12  README.md
Source Rendered
@@ -339,6 +339,18 @@ $ guard start -I
339 339
 $ guard start --no-vendor
340 340
 ```
341 341
 
  342
+### Pause watching (all but Windows)
  343
+
  344
+```bash
  345
+$ kill -USR1 <guard_pid>
  346
+```
  347
+
  348
+### Continue watching (all but Windows)
  349
+
  350
+```bash
  351
+$ kill -USR2 <guard_pid>
  352
+```
  353
+
342 354
 ### List
343 355
 
344 356
 You can list the available Guards with the `list` task:
14  lib/guard/listener.rb
@@ -82,6 +82,7 @@ def start_reactor
82 82
       return if ENV["GUARD_ENV"] == 'test'
83 83
 
84 84
       Thread.new do
  85
+        add_signal_handlers unless self.windows?
85 86
         loop do
86 87
           if @changed_files != [] && !@paused
87 88
             changed_files = @changed_files.dup
@@ -138,6 +139,19 @@ def update_last_event
138 139
       @last_event = Time.now
139 140
     end
140 141
 
  142
+    # Add signals handlers (works only on posix-compatible systems).
  143
+    # (Yes, they're not atomic.  Be nice to them. :)
  144
+    def add_signal_handlers
  145
+      Signal.trap("USR1") do
  146
+        UI.info "Paused Guard on signal USR1" unless @paused
  147
+        pause
  148
+      end
  149
+      Signal.trap("USR2") do
  150
+        UI.info "Continued Guard on signal USR2" if @paused
  151
+        run
  152
+      end
  153
+    end
  154
+
141 155
     # Get the modified files.
142 156
     #
143 157
     # If the `:watch_all_modifications` option is true, then moved and
197  spec/guard/listeners/signals_spec.rb
... ...
@@ -0,0 +1,197 @@
  1
+require 'spec_helper'
  2
+require 'guard/listener'
  3
+
  4
+describe Guard::Listener do
  5
+
  6
+  if windows? 
  7
+    STDERR.puts "ERROR guard listener signal testing not run because windows"
  8
+  else # ! windows
  9
+
  10
+    describe 'when #initialize a new Listener' do
  11
+      let(:guard) { Guard::Listener.new }
  12
+      let(:ui) { Guard::UI }
  13
+
  14
+      before { ENV['GUARD_ENV'] = 'test_signals' }
  15
+      after { ENV['GUARD_ENV'] = 'test' if ENV['GUARD_ENV'] == 'test_signals' }
  16
+
  17
+      # ---- USR* signals while running
  18
+
  19
+      context 'on an USR1 signal' do
  20
+        before { Process.kill :USR1, Process.pid }
  21
+
  22
+        it '#pause once' do
  23
+          guard.should_receive(:pause).once
  24
+          guard.should_not_receive(:run).any_number_of_times
  25
+
  26
+          ui.should_receive(:info).with("Paused Guard on signal USR1")
  27
+          ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  28
+        end
  29
+      end
  30
+
  31
+      context 'on an USR2 signal' do
  32
+        before { Process.kill :USR2, Process.pid }
  33
+
  34
+        it 'does nothing' do
  35
+          guard.should_not_receive(:pause).any_number_of_times
  36
+          guard.should_not_receive(:run).any_number_of_times
  37
+
  38
+          ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  39
+          ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  40
+        end
  41
+      end
  42
+
  43
+      context 'on duplicate USR1 signals' do
  44
+        before do
  45
+          Process.kill :USR1, Process.pid
  46
+          Process.kill :USR1, Process.pid
  47
+        end
  48
+
  49
+        it '#pause once' do
  50
+          guard.should_receive(:pause).once
  51
+          guard.should_receive(:run).any_number_of_times
  52
+
  53
+          ui.should_receive(:info).with("Paused Guard on signal USR1")
  54
+          ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  55
+        end
  56
+      end
  57
+
  58
+      context 'on duplicate USR2 signals' do
  59
+        before do
  60
+          Process.kill :USR2, Process.pid
  61
+          Process.kill :USR2, Process.pid
  62
+        end
  63
+
  64
+        it 'does nothing' do
  65
+          guard.should_not_receive(:pause).any_number_of_times
  66
+          guard.should_not_receive(:run).any_number_of_times
  67
+
  68
+          ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  69
+          ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  70
+        end
  71
+      end
  72
+
  73
+      context 'on an USR1 and then USR2 signal' do
  74
+        before do
  75
+          Process.kill :USR1, Process.pid
  76
+          Process.kill :USR2, Process.pid
  77
+        end
  78
+
  79
+        it '#pause and then #run' do
  80
+          guard.should_receive(:pause).once
  81
+          guard.should_receive(:run).once
  82
+
  83
+          ui.should_receive(:info).with("Paused Guard on signal USR1")
  84
+          ui.should_receive(:info).with("Continued Guard on signal USR2")
  85
+        end
  86
+      end
  87
+
  88
+      context 'on an USR2 and then USR1 signal' do
  89
+        before do
  90
+          Process.kill :USR2, Process.pid
  91
+          Process.kill :USR1, Process.pid
  92
+        end
  93
+
  94
+        it '#run once' do
  95
+          guard.should_receive(:pause).once
  96
+          guard.should_not_receive(:run).any_number_of_times
  97
+
  98
+          ui.should_receive(:info).with("Paused Guard on signal USR1")
  99
+          ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  100
+        end
  101
+      end
  102
+
  103
+     
  104
+      # ---- USR* signals while started and then paused
  105
+
  106
+      context 'when #pause' do
  107
+        before { guard.stub(:pause) }
  108
+
  109
+        context 'on an USR1 signal' do
  110
+          before { Process.kill :USR1, Process.pid }
  111
+
  112
+          it 'does nothing' do
  113
+            guard.should_not_receive(:pause).any_number_of_times
  114
+            guard.should_not_receive(:run).any_number_of_times
  115
+
  116
+            ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  117
+            ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  118
+          end
  119
+        end
  120
+
  121
+        context 'on an USR2 signal' do
  122
+          before { Process.kill :USR2, Process.pid }
  123
+
  124
+          it '#pause once' do
  125
+            guard.should_not_receive(:pause).any_number_of_times
  126
+            guard.should_receive(:run).once
  127
+
  128
+            ui.should_receive(:info).with("Paused Guard on signal USR1")
  129
+            ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  130
+            ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  131
+          end
  132
+        end
  133
+
  134
+        context 'on duplicate USR1 signals' do
  135
+          before do
  136
+            Process.kill :USR1, Process.pid
  137
+            Process.kill :USR1, Process.pid
  138
+          end
  139
+
  140
+          it 'does nothing' do
  141
+            guard.should_not_receive(:pause).any_number_of_times
  142
+            guard.should_not_receive(:run).any_number_of_times
  143
+
  144
+            ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  145
+            ui.should_not_receive(:info).with("Continued Guard on signal USR2")
  146
+          end
  147
+        end
  148
+
  149
+        context 'on duplicate USR2 signals' do
  150
+          before do
  151
+            Process.kill :USR2, Process.pid
  152
+            Process.kill :USR2, Process.pid
  153
+          end
  154
+
  155
+          it '#run once' do
  156
+            guard.should_not_receive(:pause).any_number_of_times
  157
+            guard.should_receive(:run).once
  158
+
  159
+            ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  160
+            ui.should_receive(:info).with("Continued Guard on signal USR2")
  161
+          end
  162
+        end
  163
+
  164
+        context 'on an USR1 and then USR2 signal' do
  165
+          before do
  166
+            Process.kill :USR1, Process.pid
  167
+            Process.kill :USR2, Process.pid
  168
+          end
  169
+
  170
+          it '#run once' do
  171
+            guard.should_not_receive(:pause).any_number_of_times
  172
+            guard.should_receive(:run).once
  173
+
  174
+            ui.should_not_receive(:info).with("Paused Guard on signal USR1")
  175
+            ui.should_receive(:info).with("Continued Guard on signal USR2")
  176
+          end
  177
+        end
  178
+
  179
+        context 'on an USR2 and then USR1 signal' do
  180
+          before do
  181
+            Process.kill :USR2, Process.pid
  182
+            Process.kill :USR1, Process.pid
  183
+          end
  184
+
  185
+          it '#run and then #pause' do
  186
+            guard.should_not_receive(:pause).once
  187
+            guard.should_receive(:run).once
  188
+
  189
+            ui.should_receive(:info).with("Paused Guard on signal USR1")
  190
+            ui.should_receive(:info).with("Continued Guard on signal USR2")
  191
+          end
  192
+        end
  193
+      end # when #pause
  194
+
  195
+    end # describe when #start
  196
+  end # !windows
  197
+end # describe Guard::Listener

0 notes on commit 5c60354

Please sign in to comment.
Something went wrong with that request. Please try again.