-
Notifications
You must be signed in to change notification settings - Fork 41
Add Popen_with_deplayed_expansion to process_utils #188
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from genty import genty, genty_dataset | ||
|
|
||
| from app.util.process_utils import Popen_with_delayed_expansion | ||
|
|
||
| from test.framework.base_unit_test_case import BaseUnitTestCase | ||
|
|
||
|
|
||
| @genty | ||
| class TestProcessUtils(BaseUnitTestCase): | ||
|
|
||
| @genty_dataset( | ||
| str_cmd_on_windows=( | ||
| 'set FOO=1 && echo !FOO!', | ||
| 'nt', | ||
| ['cmd', '/V', '/C', 'set FOO=1 && echo !FOO!'], | ||
| ), | ||
| list_cmd_on_windows=( | ||
| ['set', 'FOO=1', '&&', 'echo', '!FOO!'], | ||
| 'nt', | ||
| ['cmd', '/V', '/C', 'set', 'FOO=1', '&&', 'echo', '!FOO!'], | ||
| ), | ||
| str_cmd_on_posix=( | ||
| 'export FOO=1; echo $FOO', | ||
| 'posix', | ||
| 'export FOO=1; echo $FOO', | ||
| ), | ||
| list_cmd_on_posix=( | ||
| ['export', 'FOO=1;', 'echo', '$FOO'], | ||
| 'posix', | ||
| ['export', 'FOO=1;', 'echo', '$FOO'], | ||
| ), | ||
| ) | ||
| def test_Popen_with_deplayed_expansion(self, input_cmd, os_name, expected_final_cmd): | ||
| # Arrange | ||
| mock_os = self.patch('app.util.process_utils.os') | ||
| mock_os.name = os_name | ||
| mock_subprocess_popen = self.patch('subprocess.Popen') | ||
|
|
||
| # Act | ||
| Popen_with_delayed_expansion(input_cmd) | ||
|
|
||
| # Assert | ||
| mock_subprocess_popen.assert_called_once_with(expected_final_cmd) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you think we would want to use this wrapper everywhere throughout the app? If so, we might want to just name it Popen and maybe enforce that this wrapper must be used instead of subprocess.Popen directly.
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.
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.
Probably not patching. We'd probably enforce via convention (lightest weight), custom lint rule, or unit test blacklist.
I'm not too concerned about the enforcing since we very rarely add Popen calls. The main thing I was wondering is if we should just name this Popen.
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.
@josephharrington I don't like to name it
Popenas it's confusing to people. Why I should use thisPopeninstead ofsubprocess.Popen? Naming it this way, IMO, is explicit so hopefully more readable and maintainable. I am going to merge this as is but I am more than happy to change it later. 😃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.
I totally agree that naming it something more descriptive would be helpful to people encountering it for the first time.
My main reasoning about why I'd name it something other than
Popen_with_delayed_expansionspecifically is that I can totally see us adding more functionality to this method in the future, especially if, as you said, we'd probably want to use it everywhere throughout the app. (Just as an example of what I mean: if we add some sort of output redirection, thenPopen_with_delayed_expansionwould either becomePopen_with_delayed_expansion_and_output_redirectionor more likely just become a not-quite-so-descriptive name.) I suppose something still descriptive but a bit more general such as "Popen_cross_platform" would avoid that.I also think the naming discussion definitely depends on how this will be used. More specifically, I think it depends on if it's more of a special case when we do use this instead of standard Popen or when we don't. (And actually I think the answer to that is pretty tightly linked to the likelihood that we'll add more functionality to this method in the future, so this is pretty close to my previous point.)
Answering the question of "Why I should use this Popen instead of subprocess.Popen?" was also what I was thinking about when suggesting something like a lint rule. Chances are that even with the name
Popen_with_delayed_expansionsomeone will still end up going to read the method's docstring to find out what delayed expansion is anyway. :) We'd probably want a lint rule if we decided there would never be a reason someone would not want to use this -- but I'm not yet sure that's true.I'm fine with leaving this for now -- we can always come back to it if we want to. I just mostly wanted to explain my line of reasoning in excruciating detail. If you've read this far then you've suffered enough. 😪
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.
lol. It's all good thoughts!