-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
Simplify Formula#outdated_versions logic. #411
Conversation
@xu-cheng, what do you think about the Is it OK that all of them go in separate methods and there is some code duplication? So, the question is do we need a separate |
👍
Yes. I think it's a good idea to have multiple test methods to pin point the possible problem.
I think both are fine. But personally I prefer to putting then in Have you run test case before simplifying the logic. Ideally, we should make the tests pass before and after you applying the logic change. |
01133bf
to
3e65c72
Compare
Yes, It worked. Decided to move tests to |
3e65c72
to
54cb7f3
Compare
|
||
same_prefix.mkpath | ||
same_tab = tab_for_prefix(same_prefix, "homebrew/core") | ||
same_tab.write |
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.
It looks like this pattern is repeated in every single test method (sometimes multiple times) which tells me that all of this should be consolidated in the helper function. And the returned Tab
object isn't actually needed. How about renaming tab_for_prefix
to setup_tab_with_tap
and using it as follows?
setup_tab_with_tap(same_prefix, "homebrew/core")
This could be the implementation (untested):
def setup_tab_with_tap(prefix, tap_string = nil)
prefix.mkpath
tab = Tab.empty
tab.tabfile = prefix/"INSTALL_RECEIPT.json"
tab.source["tap"] = tap_string if tap_string
tab.write
tab # Not needed and not currently used anywhere (if I didn't overlook anything).
end
This method would also make rewrite_tab_with_tap
obsolete (i.e. setup_tab_with_tap
should create the tab file and the prefix if it doesn't already exist yet and overwrite it otherwise).
👍 on the simplification in this PR, but I have added a suggestion to further reduce unnecessary code duplication in the test methods. |
54cb7f3
to
c16deb4
Compare
👍 I didn't remove tab at the end of the |
end | ||
|
||
def test_outdated_different_tap_installed | ||
outdated_tab = setup_tab_for_prefix(outdated_prefix, "user/repo") |
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 return value assigned to the local variable is never used, thus the assignment can be dropped.
Thanks for making the change! To me the code now looks much cleaner.
You don't have to. It's completely up to you and that's one additional line that doesn't really bother me (and maybe will even become useful later on). |
c16deb4
to
ae595fc
Compare
Thanks everyone. |
@@ -956,33 +956,25 @@ def unlock | |||
@oldname_lock.unlock unless @oldname_lock.nil? | |||
end | |||
|
|||
def migration_needed? | |||
oldname && !rack.exist? && (dir = HOMEBREW_CELLAR/oldname).directory? && | |||
!dir.subdirs.empty? && tap == Tab.for_keg(dir.subdirs.first).tap |
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.
Could this be tweaked in a future PR so you return false
for each of those cases rather than chaining with &&
? It makes it much easier to follow.
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.
👍 #432
One comment that would be cool to address in a future PR. For refactoring like this it's sometimes nice to open the PR with just the commit with the added tests so we can see that all those tests pass on both the old and new versions. |
Closes Homebrew#411. Signed-off-by: Xu Cheng <xucheng@me.com>
brew tests
with your changes locally?Description
Replace complicated logic from
Formula#outdated_versions
with simpler equivalent and add tests.What
outdated_versions
used to do:Suppose we have
f
-- an instance ofFormula
.f.name
(line 4)nil
and iff.pkg_version
was less or equal keg's version then at the end ofoutdated_versions
[] used to be returned.f.tap
, then ifpkg_version
was less or equal keg's version then at the end ofoutdated_versions
[] used to be returned.f.tap
we checked iff.pkg_version
was less or equal to keg's version (line 7) and then afterolder_or_same_tap_versions
was formedf.pkg_version
was never greater than keg's version (line 15), so we returned[]
.f.tap
we checked iff.pkg_version
was greater then keg's version we didn't add keg's version toolder_or_same_tap_versions
.So, [] was returned if and only if
f.pkg_version
less or equal for at least one of the kegs orf.rack
was empty.The PR suggests that we return
[]
as soon as we are sure the previously usedolder_or_same_tap_versions
cannot satisfyolder_or_same_tap_versions.all? { |v| pkg_version > v }
.Old
Formula#outdated_versions
implementation passes all the addedoutdated_versions
tests.