-
-
Notifications
You must be signed in to change notification settings - Fork 389
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
Use remove_const after specs which set constants #1138
Conversation
8e5ae4d
to
0c3a3bd
Compare
What about just ignoring those warnings for that use case? Another way would be to define I have 2 concerns with this change:
|
Redefining constants seems rather harmless in the context of MSpec, when it's not autoload constants. |
For many cases, you are right that just ignore warnings would probably have been fine. For some cases, such as when doing include and extend, then with enough repetition of a new module, you get non-linear timings and increasing memory usage. For these, ignoring the warnings is not possible for my use case. I understand your concerns, and I had them and wondered if you would accept the changes as I was doing them. The way I see it:
My question to you is, do you think the cleanups should be in |
Looking at this again, I think it would be OK to merge but we would need some kind of check otherwise it will just regress and there is no much point adding it. |
You're right. An idea could be that before the test suite runs, create a And after the suite is done, create that If needed, there could be a list of allowed changes for some special cases. |
Actually that already exists: https://github.com/ruby/spec/blob/master/.mspec.constants IOW I currently don't see a way to test this which is not a lot of overhead/complexity. I'm OK to merge this though given it's small, work already done, and seems good to cleanup anyway, could you rebase? |
# Conflicts: # core/kernel/eval_spec.rb
0c3a3bd
to
0dc5f02
Compare
Glad to hear that for the merge! Rebase is done. I'm curious about my idea, I'll still take a stab at implementing my version. Seems like a fun little side quest :) |
Well, I see now that lots of tests are defining the classes/modules directly in the Ideally, they would either be defined outside of the Otherwise, I would have had a pretty interesting solution to efficiently detect the not cleaned up constants. I'm writing it below as reference if I or someone else ever want to do something similar. Adding this to the end of spec_helper.rb:
This is not a finished solution, but the core of it. At the end of the run, opens a binding.irb where The trick now is to add this:
By running with this no-op
Now, remove the no-op
Can now simply do |
Do you have an example? I thought that would be pretty rare. Another thought that came up is there is |
To be clear, I was talking about simple class definitions. Here is a regex to search. Here is an example: https://github.com/ruby/spec/blob/master/core/kernel/public_send_spec.rb#L7 They won't create problems from re-running, but they create noise for a system that is dedicated to ensuring that tests don't add new constants without cleaning after themself. Another file with lots of such cases: https://github.com/ruby/spec/blob/master/language/class_spec.rb For the |
So I'm making a tool which runs specs many times, and specs that set consts seems to often not cleanup after themselves. This makes the repeat have annoying warnings about already defined consts.
Those constants existing between specs also seems like something that could create an interaction between them.
So I added the remove_const calls that make sense to avoid all of these warnings. Good news, no tests relied on that!