-
Notifications
You must be signed in to change notification settings - Fork 16
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
Fix 'undefined constant RED' error + add tests #25
base: master
Are you sure you want to change the base?
Conversation
Full error message was: `NameError: uninitialized constant SaferRailsConsole::Patches::Sandbox::AutoRollback::RED` Tests added: ::SaferRailsConsole::Patches::Sandbox::AutoRollback .handle_and_reraise_exception when raising a PG::ReadOnlySqlTransaction exception outputs a message on stdout and forwards the exception when raising a classic exception rollbacks, begins a new transaction and forwards the exception
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 so much for the PR! It's much appreciated. Let me know if you have any questions!
@@ -12,7 +12,7 @@ def self.rollback_and_begin_new_transaction | |||
|
|||
def self.handle_and_reraise_exception(e) | |||
if e.message.include?('PG::ReadOnlySqlTransaction') | |||
puts color_text('An operation could not be completed due to read-only mode.', RED) # rubocop:disable Rails/Output | |||
puts color_text('An operation could not be completed due to read-only mode.', Colors::RED) # rubocop:disable Rails/Output |
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.
I'd use the entire namespace from the root to be safe - ::SaferRailsConsole::Colors::RED
. The fact that RED
resolved correctly was serendipitous.
A better way to do this would be to include SaferRailsConsole::Colors
instead of extend ...
, ensuring that the constants are included into the metaclass. That'd involve a little bit of shuffling within Colors
though, so no pressure and the first solution is totally adequate!
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.
I thought about including, but then the color_text
won't be available.
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.
Yup, the shuffling I mentioned would involve putting that into a ClassMethods
or similar module and using the popular self.included; extend ClassMethods
pattern.
@@ -0,0 +1,45 @@ | |||
# require 'safer_rails_console/patches/sandbox/auto_rollback' |
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.
Currently all the patches are integration tested in a real rails setup and console. We don't require
anything within patches
outside of that. See this context https://github.com/salsify/safer_rails_console/blob/master/spec/integration/patches/sandbox_spec.rb#L8 for more information. Feel free to help improve that by including the case where a normal exception is forwarded!
That being said, I'm totally for unit testing the patches. I'd probably update the test for SaferRailsConsole::Console.initialize_sandbox
to the following:
context ".initialize_sandbox" do
before do
allow(SaferRailsConsole::Console).to receive(:require)
end
it "loads sandbox patches" do
described_class.initialize_sandbox
expect(SaferRailsConsole::Console).to have_received(:require).with('safer_rails_console/patches/sandbox')
end
end
I will do the proposed changes. Expect it on next Friday. Thanks for the feedback. |
I dug a little deeper about this issue, trying to implement it as an integration test. In fact it passes: the constant It's because the I think I got the So I am not sure if my fix is relevant anymore. For me it looks like using safer_rails_console gem should not add What are your thoughts about it? |
Happy new year - apologies for the delay in response. Thanks for taking a deeper look into it. I agree with not polluting the global namespace. The followup is to also generate a |
Happy new year to you too! I'll try to get a look this week. It should be easier for me as I am already using pry and can reproduce the issue at will. I'll try to add an integration test using pry too. |
can we merge it? |
I just got this error while using safer_rails_console today in development:
This PR should fix the error.
Tests added:
Please note that the first test in
spec/safer_rails_console/console_spec.rb
only passes because it is run before the tests I just added: I needed to load::SaferRailsConsole::Patches::Sandbox::AutoRollback
to testhandle_and_reraise_exception
method, but it makes the test "SaferRailsConsole::Console.initialize_sandbox loads sandbox patches" fail if it is run afterwards (Because it checks that the patch is not loaded before callinginitialize_sandbox
). That makes the test order-dependent and that's bad. Happy to fix it if you have some guidance here.