Skip to content

Removed an argument to concretize that was there only for unit tests#12213

Merged
scheibelp merged 2 commits intospack:developfrom
alalazo:refactor/removed_argument_to_concretize
Aug 3, 2019
Merged

Removed an argument to concretize that was there only for unit tests#12213
scheibelp merged 2 commits intospack:developfrom
alalazo:refactor/removed_argument_to_concretize

Conversation

@alalazo
Copy link
Copy Markdown
Member

@alalazo alalazo commented Jul 31, 2019

While rebasing #11372 I noticed that the Environment.concretize method changed signature in 0715b51:

def concretize(self, force=False, _display=True):

as a new argument _display got added. This argument seems to be used only in unit tests to switch off part of the code. To avoid:

  1. Using an argument to a function that starts with a leading underscore
  2. Having code that is needed only for tests mixed with business logic

this PR extracts the logic to display specs into its own function. Client code is then responsible to make an additional call if it needs to display the result of concretization - and unit tests can simply skip that call.

Instead of using an argument to deactivate displaying a tree within
tests, we'll just use a fixture and monkey patch where appropriate.
@alalazo alalazo added refactoring tests General test capability(ies) labels Jul 31, 2019
status_fn=spack.spec.Spec.install_status,
hashlen=7, hashes=True)
)
sys.stdout.write(_tree_to_display(concrete))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO this is slightly less clear than the original logic (IMO fixtures are harder to understand than parameters). I do agree that in general, it is bad practice to switch off logic inside of a function. In this particular case, the function is mixing datastructure maintenance and UI concern, so I felt this was harmless. I think it could definitely be refactored to be cleaner; IMO this could return the specs that are newly-concretized and then the command logic could print them (and the calls in the tests could just discard them).

Does that seem reasonable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In this particular case, the function is mixing datastructure maintenance and UI concern, so I felt this was harmless.

I agree that this function as it is has possibly too many responsibilities. To explain why I did this refactor: in #11372 concretize delegates to two different private functions, depending on whether we are concretizing the specs together or not. The argument _display had to be pushed down to those functions so instead of doing that I removed the argument completely.

Does that seem reasonable?

It seems reasonable to me, with one minor caveat. Spec.concretize returns None and modifies the calling object state while Spec.concretized returns the concretized spec in a new object. Doing the proposed change in Environment we'll be having a method that is named like the former in Spec but behaves more like the latter.

@becker33 @scheibelp In the end I am fine with either this approach or the one suggested by Peter above. Let me know how to proceed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with this change in service to #11372 - if you moved these changes to that PR I would accept the changes there; IMO it's hard to justify this separate from #11372 though. The way #12213 (comment) was phrased made me think this was more of a style change.

If you prefer to address this separately, I would prefer it be done as I requested in #12213 (comment).

It seems reasonable to me, with one minor caveat. Spec.concretize returns None and modifies the calling object state while Spec.concretized returns the concretized spec in a new object. Doing the proposed change in Environment we'll be having a method that is named like the former in Spec but behaves more like the latter.

That's fine with me: concretize can mean something different for an Environment vs. a Spec.

Copy link
Copy Markdown
Member Author

@alalazo alalazo Aug 1, 2019

Choose a reason for hiding this comment

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

I'm fine with this change in service to #11372 - if you moved these changes to that PR I would accept the changes there; IMO it's hard to justify this separate from #11372 though.

Not a big deal but:

  1. These changes were cherry-picked from Spack environments can concretize specs together #11372 and moved here as I think they don't belong in that PR
  2. It's also a style issue as, at least afaik, argument to functions with a leading underscore are not idiomatic in Python + having functionality in code that is needed only for unit tests is also not a good practice

In the end I wouldn't have changed this part if I didn't have to resolve a conflict, but once I made the modification I thought it was clearer to submit it separately. I'll take a further step and try to move the UI part to the command.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@scheibelp Let me know if the latest version is what you had in mind, or if further modifications are needed.

The UI part of Environment.concretize has been moved into its own
function. The "client" code is now responsible to make an
additional call to display the specs, if it needs to.

This permits to remove fixtures from unit tests and the additional
argument to the concretize method.
@scheibelp scheibelp merged commit 90756d0 into spack:develop Aug 3, 2019
@scheibelp
Copy link
Copy Markdown
Member

Thanks! I appreciate your flexibility on this!

@alalazo alalazo deleted the refactor/removed_argument_to_concretize branch August 3, 2019 10:00
@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Aug 3, 2019

No worries, thanks for the review!

dev-zero pushed a commit to dev-zero/spack that referenced this pull request Aug 13, 2019
Environment.concretize returns newly-concretized specs rather than
printing them; as a result, the _display argument is removed from
Environment.concretize (originally only used to avoid printing specs
during unit testing). Command logic which invokes
Environment.concretize prints these explicitly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring tests General test capability(ies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants