observr 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,163 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestScript < MiniTest::Unit::TestCase
4
+ include Observr
5
+
6
+ def setup
7
+ @script = Script.new
8
+ end
9
+
10
+ ## external api
11
+
12
+ test "watch" do
13
+ @script.ec.watch('pattern')
14
+ @script.ec.watch('pattern', :event_type)
15
+ @script.ec.watch('pattern') { nil }
16
+ end
17
+
18
+ test "default action" do
19
+ @script.ec.default_action { nil }
20
+ end
21
+
22
+ test "reload" do
23
+ @script.ec.reload
24
+ end
25
+
26
+ test "eval context delegates methods to script" do
27
+ @script.ec.watch('pattern')
28
+ @script.ec.watch('pattern', :event_type)
29
+ @script.ec.watch('pattern') { nil }
30
+ @script.ec.default_action { :foo }
31
+
32
+ assert_equal 3, @script.rules.size
33
+ assert_equal :foo, @script.default_action.call
34
+ end
35
+
36
+ ## functionality
37
+
38
+ test "rule object" do
39
+ rule = @script.watch('pattern', :modified) { nil }
40
+
41
+ assert_equal 'pattern', rule.pattern
42
+ assert_equal :modified, rule.event_type
43
+ assert_equal nil, rule.action.call
44
+ end
45
+
46
+ test "default event type" do
47
+ rule = @script.watch('pattern') { nil }
48
+ assert_equal :modified, rule.event_type
49
+ end
50
+
51
+ test "finds action for path" do
52
+ @script.watch('abc') { :x }
53
+ @script.watch('def') { :y }
54
+ assert_equal :x, @script.action_for('abc').call
55
+ end
56
+
57
+ test "finds action for path with event type" do
58
+ @script.watch('abc', :accessed) { :x }
59
+ @script.watch('abc', :modified) { :y }
60
+ assert_equal :x, @script.action_for('abc', :accessed).call
61
+ end
62
+
63
+ test "finds action for path with any event type" do
64
+ @script.watch('abc', nil) { :x }
65
+ @script.watch('abc', :modified) { :y }
66
+ assert_equal :x, @script.action_for('abc', :accessed).call
67
+ end
68
+
69
+ test "no action for path" do
70
+ @script.watch('abc', :accessed) { :x }
71
+ assert_nil @script.action_for('abc', :modified).call
72
+ end
73
+
74
+ test "collects patterns" do
75
+ @script.watch('abc')
76
+ @script.watch('def')
77
+ assert_includes @script.patterns, 'abc'
78
+ assert_includes @script.patterns, 'def'
79
+ end
80
+
81
+ test "parses script file" do
82
+ path = Pathname( Tempfile.open('bar').path )
83
+ path.open('w') {|f| f.write <<-STR }
84
+ watch( 'abc' ) { :x }
85
+ STR
86
+ script = Script.new(path)
87
+ script.parse!
88
+ assert_equal :x, script.action_for('abc').call
89
+ end
90
+
91
+ test "__FILE__ is set properly in script file" do
92
+ path = Pathname( Tempfile.open('bar').path )
93
+ path.open('w') {|f| f.write <<-STR }
94
+ throw __FILE__.to_sym
95
+ STR
96
+ script = Script.new(path)
97
+ assert_throws(path.to_s.to_sym) { script.parse! }
98
+ end
99
+
100
+ test "reloads script file" do
101
+ @script.expects(:parse!)
102
+ @script.ec.reload
103
+ end
104
+
105
+ test "skips parsing on nil script file" do
106
+ script = Script.new
107
+ script.ec.stubs(:instance_eval).raises(Exception) #negative expectation hack
108
+ script.parse!
109
+ end
110
+
111
+ test "resets state" do
112
+ @script.default_action { 'x' }
113
+ @script.watch('foo') { 'bar' }
114
+ @script.reset
115
+ assert_nil @script.default_action.call
116
+ assert_equal [], @script.rules
117
+ end
118
+
119
+ test "resets state on parse" do
120
+ script = Script.new( Pathname( Tempfile.new('foo').path ) )
121
+ script.stubs(:instance_eval)
122
+ script.expects(:reset)
123
+ script.parse!
124
+ end
125
+
126
+ test "actions receive a MatchData object" do
127
+ @script.watch('de(.)') {|m| [m[0], m[1]] }
128
+ assert_equal %w( def f ), @script.action_for('def').call
129
+ end
130
+
131
+ test "rule's default action" do
132
+ @script.watch('abc')
133
+ assert_nil @script.action_for('abc').call
134
+
135
+ @script.default_action { :x }
136
+ @script.watch('def')
137
+ assert_equal :x, @script.action_for('def').call
138
+ end
139
+
140
+ test "file path" do
141
+ Script.any_instance.stubs(:parse!)
142
+ path = Pathname('some/file').expand_path
143
+ script = Script.new(path)
144
+ assert_equal path, script.path
145
+ end
146
+
147
+ test "nil file path" do
148
+ script = Script.new
149
+ assert_nil script.path
150
+ end
151
+
152
+ test "later rules take precedence" do
153
+ @script.watch('a/(.*)\.x') { :x }
154
+ @script.watch('a/b/(.*)\.x') { :y }
155
+ assert_equal :y, @script.action_for('a/b/c.x').call
156
+ end
157
+
158
+ test "rule patterns match against paths relative to pwd" do
159
+ @script.watch('^abc') { :x }
160
+ path = Pathname(Dir.pwd) + 'abc'
161
+ assert_equal :x, @script.action_for(path).call
162
+ end
163
+ end
@@ -0,0 +1,76 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestObservr < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ Observr.options = nil
7
+ end
8
+
9
+ ## options
10
+
11
+ test "debug option" do
12
+ assert_equal false, Observr.options.debug
13
+ Observr.options.debug = true
14
+ assert_equal true, Observr.options.debug
15
+ end
16
+
17
+ ## functionality
18
+
19
+ test "debug" do
20
+ assert_empty capture_io { Observr.debug('abc') }.first
21
+ Observr.options.debug = true
22
+ assert_equal "[observr debug] abc\n", capture_io { Observr.debug('abc') }.first
23
+ end
24
+
25
+ test "picking handler" do
26
+
27
+ if Observr::HAVE_REV
28
+
29
+ Observr.handler = nil
30
+ ENV['HANDLER'] = 'linux'
31
+ assert_equal Observr::EventHandler::Unix, Observr.handler
32
+
33
+ Observr.handler = nil
34
+ ENV['HANDLER'] = 'bsd'
35
+ assert_equal Observr::EventHandler::Unix, Observr.handler
36
+
37
+ Observr.handler = nil
38
+ ENV['HANDLER'] = 'unix'
39
+ assert_equal Observr::EventHandler::Unix, Observr.handler
40
+
41
+ end
42
+
43
+ if Observr::HAVE_FSE
44
+
45
+ Observr.handler = nil
46
+ ENV['HANDLER'] = 'darwin'
47
+ assert_equal Observr::EventHandler::Darwin, Observr.handler
48
+
49
+ Observr.handler = nil
50
+ ENV['HANDLER'] = 'osx'
51
+ assert_equal Observr::EventHandler::Darwin, Observr.handler
52
+
53
+ Observr.handler = nil
54
+ ENV['HANDLER'] = 'fsevent'
55
+ assert_equal Observr::EventHandler::Darwin, Observr.handler
56
+
57
+ end
58
+
59
+ Observr.handler = nil
60
+ ENV['HANDLER'] = 'mswin'
61
+ assert_equal Observr::EventHandler::Portable, Observr.handler
62
+
63
+ Observr.handler = nil
64
+ ENV['HANDLER'] = 'cygwin'
65
+ assert_equal Observr::EventHandler::Portable, Observr.handler
66
+
67
+ Observr.handler = nil
68
+ ENV['HANDLER'] = 'portable'
69
+ assert_equal Observr::EventHandler::Portable, Observr.handler
70
+
71
+ Observr.handler = nil
72
+ ENV['HANDLER'] = 'other'
73
+ assert_equal Observr::EventHandler::Portable, Observr.handler
74
+ end
75
+ end
76
+
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: observr
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - mynyml
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: every
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Modern continuous testing (flexible alternative to autotest).
56
+ email: mynyml@gmail.com
57
+ executables:
58
+ - observr
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - History.txt
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - TODO.md
68
+ - bin/observr
69
+ - contributions.txt
70
+ - docs.observr
71
+ - gem.observr
72
+ - lib/observr.rb
73
+ - lib/observr/controller.rb
74
+ - lib/observr/event_handlers/base.rb
75
+ - lib/observr/event_handlers/darwin.rb
76
+ - lib/observr/event_handlers/portable.rb
77
+ - lib/observr/event_handlers/unix.rb
78
+ - lib/observr/script.rb
79
+ - observr.gemspec
80
+ - specs.watchr
81
+ - test/README
82
+ - test/event_handlers/test_base.rb
83
+ - test/event_handlers/test_darwin.rb
84
+ - test/event_handlers/test_portable.rb
85
+ - test/event_handlers/test_unix.rb
86
+ - test/test_controller.rb
87
+ - test/test_helper.rb
88
+ - test/test_script.rb
89
+ - test/test_watchr.rb
90
+ homepage: http://mynyml.com/ruby/flexible-continuous-testing
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project: observr
110
+ rubygems_version: 2.0.0
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Modern continuous testing (flexible alternative to autotest)
114
+ test_files: []