tdd 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/bin/tdd +258 -33
  2. data/lib/rego.rb +55 -0
  3. data/lib/tdd/version.rb +1 -1
  4. data/tdd.gemspec +0 -1
  5. metadata +4 -19
data/bin/tdd CHANGED
@@ -1,35 +1,260 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
- pos = ARGV.index('--')
4
- if pos
5
- @paths = ARGV[0 ... pos].join(' ')
6
- @test_args = ARGV[pos + 1 .. -1].join(' ')
7
- else
8
- @paths = "app lib test spec"
9
- @test_args = ARGV[0..-1].join(' ')
10
- end
11
- @test_file = @test_args.scan(/^.+.rb/).first
12
-
13
- if @paths == "glob"
14
- search = File.basename(@test_file).gsub(/(_spec|_test)/, '')
15
- @paths = Dir.glob("**/#{search}")
16
- @paths << @test_file
17
- @paths = @paths.join(' ')
18
- end
19
-
20
- if @paths == "controller"
21
- resource = File.basename(@test_file).gsub(/(_controller_spec.rb|_controller_test.rb)/, '')
22
- search = File.basename(@test_file).gsub(/(_spec|_test)/, '')
23
- puts "="*45
24
- puts resource
25
- puts "="*45
26
- @paths = Dir.glob("app/views/#{resource}/**")
27
- @paths += Dir.glob("**/#{search}")
28
- @paths << @test_file
29
- @paths = @paths.join(' ')
30
- end
31
-
32
- test_command = 'rspec' if @test_file.match(/_spec.rb/)
33
- test_command = 'ruby -Itest' if @test_file.match(/_test.rb/)
34
-
35
- exec "rego #{@paths} -- #{test_command} #{@test_args}"
3
+ Main {
4
+
5
+ name <<-__
6
+
7
+ tdd
8
+
9
+ __
10
+
11
+ description <<-__
12
+
13
+ watch files and run test unit or rspec tests when they change
14
+ __
15
+
16
+ examples <<-__
17
+
18
+ ### gem install tdd
19
+
20
+ # Using with test/unit:
21
+ $ tdd test/unit/some_unit_test.rb
22
+
23
+ # Using with rspec:
24
+ $ tdd spec/some_spec.rb
25
+
26
+ # You can pass arguments you would normally pass to `ruby -Itest` or `rspec`
27
+ $ tdd test/unit/some_unit_test.rb -n /some_tests_name/
28
+ $ tdd spec/some_spec.rb:42 --fail-fast
29
+
30
+ # tdd will by default watch files in app, lib, config, test, and spec
31
+ # directories, if they exist, and run your test command if any file its
32
+ # watching changes.
33
+
34
+ # You can specify which files to watch:
35
+ $
36
+ # You can tell it to find a similarly named file to your test to watch:
37
+ $ tdd glob -- test/unit/some_unit_test.rb
38
+ # will look for some_unit.rb in your project and watch it for changes, along
39
+ # with the test file.
40
+
41
+ # In a Rails project you can ask tdd to watch view and controller files
42
+ # related to a functional or controller test:
43
+ $ tdd controller -- test/functional/users_controller_test.rb
44
+ # will watch all view files in app/views/users, the users_controller and the
45
+ # test file for changes.
46
+ __
47
+
48
+ def run
49
+ parse_the_command_line
50
+ print_a_summary_of_watched_files
51
+ loop_watching_files_and_running_commands
52
+ end
53
+
54
+ def parse_the_command_line
55
+
56
+ pos = ARGV.index('--')
57
+ if pos
58
+ @paths = ARGV[0 ... pos]
59
+ @test_args = ARGV[pos + 1 .. -1].join(' ')
60
+ else
61
+ @paths = %w[app lib config test spec]
62
+ @test_args = ARGV[0..-1].join(' ')
63
+ end
64
+ @test_file = @test_args.scan(/^.+.rb/).first
65
+
66
+ if @paths.first == "glob"
67
+ search = File.basename(@test_file).gsub(/(_spec|_test)/, '')
68
+ @paths = Dir.glob("**/#{search}")
69
+ @paths << @test_file
70
+ end
71
+
72
+ if @paths.first == "controller"
73
+ resource = File.basename(@test_file).gsub(/(_controller_spec.rb|_controller_test.rb)/, '')
74
+ search = File.basename(@test_file).gsub(/(_spec|_test)/, '')
75
+ puts "="*45
76
+ puts resource
77
+ puts "="*45
78
+ @paths = Dir.glob("app/views/#{resource}/**")
79
+ @paths += Dir.glob("**/#{search}")
80
+ @paths << @test_file
81
+ end
82
+
83
+ puts @paths.inspect
84
+ test_command = 'rspec' if @test_file.match(/_spec.rb/)
85
+ test_command = 'ruby -Itest' if @test_file.match(/_test.rb/)
86
+ @command = "#{test_command} #{@test_args}"
87
+
88
+ @paths = %w[.] if @paths.empty?
89
+
90
+ @paths.map!{|path| test(?d, path) ? [path, Dir.glob(File.join(path, '**/**'))] : path}
91
+ @paths.flatten!
92
+ @paths.compact!
93
+ @paths.uniq!
94
+ @paths.map! do |path|
95
+ begin
96
+ Pathname.new(path).realpath.to_s
97
+ rescue Object
98
+ nil
99
+ end
100
+ end
101
+ @paths.compact!
102
+ end
103
+
104
+ def print_a_summary_of_watched_files
105
+ puts "## #{ @command }"
106
+ puts "#"
107
+ puts @paths.join("\n")
108
+ puts
109
+ end
110
+
111
+ def loop_watching_files_and_running_commands
112
+ directories = []
113
+ files = []
114
+ @paths.each do |path|
115
+ if test(?d, path)
116
+ directories.push(path)
117
+ else
118
+ files.push(path)
119
+ directories.push(File.dirname(path))
120
+ end
121
+ end
122
+ directories.uniq!
123
+ files.uniq!
124
+
125
+ stats = {}
126
+
127
+ files.each do |file|
128
+ begin
129
+ stats[file] = File.stat(file)
130
+ rescue
131
+ nil
132
+ end
133
+ end
134
+
135
+ fsevent = FSEvent.new
136
+
137
+ n = '0'
138
+ line = '#' * 42
139
+ $running = false
140
+
141
+ rego = proc do |*args|
142
+ entry = args.shift
143
+ cmd = entry ? @command.gsub(/@/, entry) : @command
144
+ puts line
145
+ say("# rego.#{ n } @ #{ Time.now.strftime('%H:%M:%S') } - #{ cmd }", :color => :magenta)
146
+ puts
147
+ system(cmd)
148
+ puts
149
+ say("# rego.#{ n } @ #{ Time.now.strftime('%H:%M:%S') } - #{ $?.exitstatus }", :color => :yellow)
150
+ puts
151
+ n.succ!
152
+ end
153
+
154
+ rego[ false ]
155
+
156
+ fsevent.watch(directories) do |*args|
157
+ unless $running
158
+ $running = true
159
+
160
+ args.flatten.each do |dir|
161
+ glob = File.join(dir, '**/**')
162
+ entries = Dir.glob(glob)
163
+
164
+ entries.each do |entry|
165
+ entry = File.expand_path(entry)
166
+ next unless stats.has_key?(entry)
167
+
168
+ begin
169
+ stats[entry] ||= File.stat(entry)
170
+ before = stats[entry]
171
+ after = File.stat(entry)
172
+ rescue
173
+ next
174
+ end
175
+
176
+ unless before.mtime == after.mtime
177
+ stats[entry] = after
178
+ rego[ entry ]
179
+ end
180
+ end
181
+ end
182
+ end
183
+ $running = false
184
+ end
185
+
186
+ fsevent.run
187
+ end
188
+ }
189
+
190
+
191
+ BEGIN {
192
+ ANSI = {
193
+ :clear => "\e[0m",
194
+ :reset => "\e[0m",
195
+ :erase_line => "\e[K",
196
+ :erase_char => "\e[P",
197
+ :bold => "\e[1m",
198
+ :dark => "\e[2m",
199
+ :underline => "\e[4m",
200
+ :underscore => "\e[4m",
201
+ :blink => "\e[5m",
202
+ :reverse => "\e[7m",
203
+ :concealed => "\e[8m",
204
+ :black => "\e[30m",
205
+ :red => "\e[31m",
206
+ :green => "\e[32m",
207
+ :yellow => "\e[33m",
208
+ :blue => "\e[34m",
209
+ :magenta => "\e[35m",
210
+ :cyan => "\e[36m",
211
+ :white => "\e[37m",
212
+ :on_black => "\e[40m",
213
+ :on_red => "\e[41m",
214
+ :on_green => "\e[42m",
215
+ :on_yellow => "\e[43m",
216
+ :on_blue => "\e[44m",
217
+ :on_magenta => "\e[45m",
218
+ :on_cyan => "\e[46m",
219
+ :on_white => "\e[47m"
220
+ }
221
+
222
+ module Kernel
223
+ private
224
+ def say(phrase, *args)
225
+ options = args.last.is_a?(Hash) ? args.pop : {}
226
+ options[:color] = args.shift.to_s.to_sym unless args.empty?
227
+ keys = options.keys
228
+ keys.each{|key| options[key.to_s.to_sym] = options.delete(key)}
229
+
230
+ color = options[:color]
231
+ bold = options.has_key?(:bold)
232
+
233
+ parts = [phrase]
234
+
235
+ if STDOUT.tty?
236
+ parts.unshift(ANSI[color]) if color
237
+ parts.unshift(ANSI[:bold]) if bold
238
+ parts.push(ANSI[:clear]) if parts.size > 1
239
+ end
240
+
241
+ method = options[:method] || :puts
242
+
243
+ send(method, parts.join)
244
+ end
245
+ end
246
+
247
+ require 'pathname'
248
+ this = Pathname.new(__FILE__).realpath.to_s
249
+ bindir = File.dirname(this)
250
+ rootdir = File.dirname(bindir)
251
+ libdir = File.join(rootdir, 'lib')
252
+ rego = File.join(libdir, 'rego.rb')
253
+
254
+ require(rego)
255
+
256
+ STDOUT.sync = true
257
+ STDERR.sync = true
258
+ STDIN.sync = true
259
+ }
260
+
data/lib/rego.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'time'
2
+ require 'pathname'
3
+ require 'yaml'
4
+
5
+ module Rego
6
+ Version = '1.0.42' unless defined?(Version)
7
+
8
+ def version
9
+ Rego::Version
10
+ end
11
+
12
+ def dependencies
13
+ {
14
+ 'main' => [ 'main' , ' >= 4.8.1' ] ,
15
+ 'rb-fsevent' => [ 'rb-fsevent' , ' >= 0.4.3.1' ]
16
+ }
17
+ end
18
+
19
+ def libdir(*args, &block)
20
+ @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
21
+ args.empty? ? @libdir : File.join(@libdir, *args)
22
+ ensure
23
+ if block
24
+ begin
25
+ $LOAD_PATH.unshift(@libdir)
26
+ block.call()
27
+ ensure
28
+ $LOAD_PATH.shift()
29
+ end
30
+ end
31
+ end
32
+
33
+ def load(*libs)
34
+ libs = libs.join(' ').scan(/[^\s+]+/)
35
+ Rego.libdir{ libs.each{|lib| Kernel.load(lib) } }
36
+ end
37
+
38
+ extend(Rego)
39
+ end
40
+
41
+ # gems
42
+ #
43
+ begin
44
+ require 'rubygems'
45
+ rescue LoadError
46
+ nil
47
+ end
48
+
49
+ if defined?(gem)
50
+ Rego.dependencies.each do |lib, dependency|
51
+ gem(*dependency)
52
+ require(lib)
53
+ end
54
+ end
55
+
data/lib/tdd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tdd
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/tdd.gemspec CHANGED
@@ -15,5 +15,4 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Tdd::VERSION
17
17
 
18
- gem.add_runtime_dependency "rego"
19
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-05 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rego
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
+ dependencies: []
30
14
  description: Fire up that tdd loop
31
15
  email:
32
16
  - tyler.a.montgomery@gmail.com
@@ -41,6 +25,7 @@ files:
41
25
  - README.md
42
26
  - Rakefile
43
27
  - bin/tdd
28
+ - lib/rego.rb
44
29
  - lib/tdd.rb
45
30
  - lib/tdd/version.rb
46
31
  - tdd.gemspec