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

Added possibility for directives to have 'extra-options' defined in c… #84

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
39 changes: 36 additions & 3 deletions sphinxcontrib/test_reports/directives/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from docutils.parsers.rst import directives
from sphinx_needs.api import add_need
from sphinx_needs.utils import add_doc
from sphinx_needs.config import NeedsSphinxConfig

from sphinxcontrib.test_reports.directives.test_common import TestCommonDirective
from sphinxcontrib.test_reports.exceptions import TestReportInvalidOption
Expand Down Expand Up @@ -33,9 +34,6 @@ class TestCaseDirective(TestCommonDirective):

final_argument_whitespace = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def run(self, nested=False, suite_count=-1, case_count=-1):
self.prepare_basic_options()
self.load_test_file()
Expand Down Expand Up @@ -163,6 +161,40 @@ def run(self, nested=False, suite_count=-1, case_count=-1):

main_section = []
docname = self.state.document.settings.env.docname
needs_config = NeedsSphinxConfig(self.env.config)
extra_links = needs_config.extra_links
extra_options = needs_config.extra_options
specified_opts = (
"docname",
"lineno",
"type",
"title",
"id",
"content",
"links",
"tags",
"status",
"collapse",
"file",
"suite",
"case",
"case_name",
"case_parameter",
"classname",
"result",
"time",
"style",
"passed",
"skipped",
"failed",
"errors",
)
need_extra_options = {}
extra_links_options = [x["option"] for x in extra_links]
all_options = extra_links_options + list(extra_options.keys())
for extra_option in all_options:
if extra_option not in specified_opts:
need_extra_options[extra_option] = self.options.get(extra_option, "")
main_section += add_need(
self.app,
self.state,
Expand All @@ -185,6 +217,7 @@ def run(self, nested=False, suite_count=-1, case_count=-1):
result=result,
time=time,
style=style,
**need_extra_options
)

add_doc(self.env, docname)
Expand Down
38 changes: 33 additions & 5 deletions sphinxcontrib/test_reports/directives/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
# fmt: off
import os

from docutils.parsers.rst import Directive
from docutils.parsers.rst import Directive, directives
from sphinx.util import logging
from sphinx_needs.api import make_hashed_id
from sphinx_needs.api.need import _make_hashed_id
from sphinx_needs.config import NeedsSphinxConfig

