1 require './test/helper'
6 require 'sleepy_penguin'
8 class TestInotify < Testcase
13 ObjectSpace.each_object(Inotify) { |io| io.close unless io.closed? }
14 ObjectSpace.each_object(Tempfile) { |io| io.close unless io.closed? }
19 assert_kind_of(IO, ino)
24 (Inotify.constants - IO.constants).each do |const|
26 when :Event, :Enumerator
28 nr = Inotify.const_get(const)
29 assert nr <= 0xffffffff, "#{const}=#{nr}"
35 ino = Inotify.new Inotify::NONBLOCK
36 flags = ino.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
37 assert_equal(Fcntl::O_NONBLOCK, flags)
41 ino = Inotify.new Inotify::CLOEXEC
42 flags = ino.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
43 assert_equal(Fcntl::FD_CLOEXEC, flags)
47 ino = Inotify.new Inotify::CLOEXEC
48 tmp1 = Tempfile.new 'take'
49 tmp2 = Tempfile.new 'take'
50 wd = ino.add_watch File.dirname(tmp1.path), Inotify::MOVE
51 assert_kind_of Integer, wd
52 File.rename tmp1.path, tmp2.path
54 assert_equal wd, event.wd
55 assert_kind_of Inotify::Event, event
56 assert_equal File.basename(tmp1.path), event.name
57 others = ino.instance_variable_get(:@inotify_tmp)
58 assert_kind_of Array, others
59 assert_equal 1, others.size
60 assert_equal File.basename(tmp2.path), others[0].name
61 assert_equal [ :MOVED_FROM ], event.events
62 assert_equal [ :MOVED_TO ], others[0].events
63 assert_equal wd, others[0].wd
64 second_id = others[0].object_id
65 assert_equal second_id, ino.take.object_id
66 assert_nil ino.take(true)
69 def test_add_take_symbols
70 ino = Inotify.new :CLOEXEC
71 tmp1 = Tempfile.new 'take'
72 tmp2 = Tempfile.new 'take'
73 wd = ino.add_watch File.dirname(tmp1.path), :MOVE
74 assert_kind_of Integer, wd
75 File.rename tmp1.path, tmp2.path
77 assert_equal wd, event.wd
78 assert_kind_of Inotify::Event, event
79 assert_equal File.basename(tmp1.path), event.name
80 others = ino.instance_variable_get(:@inotify_tmp)
81 assert_kind_of Array, others
82 assert_equal 1, others.size
83 assert_equal File.basename(tmp2.path), others[0].name
84 assert_equal [ :MOVED_FROM ], event.events
85 assert_equal [ :MOVED_TO ], others[0].events
86 assert_equal wd, others[0].wd
87 second_id = others[0].object_id
88 assert_equal second_id, ino.take.object_id
89 assert_nil ino.take(true)
93 ino = Inotify.new :CLOEXEC
94 tmp1 = Tempfile.new 'take'
95 wd = ino.add_watch tmp1.path, :OPEN
96 assert_kind_of Integer, wd
98 o = File.open(tmp1.path)
100 assert_equal [:OPEN], event.events
101 break if (nr -= 1) == 0
102 o = File.open(tmp1.path)
108 ino = Inotify.new Inotify::CLOEXEC
109 tmp = Tempfile.new 'a'
110 wd = ino.add_watch tmp.path, Inotify::ALL_EVENTS
111 assert_kind_of Integer, wd
114 assert_equal wd, event.wd
115 assert_kind_of Inotify::Event, event
116 assert_equal [:MODIFY], event.events
117 assert_equal 0, ino.rm_watch(wd)
119 end if defined?(SleepyPenguin::Inotify)