Skip to content

Use version sort in pyenv versions#2405

Merged
native-api merged 7 commits intopyenv:masterfrom
fofoni:master
Jul 10, 2022
Merged

Use version sort in pyenv versions#2405
native-api merged 7 commits intopyenv:masterfrom
fofoni:master

Conversation

@fofoni
Copy link
Copy Markdown
Contributor

@fofoni fofoni commented Jul 1, 2022

  • Please consider implementing the feature as a hook script or plugin as a first step.
    • pyenv has some powerful support for plugins and hook scripts. Please refer to Authoring plugins for details and try to implement it as a plugin if possible.

Considered; implementation is too simple for a plugin.

  • Please consider contributing the patch upstream to rbenv, since we have borrowed most of the code from that project.
    • We occasionally import the changes from rbenv. In general, you can expect changes made in rbenv will be imported to pyenv too, eventually.
    • Generally speaking, we prefer not to make changes in the core in order to keep compatibility with rbenv.

Is this recommendation still valid? Looks like rbenv's had version sort for months: rbenv/rbenv@28cd6f12

Description

Check if the system sort command supports version sort and, if so, use it.

I used sort --version-sort as discussed in #2097. rbenv's solution does not depend on --version-sort, and would require a little bit more refactoring, but I'd be happy to port it to pyenv if you think it's worth the effort.

Tests

Should I add any?

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 1, 2022

Oops I see that I'm relying on the existence of at least some sort command; I'll fix it.

@native-api
Copy link
Copy Markdown
Member

sort is a standard UNIX command and is used elsewhere, too. You can rely on its presence.

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 1, 2022

sort is a standard UNIX command and is used elsewhere, too. You can rely on its presence.

apparently it's missing inside the CI environment, because it was breaking some bats tests

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 1, 2022

BTW how do I run the test suite locally? Instead of pushing only to find I've broken more tests.

@native-api
Copy link
Copy Markdown
Member

BTW how do I run the test suite locally? Instead of pushing only to find I've broken more tests.

make test

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 1, 2022

Looks like it's good now!

Copy link
Copy Markdown
Member

@native-api native-api left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As new functionality, this probably warrants a test.

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 2, 2022

As new functionality, this probably warrants a test.

The problem is the CI environment doesn't have sort. I could implement a test like "if system has sort, assert sorted by version; else, assert sorted lexicographically" -- would it be ok?

@native-api
Copy link
Copy Markdown
Member

native-api commented Jul 2, 2022

The problem is the CI environment doesn't have sort.

It does have sort as it's a part of coreutils. The reason it's not found there is the path_without test function. It doesn't include sort in the list of executables it ensures to be present on PATH -- sort needs to be added to that list.

@native-api
Copy link
Copy Markdown
Member

native-api commented Jul 2, 2022

I could implement a test like "if system has sort, assert sorted by version; else, assert sorted lexicographically"

The tests need to run in systems with both "modern" and "old" sort -- so you need to mock it for the test.

Since the new logic only fires with "modern" sort, it's sufficient to mock up a "modern" sort for the new test: the logic for "old" sort is already tested in existing tests which have test data where version sorting makes no difference.

There is an existing function that mocks an executable, use it as an example. The logic for the mockup can be to just unconditionally output the correct result for the specific set of versions that you make in the test.

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 9, 2022

Ok, the test passes, but I'm not sure if that's what you meant.

If you drop the --version-sort in https://github.com/fofoni/pyenv/blob/c70edbec5386d617f5ace823c97d90a85654c51a/libexec/pyenv-versions#L134 then the test will fail. Is that it?

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 9, 2022

Also, I didn't add sort to the whitelist inside path_without, since it's not required anymore to make the tests pass.

Copy link
Copy Markdown
Member

@native-api native-api left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is fine.

Thinking about it though, other tests would fire the code branch that correspond to the system's sort. So we do also need a test for an old sort, likewise mocking it -- or in a modern system, this case would not be tested 😐

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 10, 2022

Well, the mktemp problem is fixed.

Now, just to be sure: with the implementation we have right now, if the system doesn't have a modern sort binary, then no sort binary is used at all. In this case, what happens is the old behavior of getting the order (guaranteed by bash as you mentioned here) from the glob $versions_dir/*.

As you also already said,

Since the new logic only fires with "modern" sort, it's sufficient to mock up a "modern" sort for the new test: the logic for "old" sort is already tested in existing tests which have test data where version sorting makes no difference.

this old behavior is already checked in tests like "multiple versions", "indicates current version", "globally selected version", and others in the versions.bats file.

I understand that maybe explicitly testing the order with another test dedicated for this might be desired; I'd just like to make sure that's what you're asking for.

@native-api
Copy link
Copy Markdown
Member

native-api commented Jul 10, 2022

this old behavior is already checked in tests like "multiple versions", "indicates current version", "globally selected version", and others in the versions.bats file.

The problem is, in those tests, we don't mock anything within the ordering logic. So with a new sort, those tests would fire the new execution path rather than the old one.

@fofoni
Copy link
Copy Markdown
Contributor Author

fofoni commented Jul 10, 2022

Oh ok, that makes sense

@native-api native-api merged commit fdaeaf1 into pyenv:master Jul 10, 2022
@native-api
Copy link
Copy Markdown
Member

Good job. Thank you!

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.

Pyenv versions listing should use version sort

2 participants