rspec / rspec.tmbundle

Textmate bundle for RSpec.
MIT License
195 stars 52 forks source link

Rails Engine support #103

Closed glennfu closed 8 years ago

glennfu commented 8 years ago

I've been experimenting with a rails Engine, and specs for it. So my structure looks like:

/thing/app/models/blah.rb
/thing/spec/spec_helper.rb
/thing/spec/models/blah_spec.rb

/thing/feed/app/models/foo.rb
/thing/feed/spec/spec_helper.rb
/thing/feed/spec/models/foo_spec.rb

When I hit CMD+R from inside foo_spec.rb, with require 'spec_helper' at the top, it loads /thing/spec/spec_helper.rb instead of /thing/feed/spec/spec_helper.rb

Note that ENV['TM_PROJECT_DIRECTORY'] is "/thing" and as far as I can tell, that gets used to decide on load paths of things from thereon out.

What should I do here? Any obvious suggestions or should I do some digging?

noniq commented 8 years ago

Hm … does this work correctly when you run the spec file from the command line?

glennfu commented 8 years ago

Yes so if I'm in the /thing/feed folder, and I run rake spec or rspec spec/models/foo_spec.rb then it runs fine.

noniq commented 8 years ago

I see. Do you have the current version of the bundle? Because since https://github.com/rspec/rspec.tmbundle/commit/a81c3f9c7395d72616ddc82ce5963c113334f7b5 RSpec is always run via shelling out, so I’m a bit puzzled that it works when run directly from the command line.

Could you provide a stripped down archive / repo of the file structure causing the problem so that I can easily reproduce it?

glennfu commented 8 years ago

https://github.com/rspec/rspec.tmbundle/commit/26d04c is the commit that I'm on, which is the latest.

Remember it runs those specs because in the shell I'm in a sub-folder with its own Rakefile and its own spec_helper.rb.

Here's a trimmed down example: https://dl.dropboxusercontent.com/u/2415512/rspec_engine_test.zip

If you cd into /testapp/feed and run rake spec the specs will pass. Open up /testapp/feed/spec/models/feed/video_spec.rb and run it within TextMate and it will fail because it's trying to load the project root's environment.

noniq commented 8 years ago

Thanks for providing the example!

Currently the bundle assumes that the TextMate project directory is the root of everything RSpec related – so RSpec will always be run from that directory, all paths will be interpreted relative to that directory and so on.

A simple (but rather unpractical) workaround is to set the project directory in TextMate to testapp/feed: Then all the engine’s specs work correctly (but you won’t be able to run the specs of the main Rails app anymore).

A possible solution would be to allow for overriding the “rspec base directory” via .tm_properties: You’d need to create testapp/feed/.tm_properties with the following contents:

TM_RSPEC_DIRECTORY=$CWD

The bundle would be changed to use ENV["TM_RSPEC_DIRECTORY"] as base directory if present (else falling back to ENV["TM_PROJECT_DIRECTORY"] as before).

Do you think this solution is worth implementing?

glennfu commented 8 years ago

That would be perfect! We could do lots of clever things to figure out the right base directory, but while it might work for examples we come up with, it won't work for the next person because their situation will likely be too different. This gives exactly the control we need to set it when appropriate without any ambiguity.

noniq commented 8 years ago

I just pushed https://github.com/rspec/rspec.tmbundle/commit/41cf1b2cf84a9579c04da658857560ad1ec20551 – please try it out and let me know if it works for you! (Note that the variable is now named TM_RSPEC_BASEDIR).

noniq commented 8 years ago

There was still a bug that should now be fixed (see https://github.com/rspec/rspec.tmbundle/commit/d229b8e04f30f0fc0e7815eaff376a2de92fb640).

noniq commented 8 years ago

Closing – please reopen if you still notice something not working as expected!