File: unit_test.rb

package info (click to toggle)
ruby-spring-watcher-listen 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124 kB
  • sloc: ruby: 144; makefile: 3
file content (76 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download
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
require "helper"
require "spring/watcher/listen"

class ListenWatcherTest < Spring::Test::WatcherTest
  def watcher_class
    Spring::Watcher::Listen
  end

  # this test, as currently written, can only run against polling implementations
  undef :test_add_directory_with_dangling_symlink

  setup do
    Celluloid.boot if defined?(Celluloid)
  end

  teardown { Listen.stop }

  test "root directories" do
    begin
      other_dir_1 = File.realpath(Dir.mktmpdir)
      other_dir_2 = File.realpath(Dir.mktmpdir)
      File.write("#{other_dir_1}/foo", "foo")
      File.write("#{dir}/foo", "foo")

      watcher.add "#{other_dir_1}/foo"
      watcher.add other_dir_2
      watcher.add "#{dir}/foo"

      dirs = [dir, other_dir_1, other_dir_2].sort.map { |path| Pathname.new(path) }
      assert_equal dirs, watcher.base_directories.sort
    ensure
      FileUtils.rm_rf other_dir_1
      FileUtils.rm_rf other_dir_2
    end
  end

  test "root directories with a root subpath directory" do
    begin
      other_dir_1 = "#{dir}_other"
      other_dir_2 = "#{dir}_core"
      # same subpath as dir but with _other or _core appended
      FileUtils::mkdir_p(other_dir_1)
      FileUtils::mkdir_p(other_dir_2)
      File.write("#{other_dir_1}/foo", "foo")
      File.write("#{other_dir_2}/foo", "foo")
      File.write("#{dir}/foo", "foo")

      watcher.add "#{other_dir_1}/foo"
      watcher.add other_dir_2

      dirs = [dir, other_dir_1, other_dir_2].sort.map { |path| Pathname.new(path) }
      assert_equal dirs, watcher.base_directories.sort
    ensure
      FileUtils.rm_rf other_dir_1
      FileUtils.rm_rf other_dir_2
    end
  end

  test "stops listening when already stale" do
    # Track when we're marked as stale.
    on_stale_count = 0
    watcher.on_stale { on_stale_count += 1 }

    # Add a file to watch and start listening.
    file = "#{@dir}/omg"
    touch file, Time.now - 2.seconds
    watcher.add file
    watcher.start
    assert watcher.running?

    # Touch bumps mtime and marks as stale which stops listener.
    touch file, Time.now - 1.second
    Timeout.timeout(1) { sleep 0.1 while watcher.running? }
    assert_equal 1, on_stale_count
  end
end