2020-01-31 22:06:26 +00:00
|
|
|
"""
|
|
|
|
Let's make sure them docs work yah?
|
|
|
|
"""
|
|
|
|
from contextlib import contextmanager
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import platform
|
2020-02-08 01:20:46 +00:00
|
|
|
import shutil
|
2020-01-31 22:06:26 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def confdir():
|
|
|
|
dirname = os.path.dirname
|
|
|
|
dirpath = os.path.abspath(
|
|
|
|
dirname(dirname(os.path.realpath(__file__)))
|
|
|
|
)
|
|
|
|
return dirpath
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-02-08 01:20:46 +00:00
|
|
|
def examples_dir(confdir):
|
|
|
|
return os.path.join(confdir, 'examples')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def run_example_in_subproc(loglevel, testdir, arb_addr, examples_dir):
|
2020-01-31 22:06:26 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def run(script_code):
|
|
|
|
kwargs = dict()
|
2020-02-08 01:20:46 +00:00
|
|
|
|
2020-01-31 22:06:26 +00:00
|
|
|
if platform.system() == 'Windows':
|
2020-02-08 01:20:46 +00:00
|
|
|
# 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)
|
|
|
|
|
2020-01-31 22:06:26 +00:00
|
|
|
# without this, tests hang on windows forever
|
|
|
|
kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
|
|
|
|
|
2020-02-08 01:20:46 +00:00
|
|
|
# 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),
|
|
|
|
]
|
|
|
|
|
2020-01-31 22:06:26 +00:00
|
|
|
proc = testdir.popen(
|
|
|
|
cmdargs,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
assert not proc.returncode
|
|
|
|
yield proc
|
|
|
|
proc.wait()
|
|
|
|
assert proc.returncode == 0
|
|
|
|
|
|
|
|
yield run
|
|
|
|
|
|
|
|
|
2020-02-08 01:20:46 +00:00
|
|
|
def test_example(examples_dir, run_example_in_subproc):
|
|
|
|
ex_file = os.path.join(examples_dir, 'a_trynamic_first_scene.py')
|
2020-01-31 22:06:26 +00:00
|
|
|
with open(ex_file, 'r') as ex:
|
|
|
|
code = ex.read()
|
|
|
|
|
|
|
|
with run_example_in_subproc(code) as proc:
|
|
|
|
proc.wait()
|
|
|
|
err, _ = proc.stderr.read(), proc.stdout.read()
|
|
|
|
|
|
|
|
# if we get some gnarly output let's aggregate and raise
|
|
|
|
if err and b'Error' in err:
|
|
|
|
raise Exception(err.decode())
|
|
|
|
|
|
|
|
assert proc.returncode == 0
|