Commit 1438c33f authored by jpic ∞'s avatar jpic ∞ 💾
Browse files

Add autotest again

parent 5c7649e0
import inspect
import os
import subprocess
from .colors import colors
......@@ -7,6 +8,9 @@ from .entry_point import EntryPoint
def termsize():
if 'FORCE_TERMSIZE' in os.environ:
return 180, 80
try:
rows, columns = subprocess.check_output(['stty', 'size']).split()
except subprocess.CalledProcessError:
......
import copy
import os
import re
import subprocess
REWRITE = os.getenv('FIXTURE_REWRITE') or os.getenv('TEST_REWRITE')
def autotest(path, cmd, ignore=None, env=None):
"""
The autowriting test pattern, minimal for testing cli2 scripts.
Example:
cli2.autotest(
'tests/djcli_save_user.txt',
'djcli save auth.User username="test"',
)
"""
environ = copy.copy(os.environ)
if env:
for key, value in env.items():
environ[key] = value
environ['FORCE_TERMSIZE'] = '1'
environ['PATH'] = ':'.join([
environ.get('HOME', '') + '/.local/bin',
environ.get('PATH', '')
])
proc = subprocess.run(
cmd,
shell=True,
capture_output=True,
env=environ,
)
fixture = '\n'.join([
'command: ' + cmd,
'retcode: ' + str(proc.returncode),
'stdout:',
proc.stdout.decode('utf8'),
])
if proc.stderr:
fixture += '\n'.join([
'stderr:',
proc.stderr.decode('utf8'),
])
for r in ignore or []:
fixture = re.compile(r).sub(f'redacted', fixture)
exists = os.path.exists(path)
if REWRITE and exists:
os.unlink(path)
exists = False
if not exists:
# dirname = '/'.join(path.split('/')[:-1])
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(path, 'w+') as f:
f.write(fixture)
if REWRITE:
return
raise type('FixtureCreated', (Exception,), {})(
f'''
{path} was not in workdir and was created with:
{fixture}
'''.strip(),
)
diff_cmd = 'diff -U 1 - "%s" | sed "1,2 d"' % path
proc = subprocess.Popen(
diff_cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True
)
diff_out, diff_err = proc.communicate(input=fixture.encode('utf8'))
if diff_out:
raise type(f'''
DiffFound
- {cmd}
+ {path}
'''.strip(), (Exception,), {})('\n' + diff_out.decode('utf8'))
import pytest
from cli2.cli import main
from cli2.test import autotest
def test_call():
result = main('cli2.test_node.example_function', 'x', 'y=z')
......@@ -16,3 +20,18 @@ def test_doc():
result = main()
assert 'help' in result
@pytest.mark.parametrize('name,command', [
('alone', ''),
('help', 'help'),
('help_module', 'help cli2.test_node'),
('help_function', 'help cli2.test_node.example_function'),
('help_object', 'help cli2.test_node.example_object'),
('call_function_noargs', 'cli2.test_node.example_function'),
('call_function', 'cli2.test_node.example_function a b c=1 d=2'),
('call_method', 'cli2.test_node.example_object.example_method a b=c'),
('call_object', 'cli2.test_node.example_object_callable a b=1'),
])
def test_cli(name, command):
autotest(f'tests/{name}.txt', 'cli2 ' + command)
......@@ -84,7 +84,8 @@ def test_vararg_after_kwarg():
def test_positional_only_looksahead():
def foo(one=None, /, *two, three=None): return (one, two)
def foo(one=None, /, *two, three=None): # noqa
return (one, two)
cmd = Command(foo)
cmd.parse('three=z', 'x', 'y')
......
......@@ -21,14 +21,17 @@ example_function.cli2 = dict(color='pink')
class ExampleClass:
def example_method(self): pass
def example_method(self, *args, **kwargs):
return (args, kwargs)
example_object = ExampleClass()
class ExampleClassCallable:
def __call__(self, *args, **kwargs): return (args, kwargs)
@classmethod
def __call__(cls, *args, **kwargs):
return (args, kwargs)
example_object_callable = ExampleClassCallable()
......
command: cli2
retcode: 0
stdout:
cli2 makes your python callbacks work on CLI too !
Show doc and callables for module cli2.test_node:
cli2 cli2.test_node
cli2 help cli2.test_node # alternate
Call cli2.test_node.example_function with args=['x'] and kwargs=dict(y='z')
cli2 cli2.test_node.example_function x y=z
help Get help and callables for a dotted_path
command: cli2 cli2.test_node.example_function a b c=1 d=2
retcode: 0
stdout:
You have called example_function with:
args=('a', 'b')
kwargs={'c': '1', 'd': '2'}
command: cli2 cli2.test_node.example_function
retcode: 0
stdout:
You have called example_function with:
args=()
kwargs={}
command: cli2 cli2.test_node.example_object.example_method a b=c
retcode: 0
stdout:
(('a',), {'b': 'c'})
command: cli2 cli2.test_node.example_object_callable a b=1
retcode: 0
stdout:
(('a',), {'b': '1'})
command: cli2 help
retcode: 0
stdout:
cli2 makes your python callbacks work on CLI too !
Show doc and callables for module cli2.test_node:
cli2 cli2.test_node
cli2 help cli2.test_node # alternate
Call cli2.test_node.example_function with args=['x'] and kwargs=dict(y='z')
cli2 cli2.test_node.example_function x y=z
help Get help and callables for a dotted_path
command: cli2 help cli2.test_node.example_function
retcode: 0
stdout:
Example function docstring where the first sentence unfortunnately spreads
over the next line.
command: cli2 help cli2.test_node
retcode: 0
stdout:
Test cases for cli2.Node
ExampleClass
ExampleClassCallable
Node
example_dict
example_function Example function docstring where the first sentence unfo
rtunnately spreads over the next line
example_list
example_object
test_callable_class
test_callable_object
test_class
test_dict
test_eq
test_function
test_list
test_module
test_object
test_unknown
command: cli2 help cli2.test_node.example_object
retcode: 0
stdout:
example_method
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment