-
Notifications
You must be signed in to change notification settings - Fork 91
/
run_script.rb
executable file
·145 lines (131 loc) · 5.38 KB
/
run_script.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
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
require "#{ENV["TM_SUPPORT_PATH"]}/lib/tm/executor"
require "#{ENV["TM_SUPPORT_PATH"]}/lib/tm/tempfile"
require "pathname"
def save_untitled_document
ENV['TM_FILEPATH'] = TextMate::IO.tempfile('rb').path
ENV['TM_FILENAME'] = File.basename(ENV['TM_FILEPATH'])
ENV['TM_FILE_IS_UNTITLED'] = 'true'
begin
Dir.chdir(File.dirname(ENV["TM_FILEPATH"]))
open(ENV['TM_FILEPATH'], 'w') { |io| io << STDIN.read }
rescue e
abort "Failed to save document as ‘#{ENV['TM_FILEPATH']}’: #{e}"
end
end
save_untitled_document if ENV['TM_FILEPATH'].nil?
# For Run focused unit test, find the name of the test the user wishes to run.
args = [ ]
if ARGV.first == "--name="
n = ENV['TM_LINE_NUMBER'].to_i
spec, context, name, test_name = nil, nil, nil
File.open(ENV['TM_FILEPATH']) do |f|
# test/unit
lines = f.read.split("\n")[0...n].reverse
name = lines.find { |line| line =~ /^\s*def test[_a-z0-9]*[\?!]?/i }.to_s.sub(/^\s*def (.*?)\s*$/) { $1 }
# test helper
test_name = $2 || $3 if lines.find { |line| line =~ /^\s*test\s+('(.*)'|"(.*)")+\s*(\{|do)/ }
# test/spec.
spec = $3 || $4 if lines.find { |line| line =~ /^\s*(specify|it)\s+('(.*)'|"(.*)")+\s*(\{|do)/ }
context = $3 || $4 if lines.find { |line| line =~ /^\s*(context|describe)\s+('(.*)'|"(.*)")+\s*(\{|do)/ }
end
if name and !name.empty?
args << "--name=#{name}"
elsif test_name and !test_name.empty?
args << "--name=test_#{test_name.gsub(/\s+/,'_')}"
elsif spec and !spec.empty? and context and !context.empty?
args << %Q{--name="/test_spec \\{.*#{context}\\} \\d{3} \\[#{spec}\\]/"}
else
puts "Error: This doesn't appear to be a TestCase or spec."
exit
end
end
is_test_script = !(ENV["TM_FILEPATH"].match(/(?:\b|_)(?:tc|ts|test)(?:\b|_)/).nil? and
File.read(ENV["TM_FILEPATH"]).match(/\brequire\b.+(?:test\/unit|test_helper)/).nil?)
cmd = [ENV['TM_RUBY'] || 'ruby', '-KU', '-rcatch_exception']
if is_test_script and not ENV['TM_FILE_IS_UNTITLED']
path_ary = ENV['TM_FILEPATH'].split("/")
if index = path_ary.rindex("test")
test_path = "#{File.join(*path_ary[0..index])}:#{File.join(*path_ary[0..-2])}"
lib_path = File.join( *( path_ary[0..-2] +
[".."] * (path_ary.length - index - 1) ) +
["lib"] )
if File.exist? lib_path
cmd << "-I#{lib_path}:#{test_path}"
else
cmd << "-I#{test_path}"
end
end
end
cmd << ENV["TM_FILEPATH"]
def path_to_url_chunk(path)
unless path == "untitled"
prefix = ''
2.times do
begin
file = Pathname.new(prefix + path).realpath.to_s
return "url=file://#{e_url(file)}&"
rescue Errno::ENOENT
# Hmm lets try to prefix with project directory
prefix = "#{ENV['TM_PROJECT_DIRECTORY']}/"
end
end
else
''
end
end
def actual_path_name(path)
prefix = ''
2.times do
begin
file = Pathname.new(prefix + path).realpath.to_s
url = '&url=file://' + e_url(file)
display_name = File.basename(file)
return file, url, display_name
rescue Errno::ENOENT
# Hmm lets try to prefix with project directory
prefix = "#{ENV['TM_PROJECT_DIRECTORY']}/"
end
end
return path, '', path
end
TextMate::Executor.run( cmd, :version_args => ["--version"],
:script_args => args ) do |line, type|
if is_test_script and type == :out
if line =~ /\A[.EF]+\Z/
line.gsub!(/([.])/, "<span class=\"test ok\">\\1</span>")
line.gsub!(/([EF])/, "<span class=\"test fail\">\\1</span>")
line + "<br/>\n"
else
if line =~ /^(\s+)(\S.*?):(\d+)(?::in\s*`(.*?)')?/
indent, file, line, method = $1, $2, $3, $4
url, display_name = '', 'untitled document';
unless file == "untitled"
indent += " " if file.sub!(/^\[/, "")
if file == '(eval)'
display_name = file
else
file, url, display_name = actual_path_name(file)
end
end
out = indent
out += "<a class='near' href='txmt://open?line=#{line + url}'>" unless url.empty?
out += (method ? "method #{CGI::escapeHTML method}" : '<em>at top level</em>')
out += "</a>" unless url.empty?
out += " in <strong>#{CGI::escapeHTML display_name}</strong> at line #{line}<br/>"
out
elsif line =~ /test\_(should\_[\w\_]+)\((\w+)\)\s+\[([\w\_\/\.]+)\:(\d+)\]\:/ # shoulda 2.11.3 output test_should_fulfill(SomeTest) [test/unit/some_test.rb:42]:
spec, mod, file, line = $1, $2, $3, $4
spec.gsub!('_',' ')
"<span><a href=\"txmt://open?#{path_to_url_chunk(file)}line=#{line}\">#{mod}: #{spec}</a></span>:#{line}<br/>"
elsif line =~ /(\[[^\]]+\]\([^)]+\))\s+\[([\w\_\/\.]+)\:(\d+)\]/ # [spec](file) some text [function_name:line_no]
spec, file, line = $1, $2, $3, $4
"<span><a href=\"txmt://open?#{path_to_url_chunk(file)}line=#{line}\">#{spec}</a></span>:#{line}<br/>"
elsif line =~ /([\w\_]+).*\[([\w\_\/\.]+)\:(\d+)\]/ # whatever_message....[function_name/.whatever:line_no]
method, file, line = $1, $2, $3
"<span><a href=\"txmt://open?#{path_to_url_chunk(file)}line=#{line}\">#{method}</a></span>:#{line}<br/>"
elsif line =~ /^\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors\b.*/
"<div class=\"test #{$1 + $2 == "00" ? "ok" : "fail"}\">#{$&}</div>\n"
end
end
end
end