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

Fix spec_dir lookup when Dir.pwd is not Rails.root #1

Closed
wants to merge 1 commit into from

Conversation

kurko
Copy link
Owner

@kurko kurko commented Feb 4, 2018

Context

I was able to get this gem to work with my engine by going
inside the test_app dir and running

bundle exec rails spec:javascript

So far so good. The problem is that I don't want to have to run
cd spec/test_app every time to run my Javascript tests. I want to run them
from the engine's grandparent directory so I can include it in my Rakefile for
the entire test suite. In other words, this is my dir tree:

~/engine/
~/engine/spec/javascripts/
~/engine/spec/test_app/spec/javascripts/support/jasmine.yml

Rails.root is always ~/engine/spec/test_app/, regardless of whether
I'm in `~/engine/ or not.

The problem

Look at this line:

[paths].flatten.map { |path| Dir.glob path }.flatten.collect { |path| Rails.root.join(path) }

Dir.glob path, equivalent to Dir.glob("spec/javascripts") returns
true because Dir.pwd is ~/engine/, therefore
~/engine/spec/javascripts exists.

However, the last collect has Rails.root.join("spec_javascript),
which will map to ~/engine/spec/test_app/spec/javascripts, but
there's nothing there (because my test files were configured to
~/engine/spec/javascript).

To "fix that, I put ../javascripts in my yml's spec_dir, but no Dir.glob
just returns false because, in fact, ~/engine/../javascripts doesn't exist.
So, the code is verifying one path and resolving to some other.

The fix

-      [paths].flatten.map { |path| Dir.glob path }.flatten.collect { |path| Rails.root.join(path) }
+      [paths].flatten.map { |path| Dir.glob(Rails.root.join(path)) }.flatten.collect { |path| Rails.root.join(path) }

This fix just makes it cohesive.

I suspect this is in a way connected with
testdouble#102 (to make it work with
engines).

**Context**

I was able to get this gem to work with my engine by going
inside the `test_app` dir and running

`bundle exec rails spec:javascript`

So far so good. The problem is that I don't want to have to run
`cd spec/test_app` every time to run my Javascript tests. I want to run them
from the engine's grandparent directory so I can include it in my Rakefile for
the entire test suite. In other words, this is my dir tree:

```
~/engine/
~/engine/spec/javascripts/
~/engine/spec/test_app/spec/javascripts/support/jasmine.yml
```

`Rails.root` is always `~/engine/spec/test_app/`, regardless of whether
I'm in `~/engine/ or not.

**The problem**

Look at this line:

```rb
[paths].flatten.map { |path| Dir.glob path }.flatten.collect { |path| Rails.root.join(path) }
```

`Dir.glob path`, equivalent to `Dir.glob("spec/javascripts")` returns
`true` because `Dir.pwd` is `~/engine/`, therefore
`~/engine/spec/javascripts` exists.

However, the last `collect` has `Rails.root.join("spec_javascript)`,
which will map to `~/engine/spec/test_app/spec/javascripts`, but
there's nothing there (because my test files were configured to
`~/engine/spec/javascript`).

To "fix that, I put `../javascripts` in my yml's `spec_dir`, but no `Dir.glob`
just returns false because, in fact, `~/engine/../javascripts` doesn't exist.
So, the code is verifying one path and resolving to some other.

**The fix**

```patch
-      [paths].flatten.map { |path| Dir.glob path }.flatten.collect { |path| Rails.root.join(path) }
+      [paths].flatten.map { |path| Dir.glob(Rails.root.join(path)) }.flatten.collect { |path| Rails.root.join(path) }
```

This fix just makes it cohesive.

I suspect this is in a way connected with
testdouble#102 (to make it work with
engines).
@kurko kurko closed this Feb 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant