| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ansible import constants as C |
| 6 |
from ansible.cli.doc import DocCLI, RoleMixin |
| 7 |
from ansible.plugins.loader import module_loader, init_plugin_loader |
| 8 |
|
| 9 |
|
| 10 |
C.ANSIBLE_NOCOLOR = True |
| 11 |
TTY_IFY_DATA = { |
| 12 |
# No substitutions |
| 13 |
'no-op': 'no-op', |
| 14 |
'no-op Z(test)': 'no-op Z(test)', |
| 15 |
# Simple cases of all substitutions |
| 16 |
'I(italic)': "`italic`", |
| 17 |
'B(bold)': '*bold*', |
| 18 |
'M(ansible.builtin.module)': '[ansible.builtin.module]', |
| 19 |
'U(https://docs.ansible.com)': 'https://docs.ansible.com', |
| 20 |
'L(the user guide,https://docs.ansible.com/user-guide.html)': 'the user guide <https://docs.ansible.com/user-guide.html>', |
| 21 |
'R(the user guide,user-guide)': 'the user guide', |
| 22 |
'C(/usr/bin/file)': "`/usr/bin/file'", |
| 23 |
'HORIZONTALLINE': '\n{0}\n'.format('-' * 13), |
| 24 |
# Multiple substitutions |
| 25 |
'The M(ansible.builtin.yum) module B(MUST) be given the C(package) parameter. See the R(looping docs,using-loops) for more info': |
| 26 |
"The [ansible.builtin.yum] module *MUST* be given the `package' parameter. See the looping docs for more info", |
| 27 |
# Problem cases |
| 28 |
'IBM(International Business Machines)': 'IBM(International Business Machines)', |
| 29 |
'L(the user guide, https://docs.ansible.com/)': 'the user guide <https://docs.ansible.com/>', |
| 30 |
'R(the user guide, user-guide)': 'the user guide', |
| 31 |
# de-rsty refs and anchors |
| 32 |
'yolo :ref:`my boy` does stuff': 'yolo `my boy` does stuff', |
| 33 |
'.. seealso:: Something amazing': 'See also: Something amazing', |
| 34 |
'.. seealso:: Troublesome multiline\n Stuff goes htere': 'See also: Troublesome multiline\n Stuff goes htere', |
| 35 |
'.. note:: boring stuff': 'Note: boring stuff', |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.parametrize('text, expected', sorted(TTY_IFY_DATA.items())) |
| 40 |
def test_ttyify(text, expected): |
| 41 |
assert DocCLI.tty_ify(text) == expected |
| 42 |
|
| 43 |
|
| 44 |
def test_rolemixin__build_summary(): |
| 45 |
obj = RoleMixin() |
| 46 |
role_name = 'test_role' |
| 47 |
collection_name = 'test.units' |
| 48 |
argspec = { |
| 49 |
'main': {'short_description': 'main short description'}, |
| 50 |
'alternate': {'short_description': 'alternate short description'}, |
| 51 |
} |
| 52 |
meta = {} |
| 53 |
expected = { |
| 54 |
'collection': collection_name, |
| 55 |
'description': 'UNDOCUMENTED', |
| 56 |
'entry_points': { |
| 57 |
'main': argspec['main']['short_description'], |
| 58 |
'alternate': argspec['alternate']['short_description'], |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
fqcn, summary = obj._build_summary(role_name, collection_name, meta, argspec) |
| 63 |
assert fqcn == '.'.join([collection_name, role_name]) |
| 64 |
assert summary == expected |
| 65 |
|
| 66 |
|
| 67 |
def test_rolemixin__build_summary_empty_argspec(): |
| 68 |
obj = RoleMixin() |
| 69 |
role_name = 'test_role' |
| 70 |
collection_name = 'test.units' |
| 71 |
argspec = {} |
| 72 |
meta = {} |
| 73 |
expected = { |
| 74 |
'collection': collection_name, |
| 75 |
'description': 'UNDOCUMENTED', |
| 76 |
'entry_points': {} |
| 77 |
} |
| 78 |
|
| 79 |
fqcn, summary = obj._build_summary(role_name, collection_name, meta, argspec) |
| 80 |
assert fqcn == '.'.join([collection_name, role_name]) |
| 81 |
assert summary == expected |
| 82 |
|
| 83 |
|
| 84 |
def test_rolemixin__build_doc(): |
| 85 |
obj = RoleMixin() |
| 86 |
role_name = 'test_role' |
| 87 |
path = '/a/b/c' |
| 88 |
collection_name = 'test.units' |
| 89 |
entrypoint_filter = 'main' |
| 90 |
argspec = { |
| 91 |
'main': {'short_description': 'main short description'}, |
| 92 |
'alternate': {'short_description': 'alternate short description'}, |
| 93 |
} |
| 94 |
expected = { |
| 95 |
'path': path, |
| 96 |
'collection': collection_name, |
| 97 |
'entry_points': { |
| 98 |
'main': argspec['main'], |
| 99 |
} |
| 100 |
} |
| 101 |
fqcn, doc = obj._build_doc(role_name, path, collection_name, argspec, entrypoint_filter) |
| 102 |
assert fqcn == '.'.join([collection_name, role_name]) |
| 103 |
assert doc == expected |
| 104 |
|
| 105 |
|
| 106 |
def test_rolemixin__build_doc_no_filter_match(): |
| 107 |
obj = RoleMixin() |
| 108 |
role_name = 'test_role' |
| 109 |
path = '/a/b/c' |
| 110 |
collection_name = 'test.units' |
| 111 |
entrypoint_filter = 'doesNotExist' |
| 112 |
argspec = { |
| 113 |
'main': {'short_description': 'main short description'}, |
| 114 |
'alternate': {'short_description': 'alternate short description'}, |
| 115 |
} |
| 116 |
fqcn, doc = obj._build_doc(role_name, path, collection_name, argspec, entrypoint_filter) |
| 117 |
assert fqcn == '.'.join([collection_name, role_name]) |
| 118 |
assert doc is None |
| 119 |
|
| 120 |
|
| 121 |
def test_builtin_modules_list(): |
| 122 |
args = ['ansible-doc', '-l', 'ansible.builtin', '-t', 'module'] |
| 123 |
obj = DocCLI(args=args) |
| 124 |
obj.parse() |
| 125 |
init_plugin_loader() |
| 126 |
result = obj._list_plugins('module', module_loader) |
| 127 |
assert len(result) > 0 |
| 128 |
|
| 129 |
|
| 130 |
def test_legacy_modules_list(): |
| 131 |
args = ['ansible-doc', '-l', 'ansible.legacy', '-t', 'module'] |
| 132 |
obj = DocCLI(args=args) |
| 133 |
obj.parse() |
| 134 |
result = obj._list_plugins('module', module_loader) |
| 135 |
assert len(result) > 0 |