Discover and run unit tests from check.py#1948
Conversation
unittest is Python's standard testing framework, so this change allows arbitrary tests to be written without introducing any new dependencies or code in check.py. A new test that was not possible to write before is also included. It is the first of many.
Refactors features into a new wasm-features.h file and updates the validator to check that all types are allowed. Currently this is only relevant for the v128 SIMD type, but new types will be added in the future. The test for this change is in WebAssembly#1948.
This is necessary to write tests that don't require temporary files, such as in WebAssembly#1948, and is generally useful.
| result = Py2CompletedProcess(cmd, proc.returncode, stdout, stderr) | ||
| if check: | ||
| result.check_returncode() | ||
| return result |
There was a problem hiding this comment.
This is all copied from emscripten's shared.py. The only difference is that I added support for the capture_output parameter in run_process.
There was a problem hiding this comment.
Should we add capture_output to emscripten's shared.py? (Not knowing what that enables in any detail)
There was a problem hiding this comment.
It makes me sad that we need all this boilerplate..
There was a problem hiding this comment.
It would be great to upgrade to python3 instead, but that should probably be a separate discussion.
There was a problem hiding this comment.
@jgravelle-google I think we can wait to add capture_output to emscripten's shared.py until someone wants to use it. All it does is store the process's stdout and stderr on the returned object so they can be programmatically inspected. Emscripten tends to store output in temporary files instead.
| print '\n[ ' + str(num_failures) + ' failures! ]' | ||
|
|
||
| return num_failures | ||
| return 0 if num_failures == 0 else 1 |
There was a problem hiding this comment.
This fixes a bug where if some multiple of 256 tests fail, the exit code would be 0 (on linux, anyway).
There was a problem hiding this comment.
That should be a comment inline, because reversing this is exactly the sort of thing someone would think is a reasonable simplification
There was a problem hiding this comment.
Because I don't like python's ternary syntax (maybe I'm biased), how about moving the return 1 into the block above as an early out and then just return 0 at the end here?
There was a problem hiding this comment.
@jgravelle-google Good point. I'm not sure a comment makes sense anymore with the change @sbc100 suggested, though.
There was a problem hiding this comment.
yay to code changes to make comments not-needed
There was a problem hiding this comment.
Huh, yeah merging it with the num_failures clause above makes that even nicer 👍
| ) | ||
| """ | ||
| p = run_process(WASM_OPT + ['--mvp-features', '--print'], | ||
| input=module, check=False, capture_output=True) |
This is necessary to write tests that don't require temporary files, such as in #1948, and is generally useful.
Refactors features into a new wasm-features.h file and updates the validator to check that all types are allowed. Currently this is only relevant for the v128 SIMD type, but new types will be added in the future. The test for this change is in #1948.
|
@kripken This has no more outstanding dependencies. |
| print '\n[ checking unit tests...]\n' | ||
|
|
||
| # equivalent to `python -m unittest discover -s . -v` | ||
| suite = unittest.defaultTestLoader.discover(os.path.dirname(__file__)) |
There was a problem hiding this comment.
would this look in all of binaryen? how about looking just in test/?
jgravelle-google
left a comment
There was a problem hiding this comment.
Generally lgtm, nice
| result = Py2CompletedProcess(cmd, proc.returncode, stdout, stderr) | ||
| if check: | ||
| result.check_returncode() | ||
| return result |
There was a problem hiding this comment.
Should we add capture_output to emscripten's shared.py? (Not knowing what that enables in any detail)
| # limitations under the License. | ||
|
|
||
| import os | ||
| import unittest |
|
|
||
|
|
||
| def run_unittest(): | ||
| global num_failures |
There was a problem hiding this comment.
Hmm.. this seems odd. num_failures is a variable that is imported from scripts.test.shared but is a global in this file. Is this doing what you expect it to do? If we want changes to be visible in shared we would probably not use global by shared.num_failures.
There was a problem hiding this comment.
Looking at the details of how num_failures is currently used, I think you probably want to use a new local here.. either that or somehow hook into the existing shared.fail_with_error system.
There was a problem hiding this comment.
Yes, this is definitely doing what I expect it to do, which is count unittest failures as equivalent to any other failures. shared.num_failures is imported into the global namespace of this module, but shared is not, so using shared.num_failures instead of global num_failures would require a redundant import. Looking at shared.fail_with_error, it basically does the same thing but respects the abort_on_first_failure option. I will update this code to respect that option as well.
There was a problem hiding this comment.
But the num_failures that is global the this module is different to one in shared isn't it?
Updating num_failures in shared won't effect this value and vice versa. I guess that bug already existed but this make it worse.
There was a problem hiding this comment.
In python when you re-assign a global, it doesn't effect modules that have already imported that global.
There was a problem hiding this comment.
Wow you're totally right. That's nuts. TIL. Fixing now.
There was a problem hiding this comment.
... I take back some of the nice things I've said about Python
| print '\n[ ' + str(num_failures) + ' failures! ]' | ||
|
|
||
| return num_failures | ||
| return 0 if num_failures == 0 else 1 |
There was a problem hiding this comment.
Because I don't like python's ternary syntax (maybe I'm biased), how about moving the return 1 into the block above as an early out and then just return 0 at the end here?
| result = Py2CompletedProcess(cmd, proc.returncode, stdout, stderr) | ||
| if check: | ||
| result.check_returncode() | ||
| return result |
There was a problem hiding this comment.
It makes me sad that we need all this boilerplate..
|
Can I get an explicit lgtm on the new handling of |
unittest is Python's standard testing framework, so this change allows
arbitrary tests to be written without introducing any new dependencies
or code in check.py. A new test that was not possible to write before
is also included. It is the first of many.