| 1 |
"""Test pycode.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import sys |
| 6 |
from importlib.machinery import SourceFileLoader |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
import sphinx |
| 12 |
from sphinx.errors import PycodeError |
| 13 |
from sphinx.pycode import ModuleAnalyzer |
| 14 |
|
| 15 |
SPHINX_MODULE_PATH = Path(sphinx.__file__).resolve().with_suffix('.py') |
| 16 |
|
| 17 |
|
| 18 |
def test_ModuleAnalyzer_get_module_source() -> None: |
| 19 |
assert isinstance(sphinx.__spec__.loader, SourceFileLoader) # type checking |
| 20 |
assert ModuleAnalyzer.get_module_source('sphinx') == ( |
| 21 |
Path(sphinx.__file__), |
| 22 |
sphinx.__spec__.loader.get_source('sphinx'), |
| 23 |
) |
| 24 |
|
| 25 |
# failed to obtain source information from builtin modules |
| 26 |
with pytest.raises(PycodeError): |
| 27 |
ModuleAnalyzer.get_module_source('builtins') |
| 28 |
with pytest.raises(PycodeError): |
| 29 |
ModuleAnalyzer.get_module_source('itertools') |
| 30 |
|
| 31 |
|
| 32 |
def test_ModuleAnalyzer_for_string() -> None: |
| 33 |
analyzer = ModuleAnalyzer.for_string('print("Hello world")', 'module_name') |
| 34 |
assert analyzer.modname == 'module_name' |
| 35 |
assert analyzer.srcname == '<string>' |
| 36 |
|
| 37 |
|
| 38 |
def test_ModuleAnalyzer_for_file() -> None: |
| 39 |
analyzer = ModuleAnalyzer.for_file(SPHINX_MODULE_PATH, 'sphinx') |
| 40 |
assert analyzer.modname == 'sphinx' |
| 41 |
assert analyzer.srcname == str(SPHINX_MODULE_PATH) |
| 42 |
|
| 43 |
|
| 44 |
def test_ModuleAnalyzer_for_module(rootdir: Path) -> None: |
| 45 |
analyzer = ModuleAnalyzer.for_module('sphinx') |
| 46 |
assert analyzer.modname == 'sphinx' |
| 47 |
assert analyzer.srcname == str(SPHINX_MODULE_PATH) |
| 48 |
|
| 49 |
saved_path = sys.path.copy() |
| 50 |
sys.path.insert(0, str(rootdir / 'test-pycode')) |
| 51 |
try: |
| 52 |
analyzer = ModuleAnalyzer.for_module('cp_1251_coded') |
| 53 |
docs = analyzer.find_attr_docs() |
| 54 |
assert docs == {('', 'X'): ['It MUST look like X="\u0425"', '']} |
| 55 |
finally: |
| 56 |
sys.path[:] = saved_path |
| 57 |
|
| 58 |
|
| 59 |
def test_ModuleAnalyzer_find_tags() -> None: |
| 60 |
code = ( |
| 61 |
'class Foo(object):\n' # line: 1 |
| 62 |
' """class Foo!"""\n' |
| 63 |
' def __init__(self):\n' |
| 64 |
' pass\n' |
| 65 |
'\n' |
| 66 |
' def bar(self, arg1, arg2=True, *args, **kwargs):\n' |
| 67 |
' """method Foo.bar"""\n' |
| 68 |
' pass\n' |
| 69 |
'\n' |
| 70 |
' class Baz(object):\n' |
| 71 |
' def __init__(self):\n' # line: 11 |
| 72 |
' pass\n' |
| 73 |
'\n' |
| 74 |
'def qux():\n' |
| 75 |
' """function baz"""\n' |
| 76 |
' pass\n' |
| 77 |
'\n' |
| 78 |
'@decorator1\n' |
| 79 |
'@decorator2\n' |
| 80 |
'def quux():\n' |
| 81 |
' pass\n' # line: 21 |
| 82 |
'\n' |
| 83 |
'class Corge(object):\n' |
| 84 |
' @decorator1\n' |
| 85 |
' @decorator2\n' |
| 86 |
' def grault(self):\n' |
| 87 |
' pass\n' |
| 88 |
) |
| 89 |
analyzer = ModuleAnalyzer.for_string(code, 'module') |
| 90 |
tags = analyzer.find_tags() |
| 91 |
assert set(tags.keys()) == { |
| 92 |
'Foo', |
| 93 |
'Foo.__init__', |
| 94 |
'Foo.bar', |
| 95 |
'Foo.Baz', |
| 96 |
'Foo.Baz.__init__', |
| 97 |
'qux', |
| 98 |
'quux', |
| 99 |
'Corge', |
| 100 |
'Corge.grault', |
| 101 |
} |
| 102 |
assert tags['Foo'] == ('class', 1, 12) # type, start, end |
| 103 |
assert tags['Foo.__init__'] == ('def', 3, 4) |
| 104 |
assert tags['Foo.bar'] == ('def', 6, 8) |
| 105 |
assert tags['Foo.Baz'] == ('class', 10, 12) |
| 106 |
assert tags['Foo.Baz.__init__'] == ('def', 11, 12) |
| 107 |
assert tags['qux'] == ('def', 14, 16) |
| 108 |
assert tags['quux'] == ('def', 18, 21) |
| 109 |
assert tags['Corge'] == ('class', 23, 27) |
| 110 |
assert tags['Corge.grault'] == ('def', 24, 27) |
| 111 |
|
| 112 |
|
| 113 |
def test_ModuleAnalyzer_find_attr_docs() -> None: |
| 114 |
code = ( |
| 115 |
'class Foo(object):\n' |
| 116 |
' """class Foo!"""\n' |
| 117 |
' #: comment before attr1\n' |
| 118 |
' attr1 = None\n' |
| 119 |
' attr2 = None # attribute comment for attr2 (without colon)\n' |
| 120 |
' attr3 = None #: attribute comment for attr3\n' |
| 121 |
' attr4 = None #: long attribute comment\n' |
| 122 |
' #: for attr4\n' |
| 123 |
' #: comment before attr5\n' |
| 124 |
' attr5 = None #: attribute comment for attr5\n' |
| 125 |
' attr6, attr7 = 1, 2 #: this comment is ignored\n' |
| 126 |
'\n' |
| 127 |
' def __init__(self):\n' |
| 128 |
' self.attr8 = None #: first attribute comment (ignored)\n' |
| 129 |
' self.attr8 = None #: attribute comment for attr8\n' |
| 130 |
' #: comment before attr9\n' |
| 131 |
' self.attr9 = None #: comment after attr9\n' |
| 132 |
' "string after attr9"\n' |
| 133 |
'\n' |
| 134 |
' def bar(self, arg1, arg2=True, *args, **kwargs):\n' |
| 135 |
' """method Foo.bar"""\n' |
| 136 |
' pass\n' |
| 137 |
'\n' |
| 138 |
'def baz():\n' |
| 139 |
' """function baz"""\n' |
| 140 |
' pass\n' |
| 141 |
'\n' |
| 142 |
'class Qux: attr1 = 1; attr2 = 2' |
| 143 |
) |
| 144 |
analyzer = ModuleAnalyzer.for_string(code, 'module') |
| 145 |
docs = analyzer.find_attr_docs() |
| 146 |
assert set(docs) == { |
| 147 |
('Foo', 'attr1'), |
| 148 |
('Foo', 'attr3'), |
| 149 |
('Foo', 'attr4'), |
| 150 |
('Foo', 'attr5'), |
| 151 |
('Foo', 'attr6'), |
| 152 |
('Foo', 'attr7'), |
| 153 |
('Foo', 'attr8'), |
| 154 |
('Foo', 'attr9'), |
| 155 |
} |
| 156 |
assert docs['Foo', 'attr1'] == ['comment before attr1', ''] |
| 157 |
assert docs['Foo', 'attr3'] == ['attribute comment for attr3', ''] |
| 158 |
assert docs['Foo', 'attr4'] == ['long attribute comment', ''] |
| 159 |
assert docs['Foo', 'attr4'] == ['long attribute comment', ''] |
| 160 |
assert docs['Foo', 'attr5'] == ['attribute comment for attr5', ''] |
| 161 |
assert docs['Foo', 'attr6'] == ['this comment is ignored', ''] |
| 162 |
assert docs['Foo', 'attr7'] == ['this comment is ignored', ''] |
| 163 |
assert docs['Foo', 'attr8'] == ['attribute comment for attr8', ''] |
| 164 |
assert docs['Foo', 'attr9'] == ['string after attr9', ''] |
| 165 |
assert analyzer.tagorder == { |
| 166 |
'Foo': 0, |
| 167 |
'Foo.__init__': 8, |
| 168 |
'Foo.attr1': 1, |
| 169 |
'Foo.attr2': 2, |
| 170 |
'Foo.attr3': 3, |
| 171 |
'Foo.attr4': 4, |
| 172 |
'Foo.attr5': 5, |
| 173 |
'Foo.attr6': 6, |
| 174 |
'Foo.attr7': 7, |
| 175 |
'Foo.attr8': 10, |
| 176 |
'Foo.attr9': 12, |
| 177 |
'Foo.bar': 13, |
| 178 |
'baz': 14, |
| 179 |
'Qux': 15, |
| 180 |
'Qux.attr1': 16, |
| 181 |
'Qux.attr2': 17, |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
def test_ModuleAnalyzer_find_attr_docs_for_posonlyargs_method() -> None: |
| 186 |
code = ( |
| 187 |
'class Foo(object):\n' |
| 188 |
' def __init__(self, /):\n' |
| 189 |
' self.attr = None #: attribute comment\n' |
| 190 |
) |
| 191 |
analyzer = ModuleAnalyzer.for_string(code, 'module') |
| 192 |
docs = analyzer.find_attr_docs() |
| 193 |
assert set(docs) == {('Foo', 'attr')} |
| 194 |
assert docs['Foo', 'attr'] == ['attribute comment', ''] |
| 195 |
assert analyzer.tagorder == {'Foo': 0, 'Foo.__init__': 1, 'Foo.attr': 2} |