Skip to content
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

Allow for individual test skiping #2130

Open
aaronmallen opened this issue Dec 6, 2024 · 2 comments
Open

Allow for individual test skiping #2130

aaronmallen opened this issue Dec 6, 2024 · 2 comments

Comments

@aaronmallen
Copy link

aaronmallen commented Dec 6, 2024

It would be awesome if you could turn RBS test on and off for single unit tests.

Given the following example:

class MyClass
  attr_reader :name #: Symbol

  # @rbs (name: String | Symbol) -> void
  def initialize(name:)
    validate_name(name)
    @name = name.to_sym
  end

  private

  # @rbs (untyped name) -> void
  def validate_name(name)
    raise ArgumentError, "Invalid name: #{name}" unless name.is_a?(String) || name.is_a?(Symbol)
  end
end

and the following test:

RSpec.describe MyClass do
  describe '.new' do
    subject(:my_class) { described_class.new(name) }
  
   # RBS test needs to skip this context as we're intentionally violating types here.
    context 'when given an invalid name' do
      let(:name) { 123 }

      it { expect { my_class }.to raise_error(ArgumentError, /Invalid name/) }
    end

    # RBS test should not skip this context
    context 'when given a valid name' do
      let(:name) { 'foo' }

      it { expect { my_class }.not_to raise_error }

      it 'is expected to set the name' do
        expect(my_class).to have_attributes(name: :foo)
      end
    end
  end
end

While it's great that RBS can catch these type errors its only really beneficial if those who use your gem as a dependency are also using RBS on their project. So we still need to write in protections like this but are unable to test them because the test in this example will raise an RBS TypeError. Additionally we don't want to completely turn off RBS for the initialize method with the %a{rbs:test:skip} annotation as it's just this single test that will raise the error.

The obvious work around here is to have the name argument in the initializer be untyped however that reduces the clarity of the signature making the end user believe they may pass whatever they would like for name to the initializer.

@ksss
Copy link
Collaborator

ksss commented Dec 7, 2024

If you're using RSpec, the aws-sdk-ruby library utilizes tags to skip test cases. Consider trying it out.

https://github.com/aws/aws-sdk-ruby/blob/60bd46cf9e79bb03930230a7f37e5c8540ece0c2/tasks/rbs.rake#L24

@aaronmallen
Copy link
Author

If you're using RSpec, the aws-sdk-ruby library utilizes tags to skip test cases. Consider trying it out.

https://github.com/aws/aws-sdk-ruby/blob/60bd46cf9e79bb03930230a7f37e5c8540ece0c2/tasks/rbs.rake#L24

This is a FANTASTIC work around, my only complaint is having to run the test suite twice (once for all specs, and once with RBS using exclusion of the tag).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants