Skip to content

Commit

Permalink
termui test
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Dec 18, 2023
1 parent 4a63311 commit 9c181b7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 26 deletions.
12 changes: 9 additions & 3 deletions sphinxcontrib/typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ def generate_nodes(
section = (
nodes.section(
'',
nodes.title(text=name),
nodes.title(
text=(
name
if not getattr(self, 'parent', None) else
f'{self.parent.command_path} {name}'
)
),
ids=[nodes.make_id(source_name)],
names=[nodes.fully_normalize_name(source_name)],
)
Expand Down Expand Up @@ -474,8 +480,8 @@ def run(self) -> t.Iterable[nodes.section]:
try:
self.prog_name = (
command.callback.__module__.split('.')[-1]
if hasattr(command, 'callback')
or not hasattr(self, 'parent')
if hasattr(command, 'callback')
and not hasattr(self, 'parent')
else re.split(r'::|[.:]', self.arguments[0])[-1]
)
except Exception as err:
Expand Down
40 changes: 24 additions & 16 deletions tests/click/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,33 @@ def record_callback(callback):

todo_include_todos = True

def _typer_render_html(*args, **kwargs):
###########################################################
# Test our typer configuration parameter function overrides

def typer_render_html(*args, **kwargs):
record_callback('typer_render_html')
return sphinxcontrib_typer.render_html_iframe(*args, **kwargs)

typer_render_html = _typer_render_html

def typer_get_iframe_height(*args, **kwargs):
record_callback('typer_get_iframe_height')
return sphinxcontrib_typer.get_iframe_height(*args, **kwargs)


def typer_svg2pdf(*args, **kwargs):
record_callback('typer_svg2pdf')
return sphinxcontrib_typer.svg2pdf(*args, **kwargs)


def typer_convert_png(*args, **kwargs):
record_callback('typer_convert_png')
return sphinxcontrib_typer.convert_png(*args, **kwargs)


def typer_get_web_driver(*args, **kwargs):
record_callback('typer_get_web_driver')
return sphinxcontrib_typer.get_selenium_webdriver(*args, **kwargs)

# app.add_config_value(
# 'typer_get_iframe_height', lambda _: get_iframe_height, 'env'
# )
# app.add_config_value('typer_svg2pdf', lambda _: svg2pdf, 'env')
# app.add_config_value('typer_iframe_height_padding', 30, 'env')
# app.add_config_value(
# 'typer_iframe_height_cache_path',
# Path(app.confdir) / 'typer_cache.json',
# 'env',
# )

# app.add_config_value('typer_convert_png', lambda _: convert_png, 'env')
# app.add_config_value(
# 'typer_get_web_driver', lambda _: get_selenium_webdriver, 'env'
# )
typer_iframe_height_padding = 40
typer_iframe_height_cache_path = Path(__file__).parent / 'cache.json'
11 changes: 11 additions & 0 deletions tests/click/termui/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. typer:: termui.cli
:preferred: html
:width: 65
:make-sections:
:show-nested:

.. typer:: termui.cli:menu
:preferred: html
:width: 100
:convert-png: latex
:make-sections:
3 changes: 3 additions & 0 deletions tests/click/termui/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,6 @@ def menu():
click.echo("Invalid input")
elif menu == "quit":
return

if __name__ == "__main__":
cli()
86 changes: 79 additions & 7 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def test_sphinx_html_build():
buildername='html'
)

assert app.config.typer_iframe_height_padding == 30
assert app.config.typer_iframe_height_cache_path == SRC_DIR / 'typer_cache.json'

# Build the documentation
app.build()

Expand Down Expand Up @@ -135,6 +138,8 @@ def build_click_example(name, builder):
bld_dir = ex_dir / 'build'
if bld_dir.exists():
shutil.rmtree(bld_dir)
if (CLICK_EXAMPLES / 'cache.json').exists():
os.remove(CLICK_EXAMPLES / 'cache.json')

os.chdir(CLICK_EXAMPLES / name)

Expand All @@ -146,8 +151,12 @@ def build_click_example(name, builder):
buildername=builder
)

assert app.config.typer_iframe_height_padding == 40
assert app.config.typer_iframe_height_cache_path == CLICK_EXAMPLES / 'cache.json'

# Build the documentation
app.build()

os.chdir(cwd)
if builder == 'html':
result = (bld_dir / builder / 'index.html').read_text()
Expand Down Expand Up @@ -176,40 +185,43 @@ def get_click_ex_help(name, *subcommands):
).stdout.decode()


def check_html(html, help_txt, iframe_number=0):
def check_html(html, help_txt, iframe_number=0, threshold=0.85):
soup = bs(html, 'html.parser')
iframe = soup.find_all('iframe')[iframe_number]
assert iframe is not None
iframe_src = bs(iframe.attrs['srcdoc'], 'html.parser')
assert iframe_src is not None
code = iframe_src.find('code')
assert code is not None
assert similarity(code.text, help_txt) > 0.95
assert similarity(code.text, help_txt) > threshold


