Skip to content

Commit 7184b41

Browse files
AutonomicPerfectionistcmakepp[bot]ryanmrichard
authored
Document CTest add_test() (#128)
* update VERSION.txt * update VERSION.txt * Add CTest add_test() processor and documentation type * Add config options for documenting undocumented add_test() invocations * Delete VERSION.txt * Add tests for documenting CTest tests * Add sample and corr RST for ctest add_test * Add documentation for documenting ctest add_test() * Add test for incorrect ctest add_test params * Add additional error checking test for documenting ctest add_test() * Fix minor comment issue Co-authored-by: cmakepp[bot] <cmakepp[bot]@github.com> Co-authored-by: Ryan Richard <ryanmrichard1@gmail.com>
1 parent b6436f0 commit 7184b41

File tree

16 files changed

+249
-4
lines changed

16 files changed

+249
-4
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. Copyright 2021 CMakePP
2+
..
3+
.. Licensed under the Apache License, Version 2.0 (the "License");
4+
.. you may not use this file except in compliance with the License.
5+
.. You may obtain a copy of the License at
6+
..
7+
.. http://www.apache.org/licenses/LICENSE-2.0
8+
..
9+
.. Unless required by applicable law or agreed to in writing, software
10+
.. distributed under the License is distributed on an "AS IS" BASIS,
11+
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
.. See the License for the specific language governing permissions and
13+
.. limitations under the License.
14+
..
15+
############################
16+
Documenting a CTest Test
17+
############################
18+
19+
CMinx can be used to document a CTest test created by the
20+
`add_test() <https://cmake.org/cmake/help/latest/command/add_test.html>`_ command.
21+
Again, this is done analogous to other documentation use cases, *i.e.*, by
22+
proceeding the ``add_test()`` command with a
23+
documentation comment. For example,
24+
25+
.. literalinclude:: ../../../tests/test_samples/ctest_add_test.cmake
26+
:language: cmake
27+
28+
which generates:
29+
30+
.. literalinclude:: ../../../tests/test_samples/corr_rst/ctest_add_test.rst
31+
:language: rst

src/cminx/aggregator.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from .documentation_types import AttributeDocumentation, FunctionDocumentation, MacroDocumentation, \
1818
VariableDocumentation, GenericCommandDocumentation, ClassDocumentation, TestDocumentation, SectionDocumentation, \
19-
MethodDocumentation, VarType
19+
MethodDocumentation, VarType, CTestDocumentation
2020
from .parser.CMakeListener import CMakeListener
2121
# Annoyingly, the Antl4 Python libraries use camelcase since it was originally Java, so we have convention
2222
# inconsistencies here
@@ -363,6 +363,43 @@ def process_cpp_attr(self, ctx: CMakeParser.Command_invocationContext, docstring
363363
clazz.attributes.append(AttributeDocumentation(
364364
name, docstring, parent_class, default_values))
365365

366+
def process_add_test(self, ctx: CMakeParser.Command_invocationContext, docstring: str):
367+
"""
368+
Extracts information from a CTest add_test() command.
369+
Note: this is not the processor for the CMakeTest ct_add_test() command,
370+
but the processor for the vanilla CMake add_test() command.
371+
372+
:param ctx: Documented command context. Constructed by the Antlr4 parser.
373+
374+
:param docstring: Cleaned docstring.
375+
"""
376+
params = [param.getText() for param in ctx.single_argument()] # Extract parameters
377+
378+
if len(params) < 2:
379+
pretty_text = docstring
380+
pretty_text += f"\n{ctx.getText()}"
381+
382+
self.logger.error(
383+
f"ct_add_section() called with incorrect parameters: {params}\n\n{pretty_text}")
384+
return
385+
386+
name = ""
387+
for i in range(0, len(params)):
388+
param = params[i]
389+
if param.upper() == "NAME":
390+
try:
391+
name = params[i + 1]
392+
except IndexError:
393+
pretty_text = docstring
394+
pretty_text += f"\n{ctx.getText()}"
395+
396+
self.logger.error(f"add_test() called with incorrect parameters: {params}\n\n{pretty_text}")
397+
return
398+
399+
test_doc = CTestDocumentation(name, docstring, filter(lambda p: p != name and p != "NAME", params))
400+
self.documented.append(test_doc)
401+
self.documented_awaiting_function_def = test_doc
402+
366403
def process_generic_command(self, command_name: str, ctx: CMakeParser.Command_invocationContext, docstring: str):
367404
"""
368405
Extracts command invocation and arguments for a documented command that does not

src/cminx/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def config_template(output_dir_relative_to_config: bool = False) -> dict:
3434
"include_undocumented_cpp_constructor": bool,
3535
"include_undocumented_cpp_member": bool,
3636
"include_undocumented_ct_add_test": bool,
37+
"include_undocumented_add_test": bool,
3738
"include_undocumented_ct_add_section": bool,
3839
"auto_exclude_directories_without_cmake": bool,
3940
"exclude_filters": confuse.Optional(list, default=()),
@@ -68,6 +69,7 @@ class InputSettings:
6869
include_undocumented_cpp_member: bool = True
6970
include_undocumented_ct_add_test: bool = True
7071
include_undocumented_ct_add_section: bool = True
72+
include_undocumented_add_test: bool = True
7173
auto_exclude_directories_without_cmake: bool = True
7274
exclude_filters: List[str] = ()
7375
recursive: bool = False

src/cminx/config_default.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ input:
4545
# auto-generated.
4646
include_undocumented_ct_add_section: true
4747

48+
# Whether undocumented CTest tests should have documentation
49+
# auto-generated. This controls whether all vanilla
50+
# CMake add_test() commands should be documented,
51+
# it has no relation to CMakeTest tests.
52+
include_undocumented_add_test: true
53+
4854
# Whether directories not containing .cmake
4955
# files should be excluded from recursive mode searching.
5056
auto_exclude_directories_without_cmake: true

src/cminx/documentation_types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ def process(self, writer: RSTWriter):
161161
d.text(self.doc)
162162

163163

164+
@dataclass
165+
class CTestDocumentation(DocumentationType):
166+
"""
167+
This dataclass holds documentation information
168+
for a vanilla CTest test. These tests are created
169+
with the "add_test()" command from regular
170+
CMake, and documenting such a call will
171+
create this type of documentation.
172+
"""
173+
params: List[str] = field(default_factory=lambda: [])
174+
175+
def process(self, writer: RSTWriter):
176+
d = writer.directive(
177+
"function",
178+
f"{self.name}({' '.join(self.params)})")
179+
d.directive(
180+
"warning",
181+
'This is a CTest test definition, do not call this manually. '
182+
'Use the "ctest" program to execute this test.')
183+
d.text(self.doc)
184+
185+
164186
@dataclass
165187
class TestDocumentation(DocumentationType):
166188
"""

tests/examples/configs/no_include_undocumented_config.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ input:
3939

4040
# Whether undocumented test sections should have documentation
4141
# auto-generated.
42-
include_undocumented_ct_add_section: false
42+
include_undocumented_ct_add_section: false
43+
44+
# Whether undocumented CTest tests should have documentation
45+
# auto-generated. This controls whether all vanilla
46+
# CMake add_test() commands should be documented,
47+
# it has no relation to CMakeTest tests.
48+
include_undocumented_add_test: false

tests/examples/example.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,26 @@ function("${undocumented_test}")
251251
endfunction()
252252

253253
endfunction()
254+
255+
#[[[
256+
# This is a documented CTest test.
257+
# Note that this is a vanilla CMake
258+
# add_test() command, not a ct_add_test()
259+
# command
260+
#]]
261+
add_test(
262+
NAME ctest_test
263+
COMMAND bash -c echo test
264+
)
265+
266+
#[[
267+
# This is an udocumented CTest test.
268+
# Note that this is a vanilla CMake
269+
# add_test() command, not a ct_add_test()
270+
# command
271+
#]]
272+
add_test(
273+
NAME ctest_test_undocumented
274+
COMMAND bash -c echo test
275+
)
276+

tests/examples/sphinx/source/example.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,23 @@ examples.example
313313

314314

315315

316+
317+
.. function:: ctest_test(COMMAND bash -c echo test)
318+
319+
320+
.. warning:: This is a CTest test definition, do not call this manually. Use the "ctest" program to execute this test.
321+
322+
This is a documented CTest test.
323+
Note that this is a vanilla CMake
324+
add_test() command, not a ct_add_test()
325+
command
326+
327+
328+
329+
.. function:: ctest_test_undocumented(COMMAND bash -c echo test)
330+
331+
332+
.. warning:: This is a CTest test definition, do not call this manually. Use the "ctest" program to execute this test.
333+
334+
335+

tests/examples/sphinx/source/example_no_undocumented.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,15 @@ examples.example
121121
This is a nested class
122122

123123

124+
125+
.. function:: ctest_test(COMMAND bash -c echo test)
126+
127+
128+
.. warning:: This is a CTest test definition, do not call this manually. Use the "ctest" program to execute this test.
129+
130+
This is a documented CTest test.
131+
Note that this is a vanilla CMake
132+
add_test() command, not a ct_add_test()
133+
command
134+
135+

tests/examples/sphinx/source/example_no_undocumented_diff_header.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,15 @@ examples.example.cmake
121121
This is a nested class
122122

123123

124+
125+
.. function:: ctest_test(COMMAND bash -c echo test)
126+
127+
128+
.. warning:: This is a CTest test definition, do not call this manually. Use the "ctest" program to execute this test.
129+
130+
This is a documented CTest test.
131+
Note that this is a vanilla CMake
132+
add_test() command, not a ct_add_test()
133+
command
134+
135+

0 commit comments

Comments
 (0)