-
Notifications
You must be signed in to change notification settings - Fork 797
Fix bug in PathUtils::entries when dir is empty #153
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
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Not sure why the one test is failing. The expected and actual result look identical to me. |
Thanks for this PR, sorry for the delayed review. I took a look a the test output they're subtly different: a = {"version"=>3, "file"=>"sass/main.css", "mappings"=>"AADA,GAAA,CAAA,EAAA,CAAA;EAAA,MAAA,EAAA,CAAA;EAAA,OAAA,EAAA,CAAA;EAAA,UAAA,EAAA,IAAA,GAAA;;AAAA,GAAA,CAAA,EAAA,CAAA;EAAA,OAAA,EAAA,YAAA,GAAA;;AAAA,GAAA,CAAA,CAAA,CAAA;EAAA,OAAA,EAAA,KAAA;EAAA,OAAA,EAAA,QAAA;EAAA,eAAA,EAAA,IAAA,GAAA", "sources"=>["sass/main.source-86fe07ad89fecbab307d376bcadfa23d65ad108e3735b564510246b705f6ced1.scss"], "names"=>[]}
b = {"version"=>3, "file"=>"sass/main.css", "mappings"=>"AADA,GAAA,CAAA,EAAA,CAAA;EAAA,MAAA,EAAA,CAAA;EAAA,OAAA,EAAA,CAAA;EAAA,UAAA,EAAA,IAAA,GAAA;;AAAA,GAAA,CAAA,EAAA,CAAA;EAAA,OAAA,EAAA,YAAA,GAAA;;AAAA,GAAA,CAAA,CAAA,CAAA;EAAA,OAAA,EAAA,KAAA;EAAA,OAAA,EAAA,GAAA,CAAA,IAAA;EAAA,eAAA,EAAA,IAAA,GAAA", "sources"=>["sass/main.source-86fe07ad89fecbab307d376bcadfa23d65ad108e3735b564510246b705f6ced1.scss"], "names"=>[]}
puts a == b
# => false
puts a["mappings"]
# => "AADA,GAAA,CAAA,EAAA,CAAA;EAAA,MAAA,EAAA,CAAA;EAAA,OAAA,EAAA,CAAA;EAAA,UAAA,EAAA,IAAA,GAAA;;AAAA,GAAA,CAAA,EAAA,CAAA;EAAA,OAAA,EAAA,YAAA,GAAA;;AAAA,GAAA,CAAA,CAAA,CAAA;EAAA,OAAA,EAAA,KAAA;EAAA,OAAA,EAAA,QAAA;EAAA,eAAA,EAAA,IAAA,GAAA"
puts b["mappings"]
# => "AADA,GAAA,CAAA,EAAA,CAAA;EAAA,MAAA,EAAA,CAAA;EAAA,OAAA,EAAA,CAAA;EAAA,UAAA,EAAA,IAAA,GAAA;;AAAA,GAAA,CAAA,EAAA,CAAA;EAAA,OAAA,EAAA,YAAA,GAAA;;AAAA,GAAA,CAAA,CAAA,CAAA;EAAA,OAAA,EAAA,KAAA;EAAA,OAAA,EAAA,GAAA,CAAA,IAAA;EAAA,eAAA,EAAA,IAAA,GAAA" That commit by @tenderlove did introduce a subtle bug that wasn't tested against. When you reject from an array but reject nothing, the return value is a = [1,2,3]
puts a.reject! {|x| x == 4 }
# => nil versus a = [1,2,3]
puts a.reject {|x| x == 4 }
# => [1,2,3] To preserve the speed bump, and revert to the original, correct, behavior we should probably do something like: entries = Dir.entries(path, encoding: Encoding.default_internal)
entries.reject! { |entry|
entry =~ /^\.|~$|^\#.*\#$/
}
entries.sort!
entries We should also add a test case that verifies the correct result when an entry is used that does not have any rejected elements, can you add one in |
Thanks for the review! I'll update the code and add a test. |
Stubbing
With the new and improved PathUtils.entries implementation we would also expect ["a", "b", "c"] to be returned in the first case. |
Using
entries should never be nil. I try to never ever use mocks or stubs unless absolutely needed. You can create a folder in test/fixtures directory and add files |
On 04-11-15 17:04, Richard Schneeman wrote:
I think @bartkamphorst is right that in this case stubs will |
We don't need it to be empty, we just need the entries to all match that reject regex |
On 04-11-15 18:01, Richard Schneeman wrote:
We want to test the case in which no entries match the regex, right? |
Yep, you're correct. |
d25f6a9
to
f663079
Compare
I've updated the code and added a test. I worry that the test is not very verbose about what behavior it's actually testing, so it potentially risks getting removed again in the future. If you have any suggestions to make it more clear that would be great (can of course add a comment linking to this issue, but that's not very pretty). |
There is still a test failure, but note that I'm seeing the same failure on the |
How about [ ['a', 'b', 'c'], ['a', 'b', 'c', '.', '..'] ].each do | dir_entries_array |
Dir.stub :entries, dir_entries_array do
assert_equal ['a', 'b', 'c'], Sprockets::PathUtils.entries(Dir.tmpdir)
end
end That way, you show the two different possible outcomes of |
f663079
to
91eeebc
Compare
I've incorporated @bartkamphorst's suggestion -- thanks! The remaining test failure is caused by version Just a quick question: will you also backport this fix to the 3.x branch? Please let me know if I should open a separate PR for that. |
@@ -43,6 +43,12 @@ def test_entries | |||
"source-maps", | |||
"symlink" | |||
], entries(File.expand_path("../fixtures", __FILE__)) | |||
|
|||
[ ['a', 'b'], ['a', 'b', '.', '.'] ].each do |dir_contents| |
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.
The 4th element of the second array should be '..'
.
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.
Thx.
91eeebc
to
105ac6d
Compare
@schneems @rafaelfranca does this PR require any more work? |
Fix bug in PathUtils::entries when dir is empty
Sorry for dropping off over the weekend. I have a 4 month old. Thanks for the work, this looks great! |
Great, thanks! Want me to open one against the 3.x branch too? |
It looks like the problem exists on 3.x as well sprockets/lib/sprockets/path_utils.rb Lines 58 to 60 in 80d5620
|
👍 Thanks! |
When working on a Rails app I ran into the following exception from sprockets:
On investigation it seems that the
PathUtils::entries
method is performing anArray#reject!
, which returnsnil
for an empty array. This is a rare case becauseDir.entries
never (I think) returns an empty array on MRI . However, it apparently does on JRuby, and might on other platforms.Apparently, the
reject!
was introduced as a small performance improvement in a6bf1f9. This PR fixes the bug by reverting toreject
instead again.EDIT
To clarify,
Array#reject!
returnsnil
whenever the regex has no matches, so an empty directory is technically just a specific version of this edge case.