-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Basic toolchain.py unit tests #1928
Conversation
f00fa53
to
6c49b1c
Compare
25e3cac
to
176a4e4
Compare
The |
Good, some more coverage is welcome, thanks to take care of this 😄 Mmm...it seems that my local tests fails:
The same error when running for python2...it seems that |
tests/test_toolchain.py
Outdated
Basic `create` distribution test. | ||
""" | ||
argv = ['toolchain.py', 'create', '--sdk-dir=/tmp/android-sdk', '--ndk-dir=/tmp/android-ndk'] | ||
with mock.patch('sys.argv', argv), \ |
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.
Again here, the backslashes in this context manager:
with mock.patch('sys.argv', argv), mock.patch(
'pythonforandroid.build.get_available_apis'
) as m_get_available_apis, mock.patch(
'pythonforandroid.build.get_toolchain_versions'
) as m_get_toolchain_versions, mock.patch(
'pythonforandroid.build.get_ndk_platform_dir'
) as m_get_ndk_platform_dir, mock.patch(
'pythonforandroid.build.get_cython_path'
) as m_get_cython_path, mock.patch(
'pythonforandroid.toolchain.build_dist_from_args'
) as m_build_dist_from_args:
m_get_available_apis.return_value = [27]
m_get_toolchain_versions.return_value = (['4.9'], True)
m_get_ndk_platform_dir.return_value = (
'/tmp/android-ndk/platforms/android-21/arch-arm',
True,
)
This will also be pep8 friendly
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 also hate backslashes, but for context managers, I haven't found a more readable way to write it so far.
I'm not yet convinced this way is more readable, but I gave it a try nonetheless 😄
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.
Ei, @AndreMiras, good work 👍
Note: I know that there is no general consents about keeping lines below 80 (so I stick to pep 8 standards/recommendations) ...I think that produces more readable code and github diffs, so I always try to keep a maximum of 79 character
argv = ['toolchain.py', '--help', '--storage-dir=/tmp'] | ||
with mock.patch('sys.argv', argv), pytest.raises(SystemExit) as ex_info, \ | ||
mock.patch('argparse.ArgumentParser.print_help') as m_print_help: | ||
ToolchainCL() |
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.
Wouldn't something like this be a little easier to understand:
subprocess.check_output([
sys.executable, "-c", "from pythonforandroid.toolchain import main(); main", "--help"
], cwd=os.path.join(os.path.dirname(__file__), ".."))
Or is that only me? Mock is fine but if it's not needed I would still rather use standard methods
There is also a lot of repetition of this block in particular:
with mock.patch('sys.argv', argv), pytest.raises(SystemExit) as ex_info, \
mock.patch('argparse.ArgumentParser.print_help') as m_print_help:
ToolchainCL()
No matter if we end up using mock or not, maybe a helper function for that would be useful?
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 subprocess is overkill to me, it's really too much top level integration test.
Regarding the patching helper, yes I agree, I actually do this in my projects and at work, but since it's not common in p4a I didn't dare to introduce it.
I'll then
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's really too much top level integration test.
I'll let you decide but just as a note: that seems like an irrelevant point to me, because isn't this what the entire purpose is? Check if the overall tool responds fine to --help
? It seems a little to me with this particular argument you're trying to isolate a component here with no benefit other than possibly complicating the test, and also removing it further from the actual usage later. But of course there are other completely valid reasons to stick with your current solution, e.g. if you find it more readable (which I don't but I can see others maybe would)
But keep in mind my proposal would also possibly reduce the maintenance burden if toolchain.py
is ever heavily refactored to also adjust the test here, because it nicely stays out of how the functionality needs to be performed - even more so than the mock code I mean - as long as it works right without a crash, which I think is an additional benefit!
But that's just my personal opinion of course and I don't think this should hold off a merge
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.
Yes I definitely don't share this point of view regarding readability, I find that subprocess.check_output
absolutely ugly. Not to say about parsing eventual errors that it could output or the fact that's unclear which python executable it would run, the fact that you need to play around with the cwd. For me it's just a big nope. I've hardly ever seen people writing unit tests calling subprocess
😕
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's sys.executable
(=just the same interpreter that is already running) do people not usually know that? 😄 but it's fine, leave it with mocking I am clearly making you confused 😂 (I'm weird that's normal)
The mocking really isn't too bad I just seem to have unusual preferences here
First simple set of tests for toolchain.py Also refactors `Context.prepare_build_environment()` slightly. Splits concerns to improve readability and ease unit testing.
176a4e4
to
312b522
Compare
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.
First simple set of tests for toolchain.py
Also refactors
Context.prepare_build_environment()
slightly. Splitsconcerns to improve readability and ease unit testing.