-
-
Notifications
You must be signed in to change notification settings - Fork 753
Add config.when_first_matching_example_defined
.
#2175
Conversation
It might be trivial to implement but it seems pretty odd to someone never seeing it before (and |
@myronmarston I think this makes sense as an internal api (we could even label it public) but I think most people who are only RSpec users, as opposed to plugin authors or RSpec maintainers, won't ever use it. I think I agree with @chrisarcand in that we should surface a meaningful API to the user as the primary interface to this. In general though, having an API like this is going to be useful for plugin authors, so I'm OK with exposing it. |
0760f38
to
79c1782
Compare
If you're focused on keeping RSpec boots low by making require 'support/db'
RSpec.describe SomeClassThatUsesTheDB, :db do
# ...
end It's always felt sub-optimal to me that both the With this option, these problems can be eliminated with one line of config in RSpec.configure do |config|
config.when_first_matching_example_defined(:db) do
require 'support/db'
end
end Or, the contents of The point is, it allows me to automatically do something if any Anyhow, I agree with adding the |
Thanks for the thoughtful response; I like your example, would certainly utilize that. 😄 👍 |
This. |
# metadata is defined. If no examples are defined with matching metadata, | ||
# it will not get called at all. | ||
# | ||
# This can be used as an alternative to `before(:suite)` to define some |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
semantically, it seems weird to be running setup code at define time. before(:suite)
feels more like "they're all defined, now let's run something".
I can't think of a realistic situation where this matters though, so maybe not important.
Did you consider options like before_matching(:db, :suite)
etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you consider options like
before_matching(:db, :suite)
etc?
I did not. Actually, for before(:suite)
, extra args are ignored (although we may print a warning -- I'd have to check), so we could potentially use the extra args here for this purpose.
I'm leaning against it, though. Consider the use case I explained above -- where you want to conditionally require 'support/db'
only if there are loaded examples matching :db
. If support/db
also defines a before(:suite)
hook, and the require 'support/db'
line is executed by a before(:suite)
hook -- then I'm not sure it'll work as desired. We don't generally support you defining additional hooks while running said hooks. I think we just get the list of hooks up front, and run them, without re-querying the hook state to see if there are new ones.
Or consider the main use case that drove this PR: the new filter_run_when_matching
API we want to add. Currently before(:suite)
hooks are executed and example filtering is applied between all spec files being loaded and running the first spec, but I'm not sure which order they happen in. Suite hooks might execute first, then filtering could be applied, or vice versa. It hasn't been important before now and I don't think I want to make it important as part of this feature.
So I think I'd like to stick with when_first_matching_example_defined
but I can rewrite the docs here to no longer compare it directly to before(:suite)
. I may re-write the example to show it being used to require a support file as well.
Would that be sufficient, @xaviershay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, re-writing example to emphasize seems good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As before(:suite)
doesn't support the metadata filtering I'd +1 a rewrite here
Also 👍. I regularly build "short circuit"s into my spec helper for specific before hooks so as to execute setup "just in time" before running specs. I'd use this and I'd educate people to use it. |
79c1782
to
bfc6652
Compare
OK, I rewrote the docs and also added a cuke. Unfortunately, I can't get the cuke to pass. Specifically, the "Starting a DB transaction" and "Rolling back a DB transaction" lines are not printed, indicating the
So...I'm not sure what the best course of action is. Thoughts? #1939 relates to this; if it was fixed, I don't think this would be a problem. |
461144b
to
85897da
Compare
The use of alternate world and config instances was getting in the way of some changes I want to make related to group registration and isn't needed since we sandbox things so each spec runs with fresh world and config instances.
85897da
to
699fb0e
Compare
…nition hooks. This is important so that if you load any support files that define hooks from within a definition hook, it can apply the hooks to the already-defined example groups (as is now supported via #2189). Without this change, the current group being defined is not eligible to have newly loaded hooks applied to it.
699fb0e
to
66bdb7d
Compare
...and now it's green :). |
# We add 1 so the ids start at 1 instead of 0. This is | ||
# necessary for this branch (but not for the other one) | ||
# because we register examples and groups with the | ||
# `childeren` and `examples` collection BEFORE this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/childeren
/children
/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleaned it up post merge :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :).
Add `config.when_first_matching_example_defined`.
…defined Add `config.when_first_matching_example_defined`.
In working on implementing the solution for #1920, I realized that implementing it becomes trivial if we have a hook like
config.when_first_matching_example_defined
so I decided to start there. We could stick with just this API -- focus filtering can be setup with the desired semantics with:It's probably still worth providing
config.filter_run_when_matching
for this common case, though.Before merging this we at least need a cuke documenting it.
Thoughts, @rspec/rspec?