-
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
12 changed files
with
5,747 additions
and
60 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
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,71 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "json" | ||
|
||
module RubyLsp | ||
module TestReporter | ||
class << self | ||
#: (id: String, file: String) -> void | ||
def start_test(id:, file:) | ||
result = { | ||
event: "start", | ||
id: id, | ||
file: file, | ||
} | ||
send_message(result) | ||
end | ||
|
||
#: (id: String, file: String) -> void | ||
def record_pass(id:, file:) | ||
result = { | ||
event: "pass", | ||
id: id, | ||
file: file, | ||
} | ||
send_message(result) | ||
end | ||
|
||
#: (id: String, type: untyped, message: String, file: String) -> void | ||
def record_fail(id:, type:, message:, file:) | ||
result = { | ||
event: "fail", | ||
id: id, | ||
type: type, | ||
message: message, | ||
file: file, | ||
} | ||
send_message(result) | ||
end | ||
|
||
#: (id: String, message: String?, file: String) -> void | ||
def record_skip(id:, message:, file:) | ||
result = { | ||
event: "skip", | ||
id: id, | ||
message: message, | ||
file: file, | ||
} | ||
send_message(result) | ||
end | ||
|
||
#: (id: String, message: String?, file: String) -> void | ||
def record_error(id:, message:, file:) | ||
result = { | ||
event: "error", | ||
id: id, | ||
message: message, | ||
file: file, | ||
} | ||
send_message(result) | ||
end | ||
|
||
private | ||
|
||
def send_message(result) | ||
json_message = result.to_json | ||
$stdout.write("Content-Length: #{json_message.bytesize}\r\n\r\n#{json_message}") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "test/unit" | ||
require "test/unit/ui/testrunner" | ||
require "stringio" | ||
require "ruby_lsp/test_reporter" | ||
|
||
module RubyLsp | ||
class TestRunner < ::Test::Unit::UI::TestRunner | ||
#: (::Test::Unit::TestCase test) -> void | ||
def test_started(test) | ||
current_test = test | ||
@current_file = file_for_test(current_test) | ||
@current_test_id = "#{current_test.class.name}##{current_test.method_name}" | ||
result = { | ||
id: @current_test_id, | ||
file: @current_file, | ||
} | ||
TestReporter.start_test(**result) | ||
end | ||
|
||
#: (::Test::Unit::TestCase test) -> void | ||
def test_finished(test) | ||
if test.passed? | ||
# tests with an Omission are still marked as passed, which seems strange | ||
# return if test.instance_variable_get("@_result").faults.any? | ||
|
||
result = { | ||
id: @current_test_id, | ||
file: @current_file, | ||
} | ||
TestReporter.record_pass(**result) | ||
end | ||
end | ||
|
||
#: (::Test::Unit::Failure | ::Test::Unit::Omission | ::Test::Unit::Error result) -> void | ||
def result_fault(result) | ||
case result | ||
when ::Test::Unit::Failure | ||
record_failure(result) | ||
when ::Test::Unit::Error | ||
record_error(result) | ||
when ::Test::Unit::Pending | ||
record_skip(result) | ||
end | ||
end | ||
|
||
#: (::Test::Unit::Failure failure) -> void | ||
def record_failure(failure) | ||
result = { | ||
id: @current_test_id, | ||
type: failure.class.name, | ||
message: failure.message, | ||
file: @current_file, | ||
} | ||
TestReporter.record_fail(**result) | ||
end | ||
|
||
#: (::Test::Unit::Error error) -> void | ||
def record_error(error) | ||
result = { | ||
id: @current_test_id, | ||
message: error.message, | ||
file: @current_file, | ||
} | ||
TestReporter.record_error(**result) | ||
end | ||
|
||
#: (::Test::Unit::Omission omission) -> void | ||
def record_skip(omission) | ||
result = { | ||
id: @current_test_id, | ||
message: omission.message, | ||
file: @current_file, | ||
} | ||
TestReporter.record_skip(**result) | ||
end | ||
|
||
#: (::Test::Unit::TestCase test) -> String | ||
def file_for_test(test) | ||
location = test.method(test.method_name).source_location | ||
return "" unless location # TODO: when might this be nil? | ||
|
||
file, _line = location | ||
return "" if file.start_with?("(eval at ") # test is dynamically defined (TODO: better way to check?) | ||
|
||
file | ||
end | ||
|
||
#: -> void | ||
def attach_to_mediator | ||
@mediator.add_listener(Test::Unit::TestResult::FAULT, &method(:result_fault)) | ||
@mediator.add_listener(Test::Unit::TestCase::STARTED_OBJECT, &method(:test_started)) | ||
@mediator.add_listener(Test::Unit::TestCase::FINISHED_OBJECT, &method(:test_finished)) | ||
end | ||
end | ||
end | ||
|
||
Test::Unit::AutoRunner.register_runner(:ruby_lsp) { |_auto_runner| RubyLsp::TestRunner } |
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
Oops, something went wrong.