Skip to content

Commit

Permalink
tests: Add tests for 'commands' option
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Finucane <stephen@that.guru>
  • Loading branch information
stephenfin committed Apr 5, 2024
1 parent 1bcd407 commit 37742be
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
6 changes: 6 additions & 0 deletions tests/roots/commands/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

extensions = ['sphinx_click']
22 changes: 22 additions & 0 deletions tests/roots/commands/greet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""The greet example taken from the README."""

import click


@click.group()
def greet():
"""A sample command group."""
pass


@greet.command()
@click.argument('user', envvar='USER')
def hello(user):
"""Greet a user."""
click.echo('Hello %s' % user)


@greet.command()
def world():
"""Greet the world."""
click.echo('Hello world!')
6 changes: 6 additions & 0 deletions tests/roots/commands/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Commands
========

.. click:: greet:greet
:prog: greet
:commands: world
4 changes: 2 additions & 2 deletions tests/roots/nested-full/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Basics
======
Nested (full)
=============

.. click:: greet:greet
:prog: greet
Expand Down
42 changes: 42 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,50 @@ def test_basics(make_app, rootdir):
assert section[3].astext() == 'Commands'
assert isinstance(section[4], sphinx_nodes.index)
assert isinstance(section[5], sphinx_nodes.desc)
assert section[5].astext() == 'hello\n\nGreet a user.'
assert isinstance(section[6], sphinx_nodes.index)
assert isinstance(section[7], sphinx_nodes.desc)
assert section[7].astext() == 'world\n\nGreet the world.'


def test_commands(make_app, rootdir):
srcdir = rootdir / 'commands'
app = make_app('xml', srcdir=srcdir)
app.build()

# TODO: rather than using the pickled doctree, we should decode the XML
content = pickle.loads((app.doctreedir / 'index.doctree').read_bytes())

# doc has format like so:
#
# document:
# section:
# title:
# section:
# title:
# paragraph:
# literal_block:
# rubric:
# index:
# desc:
# desc_signature:
# desc_signature:

section = content[0][1]
assert isinstance(section, nodes.section)

assert isinstance(section[0], nodes.title)
assert section[0].astext() == 'greet'
assert isinstance(section[1], nodes.paragraph)
assert section[1].astext() == 'A sample command group.'
assert isinstance(section[2], nodes.literal_block)

# we should only show a single command, 'world'
assert isinstance(section[3], nodes.rubric)
assert section[3].astext() == 'Commands'
assert isinstance(section[4], sphinx_nodes.index)
assert isinstance(section[5], sphinx_nodes.desc)
assert section[5].astext() == 'world\n\nGreet the world.'


def test_nested_full(make_app, rootdir):
Expand Down

0 comments on commit 37742be

Please sign in to comment.