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

Prefer Pathname#glob to Dir.[] at spec/rails_helper.rb template #2678

Merged
merged 1 commit into from
May 18, 2023

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented May 10, 2023

Since Pathname#glob was added in Ruby 2.5, I thought it would be more concise to use this on spec/rails_helper.rb template.

Unlike Dir.[], Pathname#glob returns an array of Pathname, but Kernel.#require works the same way if you give an Pathname as an argument, so I think there should be no problem about this difference here. We could add .map(&:to_s), but I think that would be a bit redundant.

@JonRowe JonRowe merged commit fd5053d into rspec:main May 18, 2023
@JonRowe
Copy link
Member

JonRowe commented May 18, 2023

Thanks for the suggestion, merged because I can't see why not.

JonRowe added a commit that referenced this pull request May 18, 2023
@JonRowe
Copy link
Member

JonRowe commented Nov 21, 2023

Released in 6.1.0

@r7kamura r7kamura deleted the rails-root-glob branch January 22, 2024 08:10
@franzliedke
Copy link
Contributor

We could add .map(&:to_s), but I think that would be a bit redundant.

It turns out this affects sorting.

  • For pathnames, spec/support/dir/a.rb comes before spec/support/dir.rb
  • For strings, spec/support/dir.rb comes first

Not sure if we just do weird things in our support scripts (dir/a.rb relying on constants from dir.rb). This came up because Rubocop started preferring the Pathname#glob version as well. 😬

@pirj
Copy link
Member

pirj commented Aug 27, 2024

Wondering what RuboCop guys would say about this. I’d expect the order to remain the same as it was with Dir.[], and dir.rb to come first. The latter seems to do with the Pathname itself. Wondering if this is the first time the combination of that cop with order-dependency bites someone?

@franzliedke
Copy link
Contributor

@pirj The ordering as returned by Dir.[] and Pathname#glob is the same for both (at least on my Linux system, and Ruby 3+), but that's apparently not true for Pathname#<=> and String#<=>.

@JonRowe
Copy link
Member

JonRowe commented Aug 27, 2024

We could change sort to sort_by(&:to_s) here to solve this, or just drop back to the Dir style, would you like to work up a PR with either?

@pirj
Copy link
Member

pirj commented Aug 27, 2024

not true for Pathname#<=> and String#<=>.

That’s what I meant. But indeed the cop is not directly responsible due to us calling sort on the results.
It is more a Pathname issue apparently. Is it worth reporting in the gem? https://github.com/ruby/pathname/issues

@franzliedke
Copy link
Contributor

@JonRowe I'm happy to send a PR.

@pirj I think the fact that pathname.glob(...) has the same order of pathname.glob(...).sort is a strong argument that their <=> implementation is "correct", even though it makes use cases like this one (requiring an entire tree of files in a semantically sensible order) harder.

Random idea: We could check out how Zeitwerk determines the order when eager loading a directory.

@pirj
Copy link
Member

pirj commented Aug 27, 2024

https://github.com/ruby/pathname/blob/9d0190017f1ade0850bb6c47d8cf0c4165ea1dc0/test/pathname/test_pathname.rb#L540 is not too verbose. Can we call the behaviour in our case undetermined?

has the same order

What if it just returned the same reverse ordered list like z, y, x?

franzliedke added a commit to franzliedke/rspec-rails that referenced this pull request Aug 27, 2024
Before rspec#2678, the suggested code snippet for loading support files loaded files in directories (e.g. `spec/support/a/b.rb`) *after* files with the same basename as those directories (e.g. `spec/support/a.rb`).

This tends to be the preferrable load order, given how Ruby modules and classes are usually structured.

The root cause is a difference between the alphabetic ordering of string sorting compared to `Pathname#<=>`.
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.

4 participants