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

cmp different from the string representation #41

Open
pirj opened this issue Aug 27, 2024 · 0 comments
Open

cmp different from the string representation #41

pirj opened this issue Aug 27, 2024 · 0 comments

Comments

@pirj
Copy link

pirj commented Aug 27, 2024

There's an ordering difference when using the built-in <=> vs the string representation one:

> Pathname.new(File.expand_path('.')).glob('**/*.rb').sort.first(5)
=> [#<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expectation_chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/message_chains.rb>]
> Pathname.new(File.expand_path('.')).glob('**/*.rb').sort_by(&:to_s).first(5)
=> [#<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb>]

And the latter corresponds to the classic alternative:

> Dir[File.expand_path('**/*.rb')].sort.first(5)
=> ["/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb"]

Just by looking at the cmp implementation and tests I couldn't understand, is this the designed behaviour, or the behaviour is undefined?

Semantically, it would make sense to load rspec/mocks.rb first, and then rspec/mocks/any_instance.rb, not the other way around.
Some discussion in rspec-rails regarding the issues it may cause.
rspec-rails PR to change the helper template to use Pathname#glob instead of Dir.[].

A workaround rspec-rails plans to take is to sort_by(&:to_s) to get back to the string representation-like ordering.

(semi-)Related cops:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant