2020-01-31 22:06:26 +00:00
|
|
|
"""
|
|
|
|
Let's make sure them docs work yah?
|
|
|
|
"""
|
|
|
|
from contextlib import contextmanager
|
2020-02-10 17:59:44 +00:00
|
|
|
import itertools
|
2020-01-31 22:06:26 +00:00
|
|
|
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
|
|
|
|
|
2020-10-13 18:49:31 +00:00
|
|
|
from conftest import repodir
|
2020-01-31 22:06:26 +00:00
|
|
|
|
|
|
|
|
2020-02-09 07:01:39 +00:00
|
|
|
def examples_dir():
|
2020-02-09 19:59:22 +00:00
|
|
|
"""Return the abspath to the examples directory.
|
|
|
|
"""
|
|
|
|
return os.path.join(repodir(), 'examples')
|
2020-02-08 01:20:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-02-09 07:01:39 +00:00
|
|
|
def run_example_in_subproc(loglevel, testdir, arb_addr):
|
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
|
2020-02-09 19:59:22 +00:00
|
|
|
# be executed with ``python -m <modulename>`` on windows..
|
2020-02-08 01:20:46 +00:00
|
|
|
shutil.copyfile(
|
2020-02-09 07:01:39 +00:00
|
|
|
os.path.join(examples_dir(), '__main__.py'),
|
2020-02-08 01:20:46 +00:00
|
|
|
os.path.join(str(testdir), '__main__.py')
|
|
|
|
)
|
|
|
|
|
2020-02-10 17:59:44 +00:00
|
|
|
# drop the ``if __name__ == '__main__'`` guard onwards from
|
|
|
|
# the *NIX version of each script
|
|
|
|
windows_script_lines = itertools.takewhile(
|
|
|
|
lambda line: "if __name__ ==" not in line,
|
|
|
|
script_code.splitlines()
|
|
|
|
)
|
|
|
|
script_code = '\n'.join(windows_script_lines)
|
2020-02-08 01:20:46 +00:00
|
|
|
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-09 19:59:22 +00:00
|
|
|
# run the testdir "libary module" as a script
|
2020-02-08 01:20:46 +00:00
|
|
|
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-09 07:01:39 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'example_script',
|
2020-10-03 23:35:18 +00:00
|
|
|
[
|
|
|
|
f for f in os.listdir(examples_dir())
|
|
|
|
if (
|
|
|
|
('__' not in f) and
|
|
|
|
('debugging' not in f)
|
|
|
|
)
|
|
|
|
],
|
2020-02-09 07:01:39 +00:00
|
|
|
)
|
|
|
|
def test_example(run_example_in_subproc, example_script):
|
2020-02-09 19:59:22 +00:00
|
|
|
"""Load and run scripts from this repo's ``examples/`` dir as a user
|
|
|
|
would copy and pasing them into their editor.
|
|
|
|
|
2020-10-03 23:35:18 +00:00
|
|
|
On windows a little more "finessing" is done to make
|
|
|
|
``multiprocessing`` play nice: we copy the ``__main__.py`` into the
|
|
|
|
test directory and invoke the script as a module with ``python -m
|
|
|
|
test_example``.
|
2020-02-09 19:59:22 +00:00
|
|
|
"""
|
2020-02-09 07:01:39 +00:00
|
|
|
ex_file = os.path.join(examples_dir(), example_script)
|
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
|
2020-02-10 17:07:32 +00:00
|
|
|
errmsg = err.decode()
|
|
|
|
errlines = errmsg.splitlines()
|
|
|
|
if err and 'Error' in errlines[-1]:
|
|
|
|
raise Exception(errmsg)
|
2020-01-31 22:06:26 +00:00
|
|
|
|
|
|
|
assert proc.returncode == 0
|