Skip to content

Commit

Permalink
Rename to hidetype, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Heselmans authored and Stein Heselmans committed Dec 17, 2024
1 parent 32d3a16 commit e441c34
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
17 changes: 12 additions & 5 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ Documentation items can be defined using the *item* directive, specifying:
:status: Approved
:validated_by: ITEST-MY_FIRST_INTEGRATION_TEST
:ext_polarion_reference: project_x:workitem_y
:hidetype: validated_by
:nocaptions:
:hidelinks: validated_by
According to the Polarion reference, the software **shall** implement my first requirement.
Attributes can be added to the item, using the configured attribute keys in :ref:`traceability_default_config`
(e.g. *value* in the above example). The content of the attribute is treated as a single string and should
match_ the regular expression in configuration.

The *hidelinks* is a space-seperated list of relation types. The relations to other items will not be shown for the
mentioned relation types below the item. The links will still be used and displayed in traceability matrixes and other directives.

The relations to other documentation items can be specified as:

- a space-separated list of item ID's, or
Expand All @@ -78,7 +75,17 @@ The relations to other documentation items can be specified as:
ITEST-MY_SECOND_INTEGRATION_TEST
The output will contain hyperlinks to all related items. By default, the caption for the target item is displayed for
each of these related items. With the option *nocaptions* these captions can be omitted.
each of these related items.

:hidetype: *optional*, *multiple arguments (space-separated)*

A space-separated list of relation types. By default, the rendered item definition displays all relations to other items
in a list below the item name (id). The relation types given to this option will be omitted from this list.
It does not affect how the item is rendered in other directives, e.g. item-matrix.

:nocaptions: *optional*, *flag*

Omits the caption from the rendered item definition.

.. _adding_relations:

Expand Down
6 changes: 3 additions & 3 deletions mlx/traceability/directives/item_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _process_relationships(self, *args):
All targets get naturally sorted per relationship.
"""
for rel, targets in self._item.all_relations:
if rel not in self['hidelinks']:
if rel not in self['hidetype']:
self._list_targets_for_relation(rel, targets, *args)

def _list_targets_for_relation(self, relation, targets, dl_node, app):
Expand Down Expand Up @@ -130,7 +130,7 @@ class ItemDirective(TraceableBaseDirective):
option_spec = {
'class': directives.class_option,
'nocaptions': directives.flag,
'hidelinks': directives.unchanged,
'hidetype': directives.unchanged,
}
# Content allowed
has_content = True
Expand All @@ -154,7 +154,7 @@ def run(self):
self.process_options(
item_node,
{
'hidelinks': {'default': []},
'hidetype': {'default': []},
},
)
item = self._store_item_info(target_id, env)
Expand Down
2 changes: 1 addition & 1 deletion tests/directives/test_item_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUp(self):
self.node['document'] = 'some_doc'
self.node['id'] = 'some_id'
self.node['line'] = 1
self.node['hidelinks'] = []
self.node['hidetype'] = []
self.item = TraceableItem(self.node['id'])
self.item.set_location(self.node['document'], self.node['line'])
self.item.node = self.node
Expand Down
116 changes: 116 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import os
import shutil
import tempfile
import textwrap
import unittest
import sphinx.cmd.build
import sphinx.cmd.quickstart

class TestSphinx(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.docs_folder = tempfile.mkdtemp()
cls.rst_file = os.path.join(cls.docs_folder, "index.rst")
cls.html_file = os.path.join(cls.docs_folder, "_build", "index.html")
sphinx.cmd.quickstart.generate({
"path": cls.docs_folder,
"sep": False,
"project": "testdoc",
"author": "mlx.traceability contributors",
"version": "a",
"release": "a",
"language": "en",
"dot": "_",
"suffix": ".rst",
"master": "index",
"makefile": True,
"batchfile": False,
"extensions": ["mlx.traceability"],
}, silent=True)

with open(f'{cls.docs_folder}/conf.py', 'a') as cfg:
cfg.write("traceability_render_relationship_per_item = True\n")
cfg.write("traceability_relationships = {'my_relation': 'my_reverse_relation'}\n")
cfg.write("traceability_relationship_to_string = {'my_relation': 'My relation', 'my_reverse_relation': 'My reverse relation'}\n")
cfg.write("traceability_attributes = {}\n")
cfg.write("traceability_attributes_sort = {}\n")

@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.docs_folder)

def build(self, builder="html"):
retcode = sphinx.cmd.build.main([
"-q",
"-b",
builder,
self.docs_folder,
os.path.join(self.docs_folder, "_build"),
])
self.assertEqual(retcode, 0)

def test_item_relation(self):
with open(os.path.join(self.docs_folder, self.rst_file), "w") as f:
f.write(textwrap.dedent(r"""
.. item:: ITEM-A Description of item-a
.. item:: ITEM-B Description of item-b
:my_relation: ITEM-A
"""))

self.build(builder="html")
with open(self.html_file, "r") as f:
content = f.read()
assert 'My relation' in content
assert 'My reverse relation' in content

def test_item_relation_hide_forward(self):
with open(os.path.join(self.docs_folder, self.rst_file), "w") as f:
f.write(textwrap.dedent(r"""
.. item:: ITEM-A Description of item-a
.. item:: ITEM-B Description of item-b
:my_relation: ITEM-A
:hidetype: my_relation
"""))

self.build(builder="html")
with open(self.html_file, "r") as f:
content = f.read()
assert 'My relation' not in content
assert 'My reverse relation' in content

def test_item_relation_hide_reverse(self):
with open(os.path.join(self.docs_folder, self.rst_file), "w") as f:
f.write(textwrap.dedent(r"""
.. item:: ITEM-A Description of item-a
:hidetype: my_reverse_relation
.. item:: ITEM-B Description of item-b
:my_relation: ITEM-A
"""))

self.build(builder="html")
with open(self.html_file, "r") as f:
content = f.read()
assert 'My relation' in content
assert 'My reverse relation' not in content

def test_item_relation_hide_both(self):
with open(os.path.join(self.docs_folder, self.rst_file), "w") as f:
f.write(textwrap.dedent(r"""
.. item:: ITEM-A Description of item-a
:hidetype: my_reverse_relation
.. item:: ITEM-B Description of item-b
:my_relation: ITEM-A
:hidetype: my_relation
"""))

self.build(builder="html")
with open(self.html_file, "r") as f:
content = f.read()
assert 'My relation' not in content
assert 'My reverse relation' not in content

0 comments on commit e441c34

Please sign in to comment.