This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.watchr
151 lines (134 loc) · 3.19 KB
/
tests.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env watchr
# vim:ft=ruby
def have_notify_send?
case `which notify-send`.empty?
when true
false
else
true
end
end
def have_growl?
@have_growl ||= begin
require 'growl'
rescue LoadError
false
end
end
def error_icon_name
"gtk-dialog-error"
end
def success_icon_name
"gtk-dialog-info"
end
# Rules
watch('^spec/.+_spec\.rb$') { |md| spec md[0] }
watch('^lib/.+\.rb$') { |md| spec "spec/#{File.basename(md[0]).gsub(/\..*?$/, '')}_spec.rb" }
watch('^features/.+\.feature$') { |md| feature md[0] }
watch('^features/step_definitions/(.+)_steps\.rb$') { |md| feature "features/#{md[1]}.feature" }
# Notify using notify-send.
#
# @param icon [String] name of stock icon to use.
# @param title [String] title of notification.
# @param message [String] message for notification body.
# @return [Boolean] true if the command ran successfully, false
# otherwise.
def notify(icon, title, message)
system("notify-send -t 3000 -i #{icon} \"#{title}\" \"#{message}\"")
end
# Notify of success.
#
def notify_success
puts "Success"
if have_notify_send?
notify success_icon_name, "All green!", "Now write more tests :)"
elsif have_growl?
Growl.notify_ok "All green!"
end
end
# Notify of failure.
#
def notify_failure
puts "Failure"
if have_notify_send?
notify error_icon_name, "Something is broken", "Now go fix it :)"
elsif have_growl?
Growl.notify_error "Something is broken"
end
end
# Run a single ruby command. Notify appropriately.
#
# @param cmd [String] command to run.
def run(cmd)
system('clear')
puts "Running #{cmd}"
if system(cmd)
notify_success
true
else
notify_failure
false
end
end
# Run a single spec.
#
# @param specfiles [String] path to specfile or [Array] of paths.
def spec(specfiles)
specs = case specfiles.respond_to? :join
when true
specfiles.join(" ")
when false
specfiles
end
if run(%Q(rspec #{specs}))
if @last_run_failed
@last_run_failed = false
run_all_specs
end
else
@last_run_failed = true
end
!@last_run_failed
end
# Run a single feature.
#
# @param featurefiles [String] path to feature file or [Array] of paths.
def feature(featurefiles)
features = case featurefiles.respond_to? :join
when true
featurefiles.join(" ")
when false
featurefiles
end
run(%Q(cucumber #{features}))
end
# Run all specs.
#
def run_all_specs
spec Dir['spec/*_spec.rb']
end
# Run all features.
#
def run_features
feature Dir['features/*.feature']
end
# Run specs and features.
#
def run_suite
run_all_specs && run_features
end
# Run all specs on Ctrl-\
Signal.trap('QUIT') { run_all_specs }
# Run full suite on one Ctrl-C, quit on two
@interrupted = false
Signal.trap('INT') do
if @interrupted
abort("\n")
else
puts "Interrupt a second time to quit"
@interrupted = true
Kernel.sleep 1.5
run_suite
@interrupted = false
end
end