Use the windows "gotchcas" fix for example tests

Apply the fix from @chrizzFTD where we invoke the entry point using
module exec mode on a ``__main__.py`` and import the
``test_example::`main()` from within that entry point script.
example_tests
Tyler Goodlet 2020-02-07 20:20:46 -05:00
parent 00fc734580
commit 70636a98f6
3 changed files with 47 additions and 10 deletions

View File

@ -0,0 +1,11 @@
"""
Needed in windows.
This needs to be the main program as it will be
called '__mp_main__' by the multiprocessing module
"""
if __name__ == '__main__':
import multiprocessing
multiprocessing.freeze_support()
from . import test_example
test_example.tractor.run(test_example.main, start_method='spawn')

View File

@ -1,5 +1,5 @@
import platform
import tractor
from functools import partial
_this_module = __name__
the_line = 'Hi my name is {}'

View File

@ -6,7 +6,7 @@ import os
import sys
import subprocess
import platform
import pprint
import shutil
import pytest
@ -21,20 +21,46 @@ def confdir():
@pytest.fixture
def run_example_in_subproc(loglevel, testdir, arb_addr):
def examples_dir(confdir):
return os.path.join(confdir, 'examples')
@pytest.fixture
def run_example_in_subproc(loglevel, testdir, arb_addr, examples_dir):
@contextmanager
def run(script_code):
script_file = testdir.makefile('.py', script_code)
cmdargs = [
sys.executable,
str(script_file),
]
kwargs = dict()
if platform.system() == 'Windows':
# on windows we need to create a special __main__.py which will
# be executed with ``python -m __main__.py`` on windows..
shutil.copyfile(
os.path.join(examples_dir, '__main__.py'),
os.path.join(str(testdir), '__main__.py')
)
# drop the ``if __name__ == '__main__'`` guard
script_code = '\n'.join(script_code.splitlines()[:-4])
script_file = testdir.makefile('.py', script_code)
# without this, tests hang on windows forever
kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
# run the "libary module" as a script
cmdargs = [
sys.executable,
'-m',
# use the "module name" of this "package"
'test_example'
]
else:
script_file = testdir.makefile('.py', script_code)
cmdargs = [
sys.executable,
str(script_file),
]
proc = testdir.popen(
cmdargs,
stdout=subprocess.PIPE,
@ -49,8 +75,8 @@ def run_example_in_subproc(loglevel, testdir, arb_addr):
yield run
def test_a_trynamic_first_scene(confdir, run_example_in_subproc):
ex_file = os.path.join(confdir, 'examples', 'a_trynamic_first_scene.py')
def test_example(examples_dir, run_example_in_subproc):
ex_file = os.path.join(examples_dir, 'a_trynamic_first_scene.py')
with open(ex_file, 'r') as ex:
code = ex.read()