connorshea / vscode-ruby-test-adapter

A Ruby test adapter extension for the VS Code Test Explorer
https://marketplace.visualstudio.com/items?itemName=connorshea.vscode-ruby-test-adapter
MIT License
83 stars 50 forks source link

Add support for shoulda with minitest (idk if it works with rspec, I dont use it) #130

Closed merof-code closed 2 weeks ago

merof-code commented 7 months ago

Versions

explorer v0.9.2 Ruby 3.1.4 Rails 7.2 Minitest with rails

Problem

I see that when I added should gem and added a matcher, for example

class MyModelTest < ActiveSupport::TestCase
  context 'validations' do
    should validate_presence_of(:text)
  end
end

The json that gets passed around from the vscode:minitest:list does contain the methods, but they are listed as

{
  "description": "validations should validate that the length of :text is at least 3. ",
  "full_description": "validations should validate that the length of :text is at least 3. ",
  "file_path": ".//home/merof/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/shoulda-context-2.0.0/lib/shoulda/context/context.rb",
  "full_path": "/home/merof/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/shoulda-context-2.0.0/lib/shoulda/context/context.rb",
  "line_number": 62,
  "klass": "MyModelTest",
  "method": "test_: validations should validate that the length of :text is at least 3. ",
  "runnable": "MyModelTest",
  "id": ".//home/merof/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/shoulda-context-2.0.0/lib/shoulda/context/context.rb[62]"
},

Which makes them appear in a new category names . in the menu. image

This is like a feature request.

merof-code commented 7 months ago

I went ahead, and searched for a solution. Since I don't use rspec, I will only look to the minitest code. I figured we could start by changing the way the path to file is searched

just by changing it so here

_, line = runnable.instance_method(test_name).source_location
# we get path from the runnable definition, this prevents problems
# like method defined by shoulda (returns gem path)
path, = Object.const_source_location(runnable.to_s)

this, as far as I know, should not interfere with anything, and allows the tests to show up in the UI

image

I will look around and make a pull request with at least this change only

What happened to the #90? It has a few issues, but can't it just merge anyway?

merof-code commented 7 months ago

This can be labeled as "support for Unbound methods"

adding this bit of code to the Tests#build_list

unbound_flag = runnable.instance_method(test_name).is_a?(UnboundMethod)
line = -1 if unbound_flag
...

file_tests.sort_by! { |t| t[:line_number] }
max_line = file_tests[-1]&.[](:line_number) || 0
max_line = 0 if max_line.negative?

file_tests.each do |t|
  t[:line_number] = max_line + t[:index] + 1 if t[:line_number].negative?
  t[:id] = "#{t[:file_path]}[#{t[:line_number]}]"
end

And do the running based on method name rather then its position And that would be it, if we exclude the fact that all the methods will point to a 'random' line in a file