diff --git a/examples/__main__.py b/examples/__main__.py new file mode 100644 index 0000000..7cb2506 --- /dev/null +++ b/examples/__main__.py @@ -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') diff --git a/examples/a_trynamic_first_scene.py b/examples/a_trynamic_first_scene.py index 8f43b9f..8197dc8 100644 --- a/examples/a_trynamic_first_scene.py +++ b/examples/a_trynamic_first_scene.py @@ -1,5 +1,5 @@ +import platform import tractor -from functools import partial _this_module = __name__ the_line = 'Hi my name is {}' diff --git a/tests/test_docs_examples.py b/tests/test_docs_examples.py index 87f7666..c118ee1 100644 --- a/tests/test_docs_examples.py +++ b/tests/test_docs_examples.py @@ -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()