tekkub-watchr 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ require 'test/test_helper'
2
+
3
+ class UnixEventHandlerTest < Test::Unit::TestCase
4
+ include Watchr
5
+
6
+ SingleFileWatcher = EventHandler::Unix::SingleFileWatcher
7
+
8
+ def setup
9
+ @loop = Rev::Loop.default
10
+ @handler = EventHandler::Unix.new
11
+ @loop.stubs(:run)
12
+ end
13
+
14
+ def teardown
15
+ SingleFileWatcher.handler = nil
16
+ Rev::Loop.default.watchers.every.detach
17
+ end
18
+
19
+ test "triggers listening state" do
20
+ @loop.expects(:run)
21
+ @handler.listen([])
22
+ end
23
+
24
+ ## monitoring file events
25
+
26
+ test "listens for events on monitored files" do
27
+ @handler.listen %w( foo bar )
28
+ @loop.watchers.size.should be(2)
29
+ @loop.watchers.every.path.should include('foo', 'bar')
30
+ @loop.watchers.every.class.uniq.should be([SingleFileWatcher])
31
+ end
32
+
33
+ test "notifies observers on file event" do
34
+ watcher = SingleFileWatcher.new('foo/bar')
35
+ watcher.stubs(:path).returns('foo/bar')
36
+
37
+ @handler.expects(:notify).with('foo/bar', :changed)
38
+ watcher.on_change
39
+ end
40
+
41
+ ## on the fly updates of monitored files list
42
+
43
+ test "reattaches to new monitored files" do
44
+ @handler.listen %w( foo bar )
45
+ @loop.watchers.size.should be(2)
46
+ @loop.watchers.every.path.should include('foo')
47
+ @loop.watchers.every.path.should include('bar')
48
+
49
+ @handler.refresh %w( baz bax )
50
+ @loop.watchers.size.should be(2)
51
+ @loop.watchers.every.path.should include('baz')
52
+ @loop.watchers.every.path.should include('bax')
53
+ @loop.watchers.every.path.should exclude('foo')
54
+ @loop.watchers.every.path.should exclude('bar')
55
+ end
56
+ end
@@ -0,0 +1,104 @@
1
+ require 'test/test_helper'
2
+ require 'observer'
3
+
4
+ class MockHandler
5
+ include Observable
6
+ def listen(paths) end
7
+ def refresh(paths) end
8
+ end
9
+
10
+ class TestController < Test::Unit::TestCase
11
+ include Watchr
12
+
13
+ def to_p(str)
14
+ Pathname(str).expand_path
15
+ end
16
+
17
+ def setup
18
+ @script = Script.new
19
+ @handler = MockHandler.new
20
+ @controller = Controller.new(@script, @handler)
21
+ end
22
+
23
+ test "triggers listening state on run" do
24
+ @controller.stubs(:monitored_paths).returns %w( foo bar )
25
+ @handler.expects(:listen).with %w( foo bar )
26
+ @controller.run
27
+ end
28
+
29
+ test "adds itself as handler observer" do
30
+ @handler.count_observers.should be(1)
31
+ @handler.delete_observer(@controller)
32
+ @handler.count_observers.should be(0)
33
+ end
34
+
35
+ ## monitored paths list
36
+
37
+ test "fetches monitored paths" do
38
+ Dir.expects(:[]).at_least_once.with('**/*').returns(%w(
39
+ a
40
+ b/x.z
41
+ b/c
42
+ b/c/y.z
43
+ ))
44
+ script = Script.new
45
+ script.watch('.\.z') { :x }
46
+
47
+ contrl = Controller.new(script, MockHandler.new)
48
+ contrl.monitored_paths.should include(to_p('b/x.z'))
49
+ contrl.monitored_paths.should include(to_p('b/c/y.z'))
50
+ end
51
+
52
+ test "doesn't fetch unmonitored paths" do
53
+ Dir.expects(:[]).at_least_once.with('**/*').returns(%w(
54
+ a
55
+ b/x.z
56
+ b/c
57
+ b/c/y.z
58
+ ))
59
+ script = Script.new
60
+ script.watch('.\.z') { :x }
61
+
62
+ contrl = Controller.new(script, MockHandler.new)
63
+ contrl.monitored_paths.should exclude(to_p('a'))
64
+ contrl.monitored_paths.should exclude(to_p('b/c'))
65
+ contrl.monitored_paths.should exclude(to_p('p/q.z'))
66
+ end
67
+
68
+ test "monitored paths include script" do
69
+ Dir.expects(:[]).at_least_once.with('**/*').returns(%w( a ))
70
+ Script.any_instance.stubs(:parse!)
71
+
72
+ path = to_p('some/file')
73
+ script = Script.new(path)
74
+ contrl = Controller.new(script, MockHandler.new)
75
+ contrl.monitored_paths.should include(path)
76
+ end
77
+
78
+ ## on update
79
+
80
+ test "calls action for path" do
81
+ path = to_p('abc')
82
+ @script.expects(:action_for).with(path).returns(lambda {})
83
+
84
+ @controller.update('abc')
85
+ end
86
+
87
+ test "parses script on script file update" do
88
+ path = to_p('abc')
89
+ @script.stubs(:path).returns(path)
90
+ @script.expects(:parse!)
91
+
92
+ @controller.update('abc')
93
+ end
94
+
95
+ test "refreshes handler on script file update" do
96
+ path = to_p('abc')
97
+ @script.stubs(:path).returns(path)
98
+ @controller.stubs(:monitored_paths).returns %w( foo bar )
99
+
100
+ @handler.expects(:refresh).with %w( foo bar )
101
+ @controller.update('abc')
102
+ end
103
+ end
104
+
@@ -0,0 +1,50 @@
1
+ require 'pathname'
2
+ require 'test/unit'
3
+ require 'matchy'
4
+ require 'mocha'
5
+ require 'every'
6
+ require 'pending'
7
+ begin
8
+ require 'ruby-debug'
9
+ require 'redgreen'
10
+ require 'phocus'
11
+ rescue LoadError, RuntimeError
12
+ end
13
+
14
+ root = Pathname(__FILE__).dirname.parent.expand_path
15
+ $:.unshift(root.join('lib').to_s).uniq!
16
+
17
+ require 'watchr'
18
+
19
+ class Test::Unit::TestCase
20
+ class << self
21
+ def test(name, &block)
22
+ name = :"test_#{name.gsub(/\s/,'_')}"
23
+ define_method(name, &block)
24
+ end
25
+ alias :should :test
26
+
27
+ # noop
28
+ def xtest(*args) end
29
+ end
30
+ end
31
+
32
+ # taken from minitest/unit.rb
33
+ # (with modifications)
34
+ def capture_io
35
+ require 'stringio'
36
+
37
+ orig_stdout, orig_stderr = $stdout, $stderr
38
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
39
+ $stdout, $stderr = captured_stdout, captured_stderr
40
+
41
+ yield
42
+
43
+ return Struct.new(:stdout, :stderr).new(
44
+ captured_stdout.string,
45
+ captured_stderr.string
46
+ )
47
+ ensure
48
+ $stdout = orig_stdout
49
+ $stderr = orig_stderr
50
+ end
@@ -0,0 +1,88 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestScript < Test::Unit::TestCase
4
+ include Watchr
5
+
6
+ ## external api
7
+
8
+ test "watch" do
9
+ Script.new.watch('pattern')
10
+ Script.new.watch('pattern') { nil }
11
+ end
12
+
13
+ test "default action" do
14
+ Script.new.default_action { nil }
15
+ end
16
+
17
+ ## functionality
18
+
19
+ test "rule object" do
20
+ rule = Script.new.watch('pattern') { nil }
21
+ rule.pattern.should be('pattern')
22
+ rule.action.call.should be(nil)
23
+ end
24
+
25
+ test "finds action for path" do
26
+ script = Script.new
27
+ script.watch('abc') { :x }
28
+ script.watch('def') { :y }
29
+ script.action_for('abc').call.should be(:x)
30
+ end
31
+
32
+ test "collects patterns" do
33
+ script = Script.new
34
+ script.watch('abc')
35
+ script.watch('def')
36
+ script.patterns.should include('abc')
37
+ script.patterns.should include('def')
38
+ end
39
+
40
+ test "parses script file" do
41
+ file = StringIO.new(<<-STR)
42
+ watch( 'abc' ) { :x }
43
+ STR
44
+ script = Script.new(file)
45
+ script.action_for('abc').call.should be(:x)
46
+ end
47
+
48
+ test "actions receive a MatchData object" do
49
+ script = Script.new
50
+ script.watch('de(.)') {|m| [m[0], m[1]] }
51
+ script.action_for('def').call.should be(%w( def f ))
52
+ end
53
+
54
+ test "rule's default action" do
55
+ script = Script.new
56
+
57
+ script.watch('abc')
58
+ script.action_for('abc').call.should be(nil)
59
+ script.default_action { :x }
60
+
61
+ script.watch('def')
62
+ script.action_for('def').call.should be(:x)
63
+ end
64
+
65
+ test "file path" do
66
+ Script.any_instance.stubs(:parse!)
67
+ path = Pathname('some/file').expand_path
68
+ script = Script.new(path)
69
+ script.path.should be(path)
70
+ end
71
+
72
+ test "later rules take precedence" do
73
+ script = Script.new
74
+
75
+ script.watch('a/(.*)\.x') { :x }
76
+ script.watch('a/b/(.*)\.x') { :y }
77
+
78
+ script.action_for('a/b/c.x').call.should be(:y)
79
+ end
80
+
81
+ test "rule patterns match against paths relative to pwd" do
82
+ script = Script.new
83
+
84
+ script.watch('^abc') { :x }
85
+ path = Pathname(Dir.pwd) + 'abc'
86
+ script.action_for(path).call.should be(:x)
87
+ end
88
+ end
@@ -0,0 +1,59 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestWatchr < Test::Unit::TestCase
4
+
5
+ def setup
6
+ Watchr.options = nil
7
+ end
8
+
9
+ ## options
10
+
11
+ test "debug option" do
12
+ Watchr.options.debug.should be(false)
13
+ Watchr.options.debug = true
14
+ Watchr.options.debug.should be(true)
15
+ end
16
+
17
+ ## functionality
18
+
19
+ test "debug" do
20
+ capture_io { Watchr.debug('abc') }.stdout.should be('')
21
+ Watchr.options.debug = true
22
+ capture_io { Watchr.debug('abc') }.stdout.should be("[watchr debug] abc\n")
23
+ end
24
+
25
+ test "picking handler" do
26
+ Watchr.handler = nil
27
+ ENV['HANDLER'] = 'linux'
28
+ Watchr.handler.should be(Watchr::EventHandler::Unix)
29
+
30
+ Watchr.handler = nil
31
+ ENV['HANDLER'] = 'bsd'
32
+ Watchr.handler.should be(Watchr::EventHandler::Unix)
33
+
34
+ Watchr.handler = nil
35
+ ENV['HANDLER'] = 'darwin'
36
+ Watchr.handler.should be(Watchr::EventHandler::Unix)
37
+
38
+ Watchr.handler = nil
39
+ ENV['HANDLER'] = 'unix'
40
+ Watchr.handler.should be(Watchr::EventHandler::Unix)
41
+
42
+ Watchr.handler = nil
43
+ ENV['HANDLER'] = 'mswin'
44
+ Watchr.handler.should be(Watchr::EventHandler::Portable)
45
+
46
+ Watchr.handler = nil
47
+ ENV['HANDLER'] = 'cygwin'
48
+ Watchr.handler.should be(Watchr::EventHandler::Portable)
49
+
50
+ Watchr.handler = nil
51
+ ENV['HANDLER'] = 'portable'
52
+ Watchr.handler.should be(Watchr::EventHandler::Portable)
53
+
54
+ Watchr.handler = nil
55
+ ENV['HANDLER'] = 'other'
56
+ Watchr.handler.should be(Watchr::EventHandler::Portable)
57
+ end
58
+ end
59
+
data/watchr.gemspec ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: watchr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-12 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rev
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.0
24
+ version:
25
+ description: Continious anything; project files observer/trigger.
26
+ email: mynyml@gmail.com
27
+ executables:
28
+ - watchr
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - Rakefile
35
+ - test
36
+ - test/event_handlers
37
+ - test/event_handlers/test_portable.rb
38
+ - test/event_handlers/test_base.rb
39
+ - test/event_handlers/test_unix.rb
40
+ - test/test_controller.rb
41
+ - test/test_watchr.rb
42
+ - test/test_helper.rb
43
+ - test/test_script.rb
44
+ - TODO.txt
45
+ - bin
46
+ - bin/watchr
47
+ - lib
48
+ - lib/watchr
49
+ - lib/watchr/version.rb
50
+ - lib/watchr/event_handlers
51
+ - lib/watchr/event_handlers/portable.rb
52
+ - lib/watchr/event_handlers/base.rb
53
+ - lib/watchr/event_handlers/unix.rb
54
+ - lib/watchr/script.rb
55
+ - lib/watchr/controller.rb
56
+ - lib/watchr.rb
57
+ - README.rdoc
58
+ - LICENSE
59
+ - docs.watchr
60
+ - specs.watchr
61
+ - watchr.gemspec
62
+ has_rdoc: true
63
+ homepage: ""
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Continious anything
90
+ test_files: []
91
+