Skip to content
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

Support running tests in Rails engines #1274

Closed
wants to merge 2 commits into from

Conversation

tristil
Copy link

@tristil tristil commented Dec 30, 2023

Motivation

In Rails projects with engines, you need to cd into the engine in order to run a test, or else requires, etc. will not work.

Implementation

This change detects when a file has engines in its path and has a Rails dependency and alters the command to cd in (and out, afterwards) of the engine. This allows running a test and sending the test to terminal. The special characters involved break debug, which is fixed in the second commit. The second commit escapes the command when passing to rdbg.

Automated Tests

Tests added

Manual Tests

I am able to run tests in an engine using VS Code

@tristil tristil requested a review from a team as a code owner December 30, 2023 06:39
@tristil tristil requested review from Morriar and st0012 December 30, 2023 06:39
@tristil tristil force-pushed the play_well_with_engines branch from 1db5ebe to 889f2c0 Compare December 30, 2023 06:40
@st0012
Copy link
Member

st0012 commented Jan 4, 2024

@tristil have you tried installing the ruby-lsp-rails addon? With it, the code lens feature would use bin/rails test to execute the tests instead of ruby -Itest, which may take the engine setup into consideration.

@tristil
Copy link
Author

tristil commented Jan 4, 2024

@st0012 I tried it out and it didn't work. When running engine tests manually we always cd into the engine directory.

@st0012
Copy link
Member

st0012 commented Jan 4, 2024

Can you explain how it didn't work? Was the bin/rails test command didn't work, or the path was not correct, or was it something else?

@tristil
Copy link
Author

tristil commented Jan 4, 2024

@st0012 I run ./bin/rails test engines/some_engine/test/some_engine/some_file_test.rb and I get require: cannot load such file -- some_helper (LoadError) but if I cd into engines/some_engine I can run bundle exec ruby -Itest test/some_engine/some_file_test.rb without an error

@tristil
Copy link
Author

tristil commented Jan 4, 2024

Some context is that we are a big company with a Rails monolith that is divided into 40+ engines. So although it's true that we could open vscode, etc. in each engine it would be a lot of context switching especially since there are interactions between engines and the top level app.

To run a test in a Rails engine you need to cd into the engine before
running the test.
When we add commands for changing the cwd to the test command passed to
rdbg we need to start shell escaping the command.
@tristil tristil force-pushed the play_well_with_engines branch from 889f2c0 to e49c57d Compare January 12, 2024 21:24
@tristil tristil closed this Feb 14, 2024
@vinistock
Copy link
Member

Sorry about the lack of feedback. The main issue with this is solving it in a general way that works for any app using engines.

My understanding is that you don't have to put the engines under the engines directory, so assuming that will likely not work for everybody.

Also, depending on your Gemfile setup, then the files in engines are available for require from the top level, avoiding this issue.

If you discover that there is a convention about how to run tests inside engines and where to put them or if you find an alternative that works generally, please let us know.

@tristil
Copy link
Author

tristil commented Feb 14, 2024

@vinistock Sorry, I should have left a comment about why I was closing. Basically I think this is too specific to my organization's needs, plus we are moving away from engines to Packwerk packs. But: the underlying problem is that ruby-lsp wants there to be a Gemfile at the root of a project, even if there is also a .gemspec file, and in general it doesn't understand how to load an engine in isolation (or at least I couldn't figure it out). If it did know how to do these things then we could have set up our app as a multi root project as recommended and we wouldn't need special handling. IOW if it were possible for ruby-lsp to work with a single engine (or a single inline gem without an explicit Gemfile) then multi root project would have served our needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants