-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
# require_relative "../../ruby_lsp/test_reporter" | ||
require_relative "test_reporter" | ||
|
||
# RUBYOPT="-r./lib/ruby_lsp/ruby_lsp_reporter_plugin.rb" bundle exec ruby -Itest test/fixtures/minitest_example.rb | ||
|
||
require "minitest" | ||
|
||
module Minitest | ||
module Reporters | ||
class RubyLspReporter < ::Minitest::AbstractReporter | ||
class << self | ||
#: (Hash[untyped, untyped]) -> void | ||
def minitest_plugin_init(_options) | ||
Minitest.reporter.reporters = [RubyLspReporter.new] | ||
end | ||
end | ||
|
||
#: (singleton(Minitest::Test) test_class, String method_name) -> void | ||
def prerecord(test_class, method_name) | ||
uri = uri_from_test_class(test_class, method_name) | ||
return unless uri | ||
|
||
RubyLsp::TestReporter.start_test( | ||
id: "#{test_class.name}##{method_name}", | ||
uri: uri, | ||
) | ||
end | ||
|
||
#: (Minitest::Result result) -> void | ||
def record(result) | ||
super | ||
|
||
if result.error? | ||
record_error(result) | ||
elsif result.passed? | ||
record_pass(result) | ||
elsif result.skipped? | ||
record_skip(result) | ||
elsif result.failure | ||
record_fail(result) | ||
end | ||
end | ||
|
||
#: (Minitest::Result result) -> void | ||
def record_pass(result) | ||
RubyLsp::TestReporter.record_pass( | ||
id: id_from_result(result), | ||
uri: uri_from_result(result), | ||
) | ||
end | ||
|
||
#: (Minitest::Result result) -> void | ||
def record_skip(result) | ||
RubyLsp::TestReporter.record_skip( | ||
id: id_from_result(result), | ||
message: result.failure.message, | ||
uri: uri_from_result(result), | ||
) | ||
end | ||
|
||
#: (Minitest::Result result) -> void | ||
def record_fail(result) | ||
RubyLsp::TestReporter.record_fail( | ||
id: id_from_result(result), | ||
message: result.failure.message, | ||
uri: uri_from_result(result), | ||
) | ||
end | ||
|
||
#: (Minitest::Result result) -> void | ||
def record_error(result) | ||
RubyLsp::TestReporter.record_error( | ||
id: id_from_result(result), | ||
uri: uri_from_result(result), | ||
message: result.failures.first.message, | ||
) | ||
end | ||
|
||
private | ||
|
||
#: (Minitest::Result result) -> String | ||
def id_from_result(result) | ||
"#{result.klass}##{result.name}" | ||
end | ||
|
||
#: (Minitest::Result result) -> URI::Generic | ||
def uri_from_result(result) | ||
file = result.source_location[0] | ||
absolute_path = File.expand_path(file, Dir.pwd) | ||
URI::Generic.from_path(path: absolute_path) | ||
end | ||
|
||
#: (singleton(Minitest::Test) test_class, String method_name) -> URI::Generic? | ||
def uri_from_test_class(test_class, method_name) | ||
file, _line = test_class.instance_method(method_name).source_location | ||
return unless file | ||
|
||
return if file.start_with?("(eval at ") # test is dynamically defined | ||
|
||
absolute_path = File.expand_path(file, Dir.pwd) | ||
URI::Generic.from_path(path: absolute_path) | ||
end | ||
end | ||
end | ||
end | ||
|
||
Minitest.extensions << Minitest::Reporters::RubyLspReporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
# We are only testing the output of the runner, there's no need for to be random. | ||
Minitest::Test.i_suck_and_my_tests_are_order_dependent! | ||
|
||
class Sample < Minitest::Test | ||
def test_that_passes | ||
assert_equal(1, 1) | ||
assert_equal(2, 2) | ||
end | ||
|
||
def test_that_fails | ||
assert_equal(1, 2) | ||
end | ||
|
||
def test_that_is_pending | ||
skip("pending test") | ||
end | ||
|
||
def test_that_raises | ||
raise "oops" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module RubyLsp | ||
class MinitestTestRunnerTest < Minitest::Test | ||
def test_minitest_output | ||
plugin_path = "lib/ruby_lsp/ruby_lsp_reporter_plugin.rb" | ||
env = { "RUBYOPT" => "-r./#{plugin_path}" } | ||
_stdin, stdout, _stderr, _wait_thr = T.unsafe(Open3).popen3( | ||
env, | ||
"bundle", | ||
"exec", | ||
"ruby", | ||
"-Itest", | ||
"test/fixtures/minitest_example.rb", | ||
) | ||
stdout.binmode | ||
stdout.sync = true | ||
|
||
actual = parse_output(stdout) | ||
|
||
uri = URI::Generic.from_path(path: "#{Dir.pwd}/test/fixtures/minitest_example.rb").to_s | ||
expected = [ | ||
{ | ||
"method" => "start", | ||
"params" => { | ||
"id" => "Sample#test_that_fails", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "fail", | ||
"params" => { | ||
"id" => "Sample#test_that_fails", | ||
"message" => "--- expected\n+++ actual\n@@ -1 +1 @@\n-1\n+2\n", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "start", | ||
"params" => { | ||
"id" => "Sample#test_that_is_pending", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "skip", | ||
"params" => { | ||
"id" => "Sample#test_that_is_pending", | ||
"message" => "pending test", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "start", | ||
"params" => { | ||
"id" => "Sample#test_that_passes", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "pass", | ||
"params" => { | ||
"id" => "Sample#test_that_passes", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "start", | ||
"params" => { | ||
"id" => "Sample#test_that_raises", | ||
"uri" => uri, | ||
}, | ||
}, | ||
{ | ||
"method" => "error", | ||
"params" => { | ||
"id" => "Sample#test_that_raises", | ||
"message" => "RuntimeError: oops\n test/fixtures/minitest_example.rb:23:in 'Sample#test_that_raises'", | ||
"uri" => uri, | ||
}, | ||
}, | ||
] | ||
assert_equal(8, actual.size) | ||
assert_equal(expected, actual) | ||
end | ||
|
||
private | ||
|
||
def parse_output(output) | ||
result = [] | ||
while (headers = output.gets("\r\n\r\n")) | ||
content_length = headers[/Content-Length: (\d+)/i, 1] | ||
flunk("Error reading response") unless content_length | ||
data = output.read(Integer(content_length)) | ||
json = JSON.parse(T.must(data)) | ||
result << json | ||
end | ||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters