Skip to content

Commit 9e9ce12

Browse files
committed
Include line numbers when reporting test start event
1 parent b6f2ed0 commit 9e9ce12

5 files changed

Lines changed: 28 additions & 44 deletions

File tree

lib/ruby_lsp/test_reporters/lsp_reporter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ def internal_shutdown
5959
@io.close
6060
end
6161

62-
#: (id: String, uri: URI::Generic) -> void
63-
def start_test(id:, uri:)
64-
send_message("start", id: id, uri: uri.to_s)
62+
#: (id: String, uri: URI::Generic, line: Integer) -> void
63+
def start_test(id:, uri:, line:)
64+
send_message("start", id: id, uri: uri.to_s, line: line)
6565
end
6666

6767
#: (id: String, uri: URI::Generic) -> void

lib/ruby_lsp/test_reporters/minitest_reporter.rb

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,21 @@ def minitest_plugin_init(_options)
5151

5252
#: (singleton(Minitest::Test) test_class, String method_name) -> void
5353
def prerecord(test_class, method_name)
54-
uri = uri_from_test_class(test_class, method_name)
55-
return unless uri
54+
file_path, line = test_class.instance_method(method_name).source_location
55+
return unless file_path
56+
return if file_path.start_with?("(eval at ")
5657

57-
LspReporter.instance.start_test(id: "#{test_class.name}##{method_name}", uri: uri)
58+
uri = URI::Generic.from_path(path: File.expand_path(file_path))
59+
LspReporter.instance.start_test(id: "#{test_class.name}##{method_name}", uri: uri, line: (line || 1) - 1)
5860
end
5961

6062
#: (Minitest::Result result) -> void
6163
def record(result)
6264
id = "#{result.klass}##{result.name}"
63-
uri = uri_from_result(result)
65+
file_path, _line = result.source_location
66+
return unless file_path
67+
68+
uri = URI::Generic.from_path(path: File.expand_path(file_path))
6469

6570
if result.error?
6671
message = result.failures.first.message
@@ -79,26 +84,6 @@ def record(result)
7984
def report
8085
LspReporter.instance.shutdown
8186
end
82-
83-
private
84-
85-
#: (Minitest::Result result) -> URI::Generic
86-
def uri_from_result(result)
87-
file = result.source_location[0]
88-
absolute_path = File.expand_path(file, Dir.pwd)
89-
URI::Generic.from_path(path: absolute_path)
90-
end
91-
92-
#: (singleton(Minitest::Test) test_class, String method_name) -> URI::Generic?
93-
def uri_from_test_class(test_class, method_name)
94-
file, _line = test_class.instance_method(method_name).source_location
95-
return unless file
96-
97-
return if file.start_with?("(eval at ") # test is dynamically defined
98-
99-
absolute_path = File.expand_path(file, Dir.pwd)
100-
URI::Generic.from_path(path: absolute_path)
101-
end
10287
end
10388
end
10489

lib/ruby_lsp/test_reporters/test_unit_reporter.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ def initialize(suite, options = {})
2626
def test_started(test)
2727
super
2828

29-
current_test = test
30-
@current_uri = uri_for_test(current_test)
31-
return unless @current_uri
29+
file_path, line = test.method(test.method_name).source_location
30+
return unless file_path
31+
return if file_path.start_with?("(eval at ")
3232

33-
@current_test_id = "#{current_test.class.name}##{current_test.method_name}"
34-
LspReporter.instance.start_test(id: @current_test_id, uri: @current_uri)
33+
@current_uri = URI::Generic.from_path(path: File.expand_path(file_path))
34+
@current_test_id = "#{test.class.name}##{test.method_name}"
35+
36+
LspReporter.instance.start_test(id: @current_test_id, uri: @current_uri, line: line - 1)
3537
end
3638

3739
#: (::Test::Unit::TestCase test) -> void
@@ -62,18 +64,6 @@ def finished(elapsed_time)
6264
LspReporter.instance.shutdown
6365
end
6466

65-
#: (::Test::Unit::TestCase test) -> URI::Generic?
66-
def uri_for_test(test)
67-
location = test.method(test.method_name).source_location
68-
return unless location
69-
70-
file, _line = location
71-
return if file.start_with?("(eval at ")
72-
73-
absolute_path = File.expand_path(file, Dir.pwd)
74-
URI::Generic.from_path(path: absolute_path)
75-
end
76-
7767
#: -> void
7868
def attach_to_mediator
7969
# Events we care about

test/test_reporters/minitest_reporter_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_minitest_output
5656
"params" => {
5757
"id" => "SampleTest#test_that_fails",
5858
"uri" => uri,
59+
"line" => 14,
5960
},
6061
},
6162
{
@@ -71,6 +72,7 @@ def test_minitest_output
7172
"params" => {
7273
"id" => "SampleTest#test_that_is_pending",
7374
"uri" => uri,
75+
"line" => 18,
7476
},
7577
},
7678
{
@@ -85,6 +87,7 @@ def test_minitest_output
8587
"params" => {
8688
"id" => "SampleTest#test_that_passes",
8789
"uri" => uri,
90+
"line" => 9,
8891
},
8992
},
9093
{
@@ -99,6 +102,7 @@ def test_minitest_output
99102
"params" => {
100103
"id" => "SampleTest#test_that_raises",
101104
"uri" => uri,
105+
"line" => 22,
102106
},
103107
},
104108
{
@@ -114,6 +118,7 @@ def test_minitest_output
114118
"params" => {
115119
"id" => "SampleTest#test_with_output",
116120
"uri" => uri,
121+
"line" => 26,
117122
},
118123
},
119124
{

test/test_reporters/test_unit_reporter_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def test_test_runner_output
5959
"params" => {
6060
"id" => "SampleTest#test_that_fails",
6161
"uri" => uri,
62+
"line" => 11,
6263
},
6364
},
6465
{
@@ -74,6 +75,7 @@ def test_test_runner_output
7475
"params" => {
7576
"id" => "SampleTest#test_that_is_pending",
7677
"uri" => uri,
78+
"line" => 15,
7779
},
7880
},
7981
{
@@ -88,6 +90,7 @@ def test_test_runner_output
8890
"params" => {
8991
"id" => "SampleTest#test_that_passes",
9092
"uri" => uri,
93+
"line" => 6,
9194
},
9295
},
9396
{
@@ -102,6 +105,7 @@ def test_test_runner_output
102105
"params" => {
103106
"id" => "SampleTest#test_that_raises",
104107
"uri" => uri,
108+
"line" => 19,
105109
},
106110
},
107111
{

0 commit comments

Comments
 (0)