This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
forked from landcentral/eloqua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rspec.watchr
74 lines (60 loc) · 2.25 KB
/
rspec.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
if __FILE__ == $0
puts "Run with: watchr #{__FILE__}. \n\nRequired gems: watchr rev"
exit 1
end
# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
def run(cmd)
puts(cmd)
system(cmd)
end
def run_all_specs
run "bundle exec rspec spec"
end
def run_single_spec *spec
spec = spec.join(' ')
run "bundle exec rspec #{spec}"
end
def run_specs_with_shared_examples(shared_example_filename, spec_path = 'spec')
# Returns the names of the shared examples in filename
def shared_examples(filename)
lines = File.readlines(filename)
lines.grep(/shared_examples_for[\s'"]+(.+)['"]\s*[do|\{]/) do |matching_line|
$1
end
end
# Returns array with filenames of the specs using shared_example
def specs_with_shared_example(shared_example, path)
command = "grep -lrE 'it_behaves_like .(#{shared_example}).' #{path}"
`#{command}`.split
end
shared_examples(shared_example_filename).each do |shared_example|
specs_to_run = specs_with_shared_example(shared_example, spec_path)
run_single_spec(specs_to_run) unless specs_to_run.empty?
end
end
def run_cucumber_scenario scenario_path
run "cucumber #{scenario_path}"
end
# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch( '^spec/spec_helper\.rb' ) { run_all_specs }
watch( '^spec/shared/.*\.rb' ) { |m| run_specs_with_shared_examples(m[0]) }
watch( '^spec/.*_spec\.rb' ) { |m| run_single_spec(m[0]) }
watch( '^app/(.*)\.rb' ) { |m| run_single_spec("spec/%s_spec.rb" % m[1]) }
watch( '^app/views/(.*)\.haml' ) { |m| run_single_spec("spec/views/%s.haml_spec.rb" % m[1]) }
watch( '^lib/(.*)\.rb' ) { |m| run_single_spec("spec/lib/%s_spec.rb" % m[1] ) }
watch( '^features/.*\.feature' ) { |m| run_cucumber_scenario(m[0]) }
# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
# Ctrl-\
Signal.trap('QUIT') do
puts " --- Running all tests ---\n\n"
run_all_specs
end
# Ctrl-C
Signal.trap('INT') { abort("\n") }
puts "Watching.."