from sphinxcontrib.test_reports.exceptions import (
SphinxError, TestReportFileNotSetException)
Expand All @@ -21,6 +22,35 @@ class TestCommonDirective(Directive):
Common directive, which provides some shared functions to "real" directives.
"""

@classmethod
def update_option_spec(cls, app):
"""
Adding extra_options & extra_links defined via sphinx_needs to 'allowed' options
"""
needs_config = NeedsSphinxConfig(app.config)

if not hasattr(cls, 'option_spec'):
cls.option_spec = {}
elif cls.option_spec is None:
cls.option_spec = {}
new_options = dict(getattr(cls, 'option_spec', {}) or {})

extra_options = getattr(needs_config, 'extra_options', None)
if extra_options is None:
extra_options = {}
extra_links = getattr(needs_config, 'extra_links', None)
if extra_links is None:
extra_links = {}

extra_links_options = [x["option"] for x in extra_links]
all_options = extra_links_options + list(extra_options.keys())

for option in all_options:
if option not in new_options:
new_options[option] = directives.unchanged

cls.option_spec = new_options

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.env = self.state.document.settings.env
Expand Down Expand Up @@ -90,9 +120,7 @@ def prepare_basic_options(self):
self.need_type = self.app.tr_types[self.name][0]
self.test_id = self.options.get(
"id",
make_hashed_id(
self.app, self.need_type, self.test_name, self.test_content
),
_make_hashed_id(self.need_type, self.test_name, self.test_content, NeedsSphinxConfig(self.app.config))
)
else:
self.test_id = self.options.get("id")
Expand Down
35 changes: 31 additions & 4 deletions sphinxcontrib/test_reports/directives/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from docutils.parsers.rst import directives
from sphinx_needs.api import add_need
from sphinx_needs.utils import add_doc
from sphinx_needs.config import NeedsSphinxConfig

import sphinxcontrib.test_reports.directives.test_suite
from sphinxcontrib.test_reports.directives.test_common import TestCommonDirective
Expand Down Expand Up @@ -35,11 +36,36 @@ class TestFileDirective(TestCommonDirective):

final_argument_whitespace = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.suite_ids = {}

def run(self):
self.suite_ids = {}
needs_config = NeedsSphinxConfig(self.env.config)
extra_links = needs_config.extra_links
extra_options = needs_config.extra_options
specified_opts = (
"docname",
"lineno",
"type",
"title",
"id",
"content",
"links",
"tags",
"status",
"collapse",
"file",
"suites",
"cases",
"passed",
"skipped",
"failed",
"errors",
)
need_extra_options = {}
extra_links_options = [x["option"] for x in extra_links]
all_options = extra_links_options + list(extra_options.keys())
for extra_option in all_options:
if extra_option not in specified_opts:
need_extra_options[extra_option] = self.options.get(extra_option, "")
self.prepare_basic_options()
results = self.load_test_file()

Expand Down Expand Up @@ -85,6 +111,7 @@ def run(self):
skipped=skipped,
failed=failed,
errors=errors,
**need_extra_options
)

if (
Expand Down
35 changes: 31 additions & 4 deletions sphinxcontrib/test_reports/directives/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from docutils.parsers.rst import directives
from sphinx_needs.api import add_need
from sphinx_needs.utils import add_doc
from sphinx_needs.config import NeedsSphinxConfig

import sphinxcontrib.test_reports.directives.test_case
from sphinxcontrib.test_reports.directives.test_common import TestCommonDirective
Expand Down Expand Up @@ -34,11 +35,8 @@ class TestSuiteDirective(TestCommonDirective):

final_argument_whitespace = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def run(self, nested=False, count=-1, **kwargs):
self.case_ids = []

def run(self, nested=False, count=-1):
self.prepare_basic_options()
self.load_test_file()

Expand Down Expand Up @@ -75,6 +73,34 @@ def run(self, nested=False, count=-1):

main_section = []
docname = self.state.document.settings.env.docname
needs_config = NeedsSphinxConfig(self.env.config)
extra_links = needs_config.extra_links
extra_options = needs_config.extra_options
specified_opts = (
"docname",
"lineno",
"type",
"title",
"id",
"content",
"links",
"tags",
"status",
"collapse",
"file",
"suite",
"cases",
"passed",
"skipped",
"failed",
"errors",
)
need_extra_options = {}
extra_links_options = [x["option"] for x in extra_links]
all_options = extra_links_options + list(extra_options.keys())
for extra_option in all_options:
if extra_option not in specified_opts:
need_extra_options[extra_option] = self.options.get(extra_option, "")
main_section += add_need(
self.app,
self.state,
Expand All @@ -95,6 +121,7 @@ def run(self, nested=False, count=-1):
skipped=skipped,
failed=failed,
errors=errors,
**need_extra_options
)

# TODO double nested logic
Expand Down
13 changes: 13 additions & 0 deletions sphinxcontrib/test_reports/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def setup(app):
app.connect("env-updated", install_styles_static_files)
app.connect("config-inited", tr_preparation)
app.connect("config-inited", sphinx_needs_update)
app.connect("env-before-read-docs", add_extra_options_to_directives)

return {
"version": VERSION, # identifies the version of our extension
Expand Down Expand Up @@ -179,3 +180,15 @@ def sphinx_needs_update(app, *args):
add_need_type(app, *app.config.tr_file[1:])
add_need_type(app, *app.config.tr_suite[1:])
add_need_type(app, *app.config.tr_case[1:])


def add_extra_options_to_directives(app, env, *args, **kwargs):
"""
Add 'needs_extra_options' to the 'opt_spec' of the directives.
In order to allow them to have said directives inside rst files
"""
if not hasattr(env.config, 'needs_extra_options'):
env.config.needs_extra_options = []
TestCaseDirective.update_option_spec(app)
TestSuiteDirective.update_option_spec(app)
TestFileDirective.update_option_spec(app)
2 changes: 1 addition & 1 deletion tests/doc_test/doc_test_file/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
# The master toctree document.
master_doc = "index"

needs_extra_options = ["asil", "uses_secure"]
needs_extra_options = ["asil", "uses_secure", "verifies"]

# General information about the project.
project = "test-report test docs"
Expand Down
1 change: 1 addition & 0 deletions tests/doc_test/doc_test_file/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Basic Document FOR TEST FILE

.. test-file:: My Test Data
:file: ../utils/xml_data.xml
:verifies: This is an extra option
:id: TESTFILE_1


Expand Down
2 changes: 0 additions & 2 deletions tests/test_custom_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ def test_custom_koi8_template(test_app):
app.build()
html = Path(app.outdir / "index.html").read_text(encoding="utf8")

print(html)

assert "бцдеф" in html
assert "Testfбlle" in html
1 change: 1 addition & 0 deletions tests/test_test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_doc_build_html(test_app):
app.build()
html = Path(app.outdir, "index.html").read_text()
assert html
assert "This is an extra option" in html


@pytest.mark.parametrize(
Expand Down