rspec-rails-watchr 1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rspec-rails-watchr/version.rb +1 -1
- data/lib/rspec-rails-watchr.rb +122 -113
- metadata +1 -1
data/lib/rspec-rails-watchr.rb
CHANGED
@@ -1,143 +1,152 @@
|
|
1
1
|
require 'rspec-rails-watchr/version'
|
2
2
|
require 'term/ansicolor'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
class Watchr
|
7
|
-
String.send :include, Term::ANSIColor
|
8
|
-
|
9
|
-
module CommandLine
|
10
|
-
def terminal_columns
|
11
|
-
cols = `stty -a`.scan(/ (\d+) columns/).flatten.first
|
12
|
-
$?.success? ? cols.to_i : nil
|
13
|
-
end
|
4
|
+
class SpecWatchr
|
5
|
+
String.send :include, Term::ANSIColor
|
14
6
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
7
|
+
module CommandLine
|
8
|
+
def terminal_columns
|
9
|
+
cols = `stty -a`.scan(/ (\d+) columns/).flatten.first
|
10
|
+
$?.success? ? cols.to_i : nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def run cmd
|
14
|
+
puts "=== running: #{cmd} ".ljust(terminal_columns, '=').cyan
|
15
|
+
success = system cmd
|
16
|
+
puts "===".ljust(terminal_columns, '=').cyan
|
17
|
+
success
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear!
|
21
|
+
system 'clear'
|
22
|
+
end
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
25
|
+
module Specs
|
26
|
+
def notify message
|
27
|
+
Thread.new do
|
28
|
+
begin
|
29
|
+
require 'notify'
|
30
|
+
Notify.notify 'RSpec Result:', message
|
31
|
+
rescue
|
32
|
+
nil
|
24
33
|
end
|
25
34
|
end
|
35
|
+
end
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
begin
|
31
|
-
require 'notify'
|
32
|
-
Notify.notify 'RSpec Result:', message
|
33
|
-
rescue
|
34
|
-
nil
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
37
|
+
def rspec_command
|
38
|
+
@rspec_command ||= File.exist?('./.rspec') ? 'rspec' : 'spec'
|
39
|
+
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
def rspec options
|
42
|
+
unless options.empty?
|
43
|
+
success = run("bundle exec #{rspec_command} #{options}")
|
44
|
+
notify( success ? '♥♥ SUCCESS :) ♥♥' : '♠♠ FAILED >:( ♠♠' )
|
45
|
+
end
|
46
|
+
end
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
notify( success ? '♥♥ SUCCESS :) ♥♥' : '♠♠ FAILED >:( ♠♠' )
|
47
|
-
end
|
48
|
-
end
|
48
|
+
def rspec_all
|
49
|
+
rspec 'spec'
|
50
|
+
end
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
-
|
52
|
+
def rspec_files *files
|
53
|
+
rspec files.join(' ')
|
54
|
+
end
|
53
55
|
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
def specs_for(path)
|
57
|
+
print "--- Searching specs for #{path.inspect}...".yellow
|
58
|
+
specs = match_specs path, Dir['spec/**/*_spec.rb']
|
59
|
+
puts specs.empty? ? ' nothing found.'.red : " #{specs.size} matched.".green
|
60
|
+
specs
|
61
|
+
end
|
57
62
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
puts specs.empty? ? ' nothing found.'.red : " #{specs.size} matched.".green
|
62
|
-
specs
|
63
|
-
end
|
63
|
+
def default_rails_matcher path, specs
|
64
|
+
specs.grep(/\b#{path}((_spec)?\.rb)?$/)
|
65
|
+
end
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
def match_specs path, specs
|
68
|
+
matched_specs = @custom_matcher.call(path, specs) if @custom_matcher
|
69
|
+
matched_specs = default_rails_matcher(path, specs) if matched_specs.nil?
|
70
|
+
end
|
71
|
+
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
73
|
+
module Control
|
74
|
+
def exit_watchr
|
75
|
+
@exiting = true
|
76
|
+
puts '--- Exiting...'.white
|
77
|
+
exit
|
78
|
+
end
|
74
79
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
exit
|
80
|
-
end
|
80
|
+
def abort_watchr!
|
81
|
+
puts '--- Forcing abort...'.white
|
82
|
+
abort("\n")
|
83
|
+
end
|
81
84
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
85
|
+
def reload!
|
86
|
+
# puts ARGV.join(' ')
|
87
|
+
exec('bundle exec watchr')
|
88
|
+
end
|
86
89
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
90
|
+
def reload_file_list
|
91
|
+
require 'shellwords'
|
92
|
+
system "touch #{__FILE__.shellescape}"
|
93
|
+
# puts '--- Watch\'d file list reloaded.'.green
|
94
|
+
end
|
91
95
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
+
def trap_int!
|
97
|
+
# Ctrl-C
|
98
|
+
|
99
|
+
@interrupted ||= false
|
100
|
+
|
101
|
+
Signal.trap('INT') {
|
102
|
+
puts ' (Interrupted with CTRL+C)'.red
|
103
|
+
if @interrupted
|
104
|
+
@exiting ? abort_watchr : exit_watchr
|
105
|
+
else
|
106
|
+
@interrupted = true
|
107
|
+
# reload_file_list
|
108
|
+
print '--- What to do now? (q=quit, a=all-specs, r=reload): '.yellow
|
109
|
+
case STDIN.gets.chomp.strip.downcase
|
110
|
+
when 'q'; @interrupted = false; exit_watchr
|
111
|
+
when 'a'; @interrupted = false; rspec_all
|
112
|
+
when 'r'; @interrupted = false; reload!
|
113
|
+
else
|
114
|
+
@interrupted = false
|
115
|
+
puts '--- Bad input, ignored.'.yellow
|
116
|
+
end
|
117
|
+
puts '--- Waiting for changes...'.cyan
|
96
118
|
end
|
119
|
+
}
|
120
|
+
end
|
121
|
+
end
|
97
122
|
|
98
|
-
def trap_int!
|
99
|
-
# Ctrl-C
|
100
|
-
|
101
|
-
@interrupted ||= false
|
102
|
-
|
103
|
-
Signal.trap('INT') {
|
104
|
-
puts ' (Interrupted with CTRL+C)'.red
|
105
|
-
if @interrupted
|
106
|
-
@exiting ? abort_watchr : exit_watchr
|
107
|
-
else
|
108
|
-
@interrupted = true
|
109
|
-
# reload_file_list
|
110
|
-
print '--- What to do now? (q=quit, a=all-specs, r=reload): '.yellow
|
111
|
-
case STDIN.gets.chomp.strip.downcase
|
112
|
-
when 'q'; @interrupted = false; exit_watchr
|
113
|
-
when 'a'; @interrupted = false; rspec_all
|
114
|
-
when 'r'; @interrupted = false; reload!
|
115
|
-
else
|
116
|
-
@interrupted = false
|
117
|
-
puts '--- Bad input, ignored.'.yellow
|
118
|
-
end
|
119
|
-
puts '--- Waiting for changes...'.cyan
|
120
|
-
end
|
121
|
-
}
|
122
|
-
end
|
123
|
-
end
|
124
123
|
|
124
|
+
include CommandLine
|
125
|
+
include Specs
|
126
|
+
include Control
|
125
127
|
|
126
|
-
|
127
|
-
|
128
|
-
|
128
|
+
def initialize watchr, &block
|
129
|
+
@custom_matcher = block if block_given?
|
130
|
+
@watchr = watchr
|
129
131
|
|
130
|
-
|
131
|
-
|
132
|
-
@watchr = watchr
|
132
|
+
watchr.watch('^spec/(.*)_spec\.rb$') {|m| rspec_files specs_for(m[1])}
|
133
|
+
watchr.watch('^(?:app|lib|script)/(.*)(?:\.rb|\.\w+|)$') {|m| rspec_files specs_for(m[1].gsub(/\.rb$/,''))}
|
133
134
|
|
134
|
-
|
135
|
-
|
135
|
+
trap_int!
|
136
|
+
|
137
|
+
puts '--- Waiting for changes...'.cyan
|
138
|
+
end
|
139
|
+
end
|
136
140
|
|
137
|
-
trap_int!
|
138
141
|
|
139
|
-
|
142
|
+
class Object
|
143
|
+
module Rspec
|
144
|
+
module Rails
|
145
|
+
module Watchr
|
146
|
+
def self.new *args, &block
|
147
|
+
SpecWatchr.new *args, &block
|
148
|
+
end
|
140
149
|
end
|
141
150
|
end
|
142
151
|
end
|
143
|
-
end
|
152
|
+
end
|