Possible to Command-Line Filter Tags From Code

20 views
Skip to first unread message

sshaw

unread,
Jul 19, 2022, 12:57:41 PM7/19/22
to rspec
If one calls with: 

    rspec -t ~foo

Is it possible to determine from the spec code, in or out of an example, that the current invocation specifies running examples without the foo tag?

I have looked at doing this at the example level via the RSpec::Core::Example instance passed to the block as well as poking around in RSpec.configuration but cannot find that foo tag has been excluded. Is it even possible? 

Thanks


Filipp Pirozhkov

unread,
Jul 19, 2022, 2:23:01 PM7/19/22
to rs...@googlegroups.com
Hi Skye,

Can you describe your use case, why would examples need to know that?

- Phil

On 19 Jul 2022, at 19:57, sshaw <skye...@gmail.com> wrote:

If one calls with: 
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/bc6d474c-d4ac-49b1-ad46-4fb278d1e6c1n%40googlegroups.com.

Jon Rowe

unread,
Jul 26, 2022, 12:13:12 PM7/26/22
to rspec
Its easier to flip this and check that foo was expected / allowed to run with a circuit breaker, but if you must know if a tag was excluded that information is available sort of via the filter manager, which is private but stable api...

sshaw

unread,
Aug 15, 2022, 10:27:42 AM8/15/22
to rspec
I need to perform costly Elasticsearch-specific setup  when it's being targeted. If one is explicitly excluding it then no need for it. 

Jon Rowe

unread,
Aug 15, 2022, 3:20:30 PM8/15/22
to rs...@googlegroups.com
If you tag your examples with something that requires this setup, then declare a context level hook, the setup will be run before example contexts (`describe` and `context` blocks) that will require it, if your code uses a circuit breaker this can mean it is only run once per suite.

```
# in your spec_helper.rb
module ExpensiveSetup
  module_function
  def perform
    @perform ||=
      begin
        # expensive setup
      end
   end
end

RSpec.configure do |config|
  config.before(:context, :expensive_elastic_search_setup => true) do
    ExpensiveSetup.perform
  end
end

# in your specs
RSpec.describe "some tricky search stuff", :expensive_elastic_search_setup => true do
 # ...
end
```
Reply all
Reply to author
Forward
0 new messages