diff --git a/lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb b/lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb index f7cfa42..d06cdff 100644 --- a/lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb +++ b/lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb @@ -18,6 +18,7 @@ class RSpecFormatter def initialize(output) @output = output + @relative_path = ENV["RUBY_LSP_RSPEC_RELPATH"] end def example_started(notification) @@ -59,7 +60,13 @@ def uri_for(example) end def generate_id(example) - [example, *example.example_group.parent_groups].reverse.map(&:location).join("::") + [example, *example.example_group.parent_groups].reverse.map(&:location).map(&method(:make_relative)).join("::") + end + + def make_relative(location) + return location unless @relative_path + + location.sub(/^#{@relative_path}/, "./") end end end diff --git a/spec/rspec_formatter_spec.rb b/spec/rspec_formatter_spec.rb index 4e0e8b0..8990ba5 100644 --- a/spec/rspec_formatter_spec.rb +++ b/spec/rspec_formatter_spec.rb @@ -5,6 +5,7 @@ require "socket" require "open3" require "json" +require_relative "../lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter" RSpec.describe "RubyLsp::RSpec::RSpecFormatter" do it "sends correct LSP events during test execution" do @@ -135,4 +136,24 @@ expect(events).to eq(expected) end + + context "ID generation" do + before(:all) { @original_relpath = ENV["RUBY_LSP_RSPEC_RELPATH"] } + after { ENV["RUBY_LSP_RSPEC_RELPATH"] = @original_relpath } + + it "generates the correct ID" do + formatter = RubyLsp::RSpec::RSpecFormatter.new(double("output")) + + id = formatter.generate_id(RSpec.current_example) + expect(id).to match(%r{\.\/spec\/rspec_formatter_spec.rb:\d+::\.\/spec\/rspec_formatter_spec.rb:\d+}) + end + + it "strips a path prefix from test IDs" do + ENV["RUBY_LSP_RSPEC_RELPATH"] = "./spec/" + formatter = RubyLsp::RSpec::RSpecFormatter.new(double("output")) + + id = formatter.generate_id(RSpec.current_example) + expect(id).to match(%r{\.\/rspec_formatter_spec.rb:\d+::\.\/rspec_formatter_spec.rb:\d+}) + end + end end