-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add :on_rails hook to rspec #15
Conversation
WalkthroughRecent updates introduce a new workflow to work with a dummy Rails environment within tests, by adding the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
FileUtils.remove_entry(tmp_dir) | ||
end | ||
|
||
describe '#on_rails', skip: 'Conflicts with manager in rspec_settings' do |
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.
It seems that RailsManager#unload_rails!
isn't fully unloads rails application. Running this test separately works fine, but running all tests at once raises issue
If we run bundle exec rspec
2 rails applications will be initialized (first one when this test is run, second one is when test with :on_rails
hook is executed), and error will be raised.
I couldn't figure out how to fix this issue, maybe i/o creating hook we should just run tests on rails and non-rails application environment 🤔 @borela WDYT?
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.
Actionable comments posted: 3
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (7)
- Gemfile (1 hunks)
- Rakefile (1 hunks)
- spec/rspec_settings.rb (1 hunks)
- spec/support/rails_manager.rb (1 hunks)
- spec/support_specs/rails_manager_spec.rb (1 hunks)
- spec/uber_task/internal/path_spec.rb (1 hunks)
- spec/uber_task/logger_spec.rb (1 hunks)
Additional comments not posted (13)
Gemfile (1)
12-12
: Approved: Addition of Rails dependency for testing.The addition of
rails
version~> 7.1.1
in the test group is appropriate for running tests in a Rails environment.spec/rspec_settings.rb (3)
6-6
: Approved: Addition of SimpleCov filter for/tmp/
.The addition of the filter
%r{^/tmp/}
to SimpleCov is appropriate for excluding temporary directories from coverage reports.
12-13
: Approved: Requirerails_manager
for RSpec configuration.The require statement for
support/rails_manager
is necessary for the new RSpec around hook.
18-21
: Approved: Addition of RSpec around hook for:on_rails
.The new around hook for examples tagged with
:on_rails
is a good addition to manage Rails-specific tests.However, ensure that all relevant tests are appropriately tagged with
:on_rails
to utilize this hook.spec/uber_task/internal/path_spec.rb (2)
28-28
: Approved: Addition of'on rails'
context with:on_rails
tag.The new context
'on rails'
with the:on_rails
tag is appropriate for testing Rails-specific paths.
31-31
: Approved: Use ofRails.root.join
for path.The usage of
Rails.root.join('abc').to_s
is correct and aligns with Rails best practices.Rakefile (4)
5-5
: Approved: Addition of default task to run RuboCop and RSpec.The addition of the default task to run RuboCop and RSpec is appropriate for ensuring code quality and test coverage.
7-12
: Approved: Addition of RuboCop task.The addition of the RuboCop task is appropriate for running static code analysis.
14-19
: Approved: Addition of RSpec task.The addition of the RSpec task is appropriate for running tests.
21-37
: Approved: Addition ofrails
namespace with task to generate dummy Rails application.The addition of the
rails
namespace with a task to generate a dummy Rails application is appropriate for setting up a Rails environment for testing.However, ensure that the generated dummy Rails application is correctly configured and does not interfere with existing tests.
spec/support/rails_manager.rb (1)
10-13
: LGTM!The initialize method correctly sets up the configuration and application path.
spec/uber_task/logger_spec.rb (1)
25-30
: LGTM!The new context correctly tests the behavior when Rails logger is defined.
spec/support_specs/rails_manager_spec.rb (1)
5-41
: LGTM!The test suite correctly tests the functionality of the
on_rails
method.However, there is a known issue with unloading Rails. Consider addressing this issue to ensure tests run correctly in all scenarios.
def unload_rails! | ||
ENV.delete('RAILS_ROOT') | ||
Object.send(:remove_const, :Rails) | ||
end |
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.
Check if Rails is defined before removing the constant.
Add a check to ensure Rails is defined before attempting to remove the constant.
- Object.send(:remove_const, :Rails)
+ Object.send(:remove_const, :Rails) if defined?(Rails)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def unload_rails! | |
ENV.delete('RAILS_ROOT') | |
Object.send(:remove_const, :Rails) | |
end | |
def unload_rails! | |
ENV.delete('RAILS_ROOT') | |
Object.send(:remove_const, :Rails) if defined?(Rails) | |
end |
def on_rails | ||
load_rails! | ||
yield | ||
unload_rails! | ||
end |
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.
Consider adding error handling.
Ensure that load_rails!
and unload_rails!
handle potential errors gracefully.
- load_rails!
- yield
- unload_rails!
+ begin
+ load_rails!
+ yield
+ ensure
+ unload_rails!
+ end
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def on_rails | |
load_rails! | |
yield | |
unload_rails! | |
end | |
def on_rails | |
begin | |
load_rails! | |
yield | |
ensure | |
unload_rails! | |
end | |
end |
def load_rails! | ||
return if defined?(Rails) | ||
|
||
if config.instance_variable_defined?(:@rails) | ||
Object.const_set(:Rails, config.instance_variable_get(:@rails)) | ||
return | ||
end | ||
|
||
absolute_app_path = File.expand_path(app_path, __dir__) | ||
system("rake rails:generate_dummy_app[#{absolute_app_path}]") | ||
|
||
require File.expand_path( | ||
File.join(absolute_app_path, '/config/environment.rb'), | ||
__dir__, | ||
) | ||
|
||
ENV['RAILS_ROOT'] = absolute_app_path | ||
config.instance_variable_set(:@rails, Object.const_get(:Rails)) | ||
end |
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.
Ensure the system call succeeds.
Add error handling for the system call that generates the dummy Rails app.
- system("rake rails:generate_dummy_app[#{absolute_app_path}]")
+ unless system("rake rails:generate_dummy_app[#{absolute_app_path}]")
+ raise "Failed to generate dummy Rails app at #{absolute_app_path}"
+ end
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def load_rails! | |
return if defined?(Rails) | |
if config.instance_variable_defined?(:@rails) | |
Object.const_set(:Rails, config.instance_variable_get(:@rails)) | |
return | |
end | |
absolute_app_path = File.expand_path(app_path, __dir__) | |
system("rake rails:generate_dummy_app[#{absolute_app_path}]") | |
require File.expand_path( | |
File.join(absolute_app_path, '/config/environment.rb'), | |
__dir__, | |
) | |
ENV['RAILS_ROOT'] = absolute_app_path | |
config.instance_variable_set(:@rails, Object.const_get(:Rails)) | |
end | |
def load_rails! | |
return if defined?(Rails) | |
if config.instance_variable_defined?(:@rails) | |
Object.const_set(:Rails, config.instance_variable_get(:@rails)) | |
return | |
end | |
absolute_app_path = File.expand_path(app_path, __dir__) | |
unless system("rake rails:generate_dummy_app[#{absolute_app_path}]") | |
raise "Failed to generate dummy Rails app at #{absolute_app_path}" | |
end | |
require File.expand_path( | |
File.join(absolute_app_path, '/config/environment.rb'), | |
__dir__, | |
) | |
ENV['RAILS_ROOT'] = absolute_app_path | |
config.instance_variable_set(:@rails, Object.const_get(:Rails)) | |
end |
PR #16 was opened with new approach |
What does this PR do?
This PR adds
:on_rails
hook to rspec config so test cases can be run as if onrails
applicationsSummary by CodeRabbit
New Features
RailsManager
to manage and customize Rails applications in tests.Improvements
Testing
RailsManager
.Chores
rubocop
andspec
tasks into the default Rake task.