aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/file_watcher_test.rb
blob: 7b4d4be24fb4d2faaf4d1b0f623c9b9fdd55f0f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'abstract_unit'
require 'fssm'
require "fileutils"
require "timeout"


class FileWatcherTest < ActiveSupport::TestCase
  class DumbBackend < ActiveSupport::FileWatcher::Backend
  end

  def setup
    @watcher = ActiveSupport::FileWatcher.new

    # In real life, the backend would take the path and use it to observe the file
    # system. In our case, we will manually trigger the events for unit testing,
    # so we can pass any path.
    @backend = DumbBackend.new("RAILS_WOOT", @watcher)

    @payload = []
    @watcher.watch %r{^app/assets/.*\.scss$} do |pay|
      pay.each do |status, files|
        files.sort!
      end
      @payload << pay
    end
  end

  def test_use_triple_equals
    fw = ActiveSupport::FileWatcher.new
    called = []
    fw.watch("some_arbitrary_file.rb") do |file|
      called << "omg"
    end
    fw.trigger(%w{ some_arbitrary_file.rb })
    assert_equal ['omg'], called
  end

  def test_one_change
    @backend.trigger("app/assets/main.scss" => :changed)
    assert_equal({:changed => ["app/assets/main.scss"]}, @payload.first)
  end

  def test_multiple_changes
    @backend.trigger("app/assets/main.scss" => :changed, "app/assets/javascripts/foo.coffee" => :changed)
    assert_equal([{:changed => ["app/assets/main.scss"]}], @payload)
  end

  def test_multiple_changes_match
    @backend.trigger("app/assets/main.scss" => :changed, "app/assets/print.scss" => :changed, "app/assets/javascripts/foo.coffee" => :changed)
    assert_equal([{:changed => ["app/assets/main.scss", "app/assets/print.scss"]}], @payload)
  end

  def test_multiple_state_changes
    @backend.trigger("app/assets/main.scss" => :created, "app/assets/print.scss" => :changed)
    assert_equal([{:changed => ["app/assets/print.scss"], :created => ["app/assets/main.scss"]}], @payload)
  end

  def test_more_blocks
    payload = []
    @watcher.watch %r{^config/routes\.rb$} do |pay|
      payload << pay
    end

    @backend.trigger "config/routes.rb" => :changed
    assert_equal [:changed => ["config/routes.rb"]], payload
    assert_equal [], @payload
  end

  def test_overlapping_watchers
    payload = []
    @watcher.watch %r{^app/assets/main\.scss$} do |pay|
      payload << pay
    end

    @backend.trigger "app/assets/print.scss" => :changed, "app/assets/main.scss" => :changed
    assert_equal [:changed => ["app/assets/main.scss"]], payload
    assert_equal [:changed => ["app/assets/main.scss", "app/assets/print.scss"]], @payload
  end
end

module FSSM::Backends
  class Polling
    def initialize_with_low_latency(options={})
      initialize_without_low_latency(options.merge(:latency => 0.1))
    end
    alias_method_chain :initialize, :low_latency
  end
end

class FSSMFileWatcherTest < ActiveSupport::TestCase
  class FSSMBackend < ActiveSupport::FileWatcher::Backend
    def initialize(path, watcher)
      super

      monitor = FSSM::Monitor.new
      monitor.path(path, '**/*') do |p|
        p.update { |base, relative| trigger relative => :changed }
        p.delete { |base, relative| trigger relative => :deleted }
        p.create { |base, relative| trigger relative => :created }
      end

      @thread = Thread.new do
        monitor.run
      end
    end

    def stop
      @thread.kill
    end
  end

  def setup
    Thread.abort_on_exception = true

    @payload = []
    @triggered = false

    @watcher = ActiveSupport::FileWatcher.new

    @path = path = File.expand_path("../tmp", __FILE__)
    FileUtils.rm_rf path

    create "app/assets/main.scss", true
    create "app/assets/javascripts/foo.coffee", true
    create "app/assets/print.scss", true
    create "app/assets/videos.scss", true

    @backend = FSSMBackend.new(path, @watcher)

    @watcher.watch %r{^app/assets/.*\.scss$} do |pay|
      pay.each do |status, files|
        files.sort!
      end
      @payload << pay
      trigger
    end
  end

  def teardown
    @backend.stop
    Thread.abort_on_exception = false
  end

  def create(path, past = false)
    wait(past) do
      path = File.join(@path, path)
      FileUtils.mkdir_p(File.dirname(path))

      FileUtils.touch(path)
      File.utime(Time.now - 100, Time.now - 100, path) if past
    end
  end

  def change(path)
    wait do
      FileUtils.touch(File.join(@path, path))
    end
  end

  def delete(path)
    wait do
      FileUtils.rm(File.join(@path, path))
    end
  end

  def wait(past = false)
    yield
    return if past

    begin
      Timeout.timeout(1) do
        sleep 0.05 until @triggered
      end
    rescue Timeout::Error
    end

    @triggered = false
  end

  def trigger
    @triggered = true
  end

  def test_one_change
    change "app/assets/main.scss"
    assert_equal({:changed => ["app/assets/main.scss"]}, @payload.first)
  end

  def test_multiple_changes
    change "app/assets/main.scss"
    change "app/assets/javascripts/foo.coffee"
    assert_equal([{:changed => ["app/assets/main.scss"]}], @payload)
  end

  def test_multiple_changes_match
    change "app/assets/main.scss"
    change "app/assets/print.scss"
    change "app/assets/javascripts/foo.coffee"
    assert_equal([{:changed => ["app/assets/main.scss"]}, {:changed => ["app/assets/print.scss"]}], @payload)
  end

  def test_multiple_state_changes
    create "app/assets/new.scss"
    change "app/assets/print.scss"
    delete "app/assets/videos.scss"
    assert_equal([{:created => ["app/assets/new.scss"]}, {:changed => ["app/assets/print.scss"]}, {:deleted => ["app/assets/videos.scss"]}], @payload)
  end

  def test_more_blocks
    payload = []
    @watcher.watch %r{^config/routes\.rb$} do |pay|
      payload << pay
      trigger
    end

    create "config/routes.rb"
    assert_equal [{:created => ["config/routes.rb"]}], payload
    assert_equal [], @payload
  end

  def test_overlapping_watchers
    payload = []
    @watcher.watch %r{^app/assets/main\.scss$} do |pay|
      payload << pay
      trigger
    end

    change "app/assets/main.scss"
    change "app/assets/print.scss"
    assert_equal [{:changed => ["app/assets/main.scss"]}], payload
    assert_equal [{:changed => ["app/assets/main.scss"]}, {:changed => ["app/assets/print.scss"]}], @payload
  end
end