/
runner.rb
86 lines (73 loc) · 2.58 KB
/
runner.rb
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
require 'rspec'
require 'guard/jruby-rspec/formatters/notification_rspec'
module Guard
class JRubyRSpec
class Runner
def initialize(options = {})
@options = {
:cli => [],
:notification => true
}.merge(options)
@pipefile = options[:pipefile]
@pipefile ||= ".guard-jruby-rspec-pipe"
cleanup
end
def cleanup
File.delete(@pipefile) if File.exists?(@pipefile)
end
def run(paths, options = {})
return false if paths.empty?
message = options[:message] || "Running: #{paths.join(' ')}"
UI.info(message, :reset => true)
# it might be a problem to run Rspec within this runtime. Might have to create an
# embedded jruby.
if File.exists?(@pipefile)
raise "not supported yet"
# instead of writing to the pipefile, we should probably use a
# formatter and write to /dev/null
# orig_stdout = $stdout.clone
# orig_stderr = $stderr.clone
# begin
# $stdout.reopen(@pipefile, "w")
# $stderr.reopen(@pipefile, "w")
# ::RSpec::Core::Runner.run(paths)
# ensure
# $stdout.reopen(orig_stdout)
# $stderr.reopen(orig_stderr)
# end
else
begin
::RSpec::Core::Runner.run(rspec_arguments(paths, @options))
rescue SyntaxError => e
UI.error e.message
end
end
end
def parsed_or_default_formatter
@parsed_or_default_formatter ||= begin
file_name = "#{Dir.pwd}/.rspec"
parsed_formatter = if File.exist?(file_name)
formatters = File.read(file_name).scan(formatter_regex).flatten
formatters.map { |formatter| "-f#{formatter}" }.join(' ')
end
parsed_formatter.nil? || parsed_formatter.empty? ? '-fprogress' : parsed_formatter
end
end
private
def rspec_arguments(paths, options)
arg_parts = []
arg_parts.concat(options[:cli]) if options[:cli]
if @options[:notification]
arg_parts << parsed_or_default_formatter unless options[:cli] =~ formatter_regex
arg_parts << "-fGuard::JRubyRSpec::Formatter::NotificationRSpec"
arg_parts << "-o/dev/null"
end
#arg_parts << "--failure-exit-code #{FAILURE_EXIT_CODE}" if failure_exit_code_supported?
arg_parts.concat(paths)
end
def formatter_regex
@formatter_regex ||= /(?:^|\s)(?:-f\s*|--format(?:=|\s+))([\w:]+)/
end
end
end
end