This repository has been archived by the owner on Jul 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.watchr
132 lines (107 loc) · 3.03 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'rubygems'
# TODO Use libnotify
require 'gir_ffi'
# If your spec runner is at a different location: customize it here..
RSPEC_RUNNER = "bundle exec rspec" unless defined?(RSPEC_RUNNER)
JASMINE_RUNNER = "bundle exec jasmine-headless-webkit --color" unless defined?(JASMINE_RUNNER)
GirFFI.setup :Notify
Notify.init("Watchr")
#growl("Watchr started", "wachr started", "dialog-information")
# ---------
# Signals
# ---------
Signal.trap('QUIT') {
run_all_specs(File.join(Dir.pwd, 'spec'))
run_all_jasmine
} # Ctrl-\
Signal.trap('INT') { abort("\nBye.\n") } # Ctrl-C
# ---------
# Rules
# ---------
watch("spec/.*/*_spec.rb") do |md|
run "Runnning: #{md[0]}" do
`#{RSPEC_RUNNER} #{md[0]}`
end
end
watch("app/(.*/.*).rb") do |match|
spec = %{spec/#{match[1]}_spec.rb}
if File.exist?(spec)
run "Running: #{spec}" do
`#{RSPEC_RUNNER} #{spec}`
end
end
end
watch("app/(.*/.*).coffee") do |match|
run_all_jasmine
end
watch("spec/(.*/.*).coffee") do |match|
run_jasmine match[0]
end
# ---------
# Helpers
# ---------
def run_jasmine(target)
run "Running all in: #{target}", "jasmine" do
`#{JASMINE_RUNNER} #{target}`
end
end
def run_all_jasmine
run_jasmine("spec/javascript")
end
def run_all_specs(target)
run "Running all in: #{target}" do
`#{RSPEC_RUNNER} #{target}`
end
end
def run(description, runner = 'rspec', &block)
puts "#{description}"
result = parse_result(block.call, runner)
if result[:tests] =~ /\d+/
if result[:success]
title = "Specs Passed!"
dialog_class = "dialog-information"
else
title = "Specs Failed!"
dialog_class = "dialog-error"
end
specs_count = pluralize(result[:tests], "example", "examples")
failed_count = pluralize(result[:failures], "failure", "failures")
pending_count = pluralize(result[:pending], "pending", "pending")
growl(title, "#{specs_count}, #{failed_count}, #{pending_count}", dialog_class)
else
growl("Running Specs Failed!", "Runner returned an error..")
end
end
def pluralize(count, singular, plural)
return "" if count.nil?
count == "1" ? "#{count} #{singular}" : "#{count} #{plural}"
end
def growl(title, message, dialog_class = nil)
notification = Notify::Notification.new(title, message, dialog_class)
notification.show
end
def parse_result(result, runner)
puts result
send "parse_result_#{runner}", result
end
def parse_result_rspec(result)
duration = result.scan(/Finished in (\d.\d+) seconds/).flatten.first
tests, failures, pending = result.scan(/(\d+) examples?, (\d+) failures?(?:, (\d+) pending?)?/).flatten
{
:tests => tests,
:pending => pending,
:failures => failures,
:success => failures == "0",
:duration => duration
}
end
def parse_result_jasmine(result)
summary, tests, failures, duration = result.scan(/(\w+): (\d+) tests?, (\d+) failures?, (\d+.\d+) secs./).flatten
{
:tests => tests,
:pending => 0,
:failures => failures,
:success => !summary.nil? && summary.end_with?("PASS"),
:duration => duration
}
end