def check_svg(html, help_txt, svg_number=0):
def check_svg(html, help_txt, svg_number=0, threshold=0.75):
soup = bs(html, 'html.parser')
svg = soup.find_all('svg')[svg_number]
assert svg is not None
assert similarity(svg.text.strip(), help_txt) > 0.75
assert similarity(svg.text.strip(), help_txt) > threshold


def check_text(html, help_txt, txt_number=0):
def check_text(html, help_txt, txt_number=0, threshold=0.95):
soup = bs(html, 'html.parser')
txt = soup.find_all('pre')[txt_number]
assert txt is not None
assert similarity(txt.text.strip(), help_txt) > 0.95
assert similarity(txt.text.strip(), help_txt) > threshold


def test_click_ex_validation():
clear_callbacks()

bld_dir, html = build_click_example('validation', 'html')
assert (CLICK_EXAMPLES / 'cache.json').is_file()

help_txt = get_click_ex_help('validation')

check_html(html, help_txt)

assert check_callback('typer_render_html')
assert check_callback('typer_get_iframe_height')
assert check_callback('typer_get_web_driver')

check_svg(html, help_txt)
check_text(html, help_txt)
Expand All @@ -218,6 +230,60 @@ def test_click_ex_validation():
shutil.rmtree(bld_dir.parent)


def test_click_ex_termui():
clear_callbacks()

bld_dir, html = build_click_example('termui', 'html')

help_txt = get_click_ex_help('termui')
clear_help = get_click_ex_help('termui', 'clear')
colordemo_help = get_click_ex_help('termui', 'colordemo')
edit_help = get_click_ex_help('termui', 'edit')
locate_help = get_click_ex_help('termui', 'locate')
menu_help = get_click_ex_help('termui', 'menu')
open_help = get_click_ex_help('termui', 'open')
pager_help = get_click_ex_help('termui', 'pager')
pause_help = get_click_ex_help('termui', 'pause')
progress_help = get_click_ex_help('termui', 'progress')

# verifies :show-nested:
check_html(html, help_txt)
check_html(html, clear_help, 1)
check_html(html, colordemo_help, 2)
check_html(html, edit_help, 3)
check_html(html, locate_help, 4)
check_html(html, menu_help, 5)
check_html(html, open_help, 6)
check_html(html, pager_help, 7)
check_html(html, pause_help, 8)
check_html(html, progress_help, 9)
check_html(html, menu_help, 10)

# verify :make-sections:
soup = bs(html, 'html.parser')
assert soup.find('section').find('h1').text.startswith('termui')
assert soup.find_all('section')[1].find('h2').text.startswith('clear')
assert soup.find_all('section')[2].find('h2').text.startswith('colordemo')
assert soup.find_all('section')[3].find('h2').text.startswith('edit')
assert soup.find_all('section')[4].find('h2').text.startswith('locate')
assert soup.find_all('section')[5].find('h2').text.startswith('menu')
assert soup.find_all('section')[6].find('h2').text.startswith('open')
assert soup.find_all('section')[7].find('h2').text.startswith('pager')
assert soup.find_all('section')[8].find('h2').text.startswith('pause')
assert soup.find_all('section')[9].find('h2').text.startswith('progress')

assert soup.find_all('section')[10].find('h1').text.startswith('termui menu')

# verify correct name of subcommand
assert 'Usage: termui menu [OPTIONS]' in bs(
soup.find_all('iframe')[10].attrs['srcdoc'],
'html.parser'
).find('code').text

if bld_dir.exists():
shutil.rmtree(bld_dir.parent)


def test_click_text_build_works():

bld_dir, text = build_click_example('validation', 'text')
Expand All @@ -236,6 +302,9 @@ def test_click_latex_build_works():
bld_dir, latex = build_click_example('validation', 'latex')
help_txt = get_click_ex_help('validation')

assert check_callback('typer_svg2pdf')
assert check_callback('typer_convert_png')

# make sure the expected text rendered at least once
assert latex.count('Usage: validation [OPTIONS]') == 1

Expand All @@ -252,3 +321,6 @@ def test_click_latex_build_works():
assert len(list(bld_dir.glob('**/*.png'))) == 1, 'Should have rendered the help 1 time as png'
html_png = bld_dir / 'validation_0b121597.png'
assert img_similarity(CLICK_EXAMPLES / 'validation' / 'html.png', html_png) < 1000

if bld_dir.exists():
shutil.rmtree(bld_dir.parent)

0 comments on commit 9c181b7

Please sign in to comment.