This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.watchr
111 lines (90 loc) · 2.32 KB
/
.watchr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'pty'
def results(str)
begin
results = if str.include?('Finished in')
str[str.rindex(/Finished in .* seconds/im)..-1].gsub(/\e\[(\d+)m/, '').match(%r/(\d+) examples?, (\d+) failures?/im)
end
failures = results[2].to_i
tests = results[1].to_i
[tests, failures]
rescue Exception => e
[0,0]
end
end
def run(cmd)
Process.kill(9, @pid) if @pid
output = ''
begin
PTY.spawn(cmd) do |r, w, pid|
@pid = pid
begin
r.each_char do |char|
print char
output.concat char
end
rescue Errno::EIO
end
end
rescue PTY::ChildExited => e
e.status.exitstatus
rescue Exception => e
puts e
end
@pid = nil
results(output)
end
def growl(tests, failed)
growlnotify = `which growlnotify`.chomp
title = 'Watchr Test Results'
message = '%d Tests, %d Tests Failed' % [tests, failed]
base_dir = File.dirname(__FILE__)
image = failed == 0 ? "#{base_dir}/watchr/passed.jpg" : "#{base_dir}/watchr/failed.jpg"
severity = failed == 0 ? '-1' : '1'
options = "-w -n Watchr --image '#{File.expand_path(image)}'"
options << " -m '#{message}' '#{title}' -p #{severity}"
system %(#{growlnotify} #{options} &)
end
def all_test_files(dir)
Dir["#{dir}/**/*_spec.rb"]
end
def run_all_tests
r1 = run("bundle exec rspec #{all_test_files('spec').join(' ')}")
growl(r1[0], r1[1])
end
def run_spec(file)
unless File.exist?(file)
puts "#{file} does not exist"
return
end
puts "Running #{file}"
result = run "bundle exec rspec #{file}"
growl(result[0], result[1])
puts
end
#######################################################
# RULES
watch('^spec/.*/*_spec.rb') { |match| run_spec match[0] }
watch('^app/(.*/.*).rb') { |match| run_spec %{spec/#{match[1]}_spec.rb} }
watch('^lib/(.*/.*).rb') { |match| run_spec %{spec/#{match[1]}_spec.rb} }
#######################################################
# Ctrl-\
Signal.trap 'QUIT' do
puts " --- Running all tests ---\n\n"
run_all_tests
end
@interrupted = false
# Ctrl-C
Signal.trap 'INT' do
if @interrupted then
@wants_to_quit = true
abort("\n")
else
puts "Interrupt a second time to quit"
@interrupted = true
Kernel.sleep 1
# raise Interrupt, nil # let the run loop catch it
run_all_tests
@interrupted = false
end
end
run_all_tests