foneandrew / ruby-spec-runner

Run specific rspec & minitest tests from within vscode
MIT License
9 stars 3 forks source link

Rewrite test file path? #6

Open hovissimo opened 1 year ago

hovissimo commented 1 year ago

Our project runs tests inside a docker container. When I run tests from the shell I usually use: docker-compose exec test rails test **/filename_test.rb

When I use Ruby Spec Runner I see:

/usr/local/bundle/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require': cannot load such file -- /home/hovis/dev/hiringthing/HiringThing/test/unit/lib/htlogic/htlogic_business_hooks_test.rb

The load error makes a lot of sense because this absolute path to the test file is incorrect inside docker, that's a host path. The correct absolute path inside the container is /app/test/unit/lib/htlogic/htlogic_business_hooks_test.rb.

Maybe instead of a setting to rewrite the test file path there could be a setting that specifies the location of the Rails app on the host, and then Ruby Spec Runner could just trim that string from the test file path?

If I set my Rails app dir to "/home/hovis/dev/hiringthing/HiringThing/" then the test command could try to run "test/unit/lib/htlogic/htlogic_business_hooks_test.rb" which should run perfectly.

The more future-proof setting would just be a regex that could rewrite the test path for a variety of scenarios different than mine.

Note: I'm also using a remote SSH connection in VS Code, but that doesn't seem to be causing any problems here at all. The test file path being executed is correct on/for the remote host, it's just not correct for the docker container running on the remote host.

Thoughts?

foneandrew commented 1 year ago

Hmmm I see a couple of problems that would need to be solved for this to work with running things in docker. The extension works by writing output from minitest and rspec to a file which the extension is watching for changes. So the test runner inside the container would need to be able to write to a file that the extension can watch (so I imagine this would need to be on the host). Right now those files are created and managed by an npm package that creates a file in an appropriate tmp dir of your machine. So I think setting up a directory like this would be doable in docker, but then would need to make large changes to the way the watched output file is setup and cleaned up.

The second problem is that the extension will need to be able to understand which file is being referenced by the test output. In minitest this might be doable cause we have to manually write the test path into the output ourselves, however in rspec it uses the built in json format which uses its own paths so we would need some way to map that back to the files open in vscode.

So it doesn't look like it will be be a trivial solve. What do you think of these problems given your experience with developing inside a docker container?

hovissimo commented 1 year ago

Oof.

As for the temp file the simplest solve would just be mapping/binding, but this is involved enough that I'm scared away for now.

Thanks for the feedback!

marcelkooi commented 7 months ago

Hi @foneandrew, I would like to revisit this.

Currently, I'm setting rspecCommand to docker-compose exec -T web bundle exec rspec to run the spec inside docker. Then when I hit run, it runs this command which works when I modify the file path from

docker-compose exec -T web bundle exec rspec -f p -f j --out '/path/to/output-file.json'  '/path/to/spec/graphql/types/query_type_spec.rb

to

docker-compose exec -T web bundle exec rspec -f p -f j --out '/path/to/output-file.json' 'spec/graphql/types/query_type_spec.rb

It seems like adding a Rails app dir, like @hovissimo suggested, could be a solution to fix that? Right now I'm just modifying it manually.

foneandrew commented 7 months ago

Thanks for posting the example of the changes you'd need to get it to work with docker. If it works with just that change to the test file path then I reckon we can do it. Glad the temp file path thing is not a blocker. I'd like to look into it this week if I can find the time.

foneandrew commented 7 months ago

I added a new config option in 3.9.0 that will hopefully work for both of you.