Skip to content

Organize test reporters into their own directory #3398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Sorbet/TrueSigil:
- "lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb"
- "lib/ruby_lsp/load_sorbet.rb"
- "lib/ruby_lsp/scripts/compose_bundle.rb"
- "lib/ruby_lsp/test_unit_test_runner.rb"
Exclude:
- "**/*.rake"
- "lib/**/*.rb"
Expand All @@ -68,9 +67,6 @@ Sorbet/StrictSigil:
- "lib/ruby_lsp/load_sorbet.rb"
- "lib/ruby_lsp/scripts/compose_bundle.rb"
- "lib/ruby_lsp/test_helper.rb"
# runs as part of host app where sorbet-runtime may not be available. Using RBS signatures we can get to
# `typed: true` but not yet `typed: strict`
- "lib/ruby_lsp/test_unit_test_runner.rb"

Layout/ClassStructure:
Enabled: true
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_lsp/listeners/test_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def handle_test_unit_groups(file_path, groups_and_examples)

include Requests::Support::Common

MINITEST_REPORTER_PATH = File.expand_path("../ruby_lsp_reporter_plugin.rb", __dir__) #: String
TEST_UNIT_REPORTER_PATH = File.expand_path("../test_unit_test_runner.rb", __dir__) #: String
MINITEST_REPORTER_PATH = File.expand_path("../test_reporters/minitest_reporter.rb", __dir__) #: String
TEST_UNIT_REPORTER_PATH = File.expand_path("../test_reporters/test_unit_reporter.rb", __dir__) #: String
ACCESS_MODIFIERS = [:public, :private, :protected].freeze
BASE_COMMAND = begin
Bundler.with_original_env { Bundler.default_lockfile }
Expand Down
77 changes: 0 additions & 77 deletions lib/ruby_lsp/ruby_lsp_reporter_plugin.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require "singleton"

module RubyLsp
class TestReporter
class LspReporter
include Singleton

#: -> void
Expand Down Expand Up @@ -146,7 +146,7 @@ def send_message(method_name, **params)
Coverage.start(:all)

at_exit do
coverage_results = RubyLsp::TestReporter.instance.gather_coverage_results
coverage_results = RubyLsp::LspReporter.instance.gather_coverage_results
File.write(File.join(".ruby-lsp", "coverage_result.json"), coverage_results.to_json)
end
end
75 changes: 75 additions & 0 deletions lib/ruby_lsp/test_reporters/minitest_reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# typed: strict
# frozen_string_literal: true

begin
require "minitest"
rescue LoadError
return
end

require_relative "lsp_reporter"
require "ruby_indexer/lib/ruby_indexer/uri"

module RubyLsp
class MinitestReporter < Minitest::AbstractReporter
class << self
#: (Hash[untyped, untyped]) -> void
def minitest_plugin_init(_options)
Minitest.reporter.reporters << MinitestReporter.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

LspReporter.instance.start_test(id: "#{test_class.name}##{method_name}", uri: uri)
end

#: (Minitest::Result result) -> void
def record(result)
id = "#{result.klass}##{result.name}"
uri = uri_from_result(result)

if result.error?
message = result.failures.first.message
LspReporter.instance.record_error(id: id, uri: uri, message: message)
elsif result.passed?
LspReporter.instance.record_pass(id: id, uri: uri)
elsif result.skipped?
LspReporter.instance.record_skip(id: id, uri: uri)
elsif result.failure
message = result.failure.message
LspReporter.instance.record_fail(id: id, uri: uri, message: message)
end
end

#: -> void
def report
LspReporter.instance.shutdown
end

private

#: (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

Minitest.extensions << RubyLsp::MinitestReporter
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

begin
Expand All @@ -9,11 +9,11 @@
return
end

require_relative "test_reporter"
require_relative "lsp_reporter"
require "ruby_indexer/lib/ruby_indexer/uri"

module RubyLsp
class TestRunner < ::Test::Unit::UI::Console::TestRunner
class TestUnitReporter < ::Test::Unit::UI::Console::TestRunner
private

#: (::Test::Unit::TestCase test) -> void
Expand All @@ -25,32 +25,35 @@ def test_started(test)
return unless @current_uri

@current_test_id = "#{current_test.class.name}##{current_test.method_name}"
TestReporter.instance.start_test(id: @current_test_id, uri: @current_uri)
LspReporter.instance.start_test(id: @current_test_id, uri: @current_uri)
end

#: (::Test::Unit::TestCase test) -> void
def test_finished(test)
super
TestReporter.instance.record_pass(id: @current_test_id, uri: @current_uri) if test.passed?
return unless test.passed? && @current_uri && @current_test_id

LspReporter.instance.record_pass(id: @current_test_id, uri: @current_uri)
end

#: (::Test::Unit::Failure | ::Test::Unit::Error | ::Test::Unit::Pending result) -> void
def add_fault(result)
super
return unless @current_uri && @current_test_id

case result
when ::Test::Unit::Failure
TestReporter.instance.record_fail(id: @current_test_id, message: result.message, uri: @current_uri)
LspReporter.instance.record_fail(id: @current_test_id, message: result.message, uri: @current_uri)
when ::Test::Unit::Error
TestReporter.instance.record_error(id: @current_test_id, message: result.message, uri: @current_uri)
LspReporter.instance.record_error(id: @current_test_id, message: result.message, uri: @current_uri)
when ::Test::Unit::Pending
TestReporter.instance.record_skip(id: @current_test_id, uri: @current_uri)
LspReporter.instance.record_skip(id: @current_test_id, uri: @current_uri)
end
end

#: (Float) -> void
def finished(elapsed_time)
TestReporter.instance.shutdown
LspReporter.instance.shutdown
end

#: (::Test::Unit::TestCase test) -> URI::Generic?
Expand Down Expand Up @@ -81,5 +84,5 @@ def attach_to_mediator
end
end

Test::Unit::AutoRunner.register_runner(:ruby_lsp) { |_auto_runner| RubyLsp::TestRunner }
Test::Unit::AutoRunner.register_runner(:ruby_lsp) { |_auto_runner| RubyLsp::TestUnitReporter }
Test::Unit::AutoRunner.default_runner = :ruby_lsp
15 changes: 14 additions & 1 deletion sorbet/rbi/shims/test_reporter.rbi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# typed: true

class RubyLsp::TestReporter
class RubyLsp::LspReporter
# https://code.visualstudio.com/api/references/vscode-api#Position
Position = T.type_alias { { line: Integer, character: Integer } }

Expand All @@ -13,3 +13,16 @@ class RubyLsp::TestReporter
# https://code.visualstudio.com/api/references/vscode-api#StatementCoverage
StatementCoverage = T.type_alias { { executed: Integer, location: Position, branches: T::Array[BranchCoverage] } }
end

class ::Test::Unit::UI::Console::TestRunner
def initialize(suite, options = T.unsafe(nil))
@mediator = T.let(T.unsafe(nil), Test::Unit::UI::TestRunnerMediator)
end
end

class RubyLsp::TestUnitReporter
def initialize(suite, options = T.unsafe(nil))
@current_uri = T.let(nil, T.nilable(URI::Generic))
@current_test_id = T.let(nil, T.nilable(String))
end
end
4 changes: 2 additions & 2 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,8 @@ def test_resolve_test_commands_returns_custom_reporters
result = find_message(RubyLsp::Result, id: 1)
reporters = result.response[:reporterPaths]

assert_includes(reporters, File.expand_path("../lib/ruby_lsp/ruby_lsp_reporter_plugin.rb", __dir__))
assert_includes(reporters, File.expand_path("../lib/ruby_lsp/test_unit_test_runner.rb", __dir__))
assert_includes(reporters, File.expand_path("../lib/ruby_lsp/test_reporters/minitest_reporter.rb", __dir__))
assert_includes(reporters, File.expand_path("../lib/ruby_lsp/test_reporters/test_unit_reporter.rb", __dir__))
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

require "test_helper"
require "coverage"
require "ruby_lsp/test_reporter"
require "ruby_lsp/test_reporters/lsp_reporter"

module RubyLsp
class TestReporterTest < Minitest::Test
class LspReporterTest < Minitest::Test
def test_coverage_results_are_formatted_as_vscode_expects
path = "/path/to/file.rb"
Coverage.expects(:result).returns({
Expand Down Expand Up @@ -49,7 +49,7 @@ def test_coverage_results_are_formatted_as_vscode_expects
},
],
},
TestReporter.instance.gather_coverage_results,
LspReporter.instance.gather_coverage_results,
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
require "test_helper"

module RubyLsp
class MinitestTestRunnerTest < Minitest::Test
class MinitestReporterTest < Minitest::Test
def test_minitest_output
plugin_path = File.expand_path("lib/ruby_lsp/ruby_lsp_reporter_plugin.rb")
plugin_path = File.expand_path("lib/ruby_lsp/test_reporters/minitest_reporter.rb")
uri = URI::Generic.from_path(path: "#{Dir.pwd}/test/fixtures/minitest_example.rb").to_s

server = TCPServer.new("localhost", 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
require "socket"

module RubyLsp
class TestUnitTestRunnerTest < Minitest::Test
class TestUnitReporterTest < Minitest::Test
def test_test_runner_output
reporter_path = File.expand_path(File.join("lib", "ruby_lsp", "test_unit_test_runner.rb"))
reporter_path = File.expand_path(File.join("lib", "ruby_lsp", "test_reporters", "test_unit_reporter.rb"))
test_path = File.join(Dir.pwd, "test", "fixtures", "test_unit_example.rb")
uri = URI::Generic.from_path(path: test_path).to_s

Expand Down
Loading