Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag glossary #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sphinx_gherkindoc"
version = "3.6.4"
version = "3.6.5"
description = "A tool to convert Gherkin into Sphinx documentation"
authors = ["Lewis Franklin <[email protected]>", "Doug Philips <[email protected]>"]
readme = "README.rst"
Expand Down
23 changes: 22 additions & 1 deletion sphinx_gherkindoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .files import is_feature_file, is_rst_file, scan_tree
from .glossary import make_steps_glossary
from .tag_management import make_tag_list
from .parsers import parsers
from .utils import make_flat_name, set_dry_run, set_verbose, verbose
from .writer import feature_to_rst, toctree
Expand Down Expand Up @@ -47,7 +48,7 @@ def process_args(
group_step_glossary = args.group_step_glossary
doc_project = args.doc_project
root_path = gherkin_path.resolve().parent

tag_list_name = args.tag_list_name
top_level_toc_filename = output_path / f"{toc_name}.rst"

non_empty_dirs: Set[pathlib.Path] = set()
Expand Down Expand Up @@ -160,6 +161,20 @@ def process_args(
verbose(f"Writing sphinx glossary: {glossary_filename}")
glossary.write_to_file(glossary_filename)

if tag_list_name:
taglist_dox = make_tag_list(doc_project)
if args.dry_run:
verbose("No taglist generated")
return

if taglist_dox is None:
print("No tags to include in the tag list: no tag list generated")
return

taglist_filename = output_path / f"{tag_list_name}.rst"
verbose(f"Writing sphinx tag list: {taglist_filename}")
taglist_dox.write_to_file(taglist_filename)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block looks like it was copied from the glossary block right above it.
Being that the previous block has 'return' statements in it, the code might never get to this block.
And if another block is added after this one, a return here might prevent the code from getting to that.
This means restructuring the two blocks. My initial feeling is that this function is already too big :-) but that is a pre-existing condition for this change. I think in the interests of simplicity, I'm willing to leave this as is unless you want to undertake refactoring the glossary and this new tags into helper functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at this moment @dgou 😆


def main() -> None:
"""Convert a directory-tree of Gherkin Feature files to rST files."""
Expand Down Expand Up @@ -234,6 +249,12 @@ def main() -> None:
"is also included."
),
)
parser.add_argument(
"-L",
"--tag-list-name",
default=None,
help=("Print a list of all tags available for use"),
)
parser.add_argument(
"--parser",
default="behave",
Expand Down
32 changes: 32 additions & 0 deletions sphinx_gherkindoc/tag_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Module specific functions for tag management and document building."""
from .utils import SphinxWriter
from typing import Set, Optional
from functools import reduce


tag_set: Set[str] = set()


def make_tag_list(project_name: str) -> Optional[SphinxWriter]:
"""Make caller to finalize a tag list."""
if not tag_set:
return None
tag_list_sphinx_doc = SphinxWriter()
tag_list_sphinx_doc = tag_list_preamble(tag_list_sphinx_doc, project_name)
tag_list_sphinx_doc = reduce(
lambda acc, t: add_tag(acc, t), sorted(tag_set), tag_list_sphinx_doc
)
return tag_list_sphinx_doc


def tag_list_preamble(sphinx_dox: SphinxWriter, project_name: str) -> SphinxWriter:
"""Add a preamble to the tag list."""
sphinx_dox.create_section(1, f"{project_name} Tag List")
return sphinx_dox


def add_tag(sphinx_dox: SphinxWriter, tag: str) -> SphinxWriter:
"""Map function over tag list."""
sphinx_dox.add_output(f"* {tag}")
sphinx_dox.blank_line()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the extra blank line (it's more apparent in the test code that the unnumbered list has the extra line.
Not sure if we don't have a helper for that, I need to look at this more in depth.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the pattern in other places where we build sphinx documents.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok then, you can just use the line_breaks keyword parameter instead, for example: https://github.com/jolly-good-toolbelt/sphinx_gherkindoc/blob/master/sphinx_gherkindoc/writer.py#L105

return sphinx_dox
3 changes: 3 additions & 0 deletions sphinx_gherkindoc/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .files import is_rst_file
from .glossary import step_glossary, step_glossary_grouped
from .tag_management import tag_set
from .parsers import parsers, ClassWithExamples
from .utils import (
display_name,
Expand Down Expand Up @@ -244,6 +245,8 @@ def ticket_url_or_tag(tag: str) -> str:
return _value_with_url(tag, url) if url else tag

def tags(tags: List[str], *parent_objs: behave.model_core.BasicStatement) -> None:
# this appends the tags to the previous set
tag_set.update(tags)
parent_with_tags = tuple(x for x in parent_objs if x.tags)
if not (tags or parent_with_tags):
return
Expand Down
26 changes: 26 additions & 0 deletions tests/test_tag_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests for tag list."""
import pytest
import copy
from sphinx_gherkindoc import tag_management


@pytest.fixture()
def tag_list():
old_value = copy.deepcopy(tag_management.tag_set)
tag_management.tag_set = {"Tag1", "Tag2", "Tag3"}
yield
tag_management.tag_set = old_value


def test_tag_list_output(tag_list):
tag_list_output = [
"Test Tag List\n",
"=============\n\n",
"* Tag1\n",
"\n",
"* Tag2\n",
"\n",
"* Tag3\n",
"\n",
]
assert tag_list_output == tag_management.make_tag_list("Test")._output