command line tests
In [ ]:
Copied!
from pathlib import Path
from subprocess import check_call
from sys import executable, path, version_info
from pathlib import Path
from subprocess import check_call
from sys import executable, path, version_info
In [ ]:
Copied!
from pytest import importorskip
from pytest import importorskip
In [ ]:
Copied!
from importnb import Notebook
from importnb import Notebook
In [ ]:
Copied!
GTE10 = version_info.major == 3 and version_info.minor >= 10
GTE10 = version_info.major == 3 and version_info.minor >= 10
In [ ]:
Copied!
HERE = Path(__file__).parent
HERE = Path(__file__).parent
In [ ]:
Copied!
path.insert(0, str(HERE))
path.insert(0, str(HERE))
In [ ]:
Copied!
UNTITLED = HERE / "Untitled42.ipynb"
UNTITLED = HERE / "Untitled42.ipynb"
In [ ]:
Copied!
ref = Notebook.load_file(UNTITLED)
REF = Path(ref.__file__)
ref = Notebook.load_file(UNTITLED)
REF = Path(ref.__file__)
In [ ]:
Copied!
def get_prepared_string(x):
if GTE10:
x = x.replace("optional arguments:", "options:")
return x.replace("\r", "")
def get_prepared_string(x):
if GTE10:
x = x.replace("optional arguments:", "options:")
return x.replace("\r", "")
In [ ]:
Copied!
def cli_test(command):
def delay(f):
def wrapper(tmp_path):
from shlex import split
path = tmp_path / "tmp"
with path.open("w") as file:
check_call([executable] + split(command), stderr=file, stdout=file)
out = path.read_text()
match = get_prepared_string(
f.__doc__.format(UNTITLED=UNTITLED.as_posix(), SLUG=ref.magic_slug)
)
if "UserWarning: Attempting to work in a virtualenv." in out:
out = "".join(out.splitlines(True)[2:])
assert out == match
return wrapper
return delay
def cli_test(command):
def delay(f):
def wrapper(tmp_path):
from shlex import split
path = tmp_path / "tmp"
with path.open("w") as file:
check_call([executable] + split(command), stderr=file, stdout=file)
out = path.read_text()
match = get_prepared_string(
f.__doc__.format(UNTITLED=UNTITLED.as_posix(), SLUG=ref.magic_slug)
)
if "UserWarning: Attempting to work in a virtualenv." in out:
out = "".join(out.splitlines(True)[2:])
assert out == match
return wrapper
return delay
In [ ]:
Copied!
@cli_test("-m importnb")
def test_usage():
"""\
usage: importnb [-h] [-m MODULE] [-c CODE] [-d DIR] [-t] [file] ...
run notebooks as python code
positional arguments:
file run a file
args arguments to pass to script
optional arguments:
-h, --help show this help message and exit
-m MODULE, --module MODULE
run a module
-c CODE, --code CODE run raw code
-d DIR, --dir DIR path to run script in
-t, --tasks run doit tasks
"""
@cli_test("-m importnb")
def test_usage():
"""\
usage: importnb [-h] [-m MODULE] [-c CODE] [-d DIR] [-t] [file] ...
run notebooks as python code
positional arguments:
file run a file
args arguments to pass to script
optional arguments:
-h, --help show this help message and exit
-m MODULE, --module MODULE
run a module
-c CODE, --code CODE run raw code
-d DIR, --dir DIR path to run script in
-t, --tasks run doit tasks
"""
In [ ]:
Copied!
@cli_test(rf"-m importnb -d {UNTITLED.parent.as_posix()} {UNTITLED.as_posix()}")
def test_file():
"""\
i was printed from {UNTITLED} and my name is __main__
{SLUG}
the parser namespace is Namespace(args=None)
"""
@cli_test(rf"-m importnb -d {UNTITLED.parent.as_posix()} {UNTITLED.as_posix()}")
def test_file():
"""\
i was printed from {UNTITLED} and my name is __main__
{SLUG}
the parser namespace is Namespace(args=None)
"""
In [ ]:
Copied!
@cli_test(rf"-m importnb -d {UNTITLED.parent.as_posix()} -m {UNTITLED.stem}")
def test_module():
"""\
i was printed from {UNTITLED} and my name is __main__
{SLUG}
the parser namespace is Namespace(args=None)
"""
@cli_test(rf"-m importnb -d {UNTITLED.parent.as_posix()} -m {UNTITLED.stem}")
def test_module():
"""\
i was printed from {UNTITLED} and my name is __main__
{SLUG}
the parser namespace is Namespace(args=None)
"""
In [ ]:
Copied!
@cli_test("-m importnb -c '{}'")
def test_empty_code():
""""""
@cli_test("-m importnb -c '{}'")
def test_empty_code():
""""""
In [ ]:
Copied!
@cli_test(rf"-m importnb -d {UNTITLED.parent.as_posix()} -t {UNTITLED.as_posix()} list")
def test_doit():
"""\
i was printed from {UNTITLED} and my name is __main__
{SLUG}
echo this the docstring for the `echo` task that echos hello.
"""
importorskip("doit")
@cli_test(rf"-m importnb -d {UNTITLED.parent.as_posix()} -t {UNTITLED.as_posix()} list")
def test_doit():
"""\
i was printed from {UNTITLED} and my name is __main__
{SLUG}
echo this the docstring for the `echo` task that echos hello.
"""
importorskip("doit")