-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_src_trace.py
More file actions
229 lines (216 loc) · 8.46 KB
/
test_src_trace.py
File metadata and controls
229 lines (216 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from collections.abc import Callable
from pathlib import Path
import shutil
import pytest
from sphinx.testing.util import SphinxTestApp
from sphinx_codelinks.analyse.projects import AnalyseProjects
from sphinx_codelinks.config import (
SRC_TRACE_CACHE,
CodeLinksConfig,
check_configuration,
)
from sphinx_codelinks.sphinx_extension.source_tracing import set_config_to_sphinx
@pytest.mark.parametrize(
("codelinks_config", "result"),
[
(
{
"remote_url_field": 555,
"local_url_field": 789,
"set_local_url": "fdd",
"set_remote_url": "TrueString",
"projects": {
"dcdc": {
"remote_url_pattern": 44332,
"source_discover": {
"comment_type": "java",
"src_dir": ["../dcdc"],
"exclude": [123],
"include": [345],
"gitignore": "_true",
},
"analyse": {
"oneline_comment_style": {
"start_sequence": "[[",
"end_sequence": "]]",
"field_split_char": ",",
"needs_fields": [
{
"name": "title",
"type": "list[]",
},
{
"name": "type",
"default": "impl",
"type": "str",
},
],
},
},
}
},
},
[
"Project 'dcdc' has the following errors:",
"Schema validation error in field 'exclude': 123 is not of type 'string'",
"Schema validation error in field 'comment_type': 'java' is not one of ['cpp', 'cs', 'python', 'yaml']",
"Schema validation error in field 'gitignore': '_true' is not of type 'boolean'",
"Schema validation error in field 'include': 345 is not of type 'string'",
"Schema validation error in field 'src_dir': ['../dcdc'] is not of type 'string'",
"Schema validation error in filed 'local_url_field': 789 is not of type 'string'",
"Schema validation error in filed 'remote_url_field': 555 is not of type 'string'",
"Schema validation error in filed 'set_local_url': 'fdd' is not of type 'boolean'",
"Schema validation error in filed 'set_remote_url': 'TrueString' is not of type 'boolean'",
"OneLineCommentStyle configuration errors:",
"Schema validation error in need_fields 'title': 'list[]' is not one of ['str', 'list[str]']",
"remote_url_pattern must be a string",
],
),
(
{
"remote_url_field": "remote-url",
"local_url_field": "local-url",
"set_local_url": True,
"set_remote_url": True,
"projects": {
"dcdc": {
# intentionally not given "remote_url_pattern": "https://github.com/useblocks/sphinx-codelinks/blob/{commit}/{path}#L{line}",
"source_discover": {
"comment_type": "cpp",
"src_dir": "../dcdc",
"exclude": [],
"include": [],
"gitignore": True,
},
"analyse": {
"oneline_comment_style": {
"start_sequence": "[[",
"end_sequence": "]]",
"field_split_char": ",",
"needs_fields": [
{
"name": "title",
"type": "str",
},
{
"name": "type",
"default": "impl",
"type": "str",
},
],
},
},
}
},
},
[
"Project 'dcdc' has the following errors:",
"remote_url_pattern must be given, as set_remote_url is enabled",
],
),
],
)
def test_src_tracing_config_negative(
make_app: Callable[..., SphinxTestApp],
codelinks_config,
result,
):
this_file_dir = Path(__file__).parent
sphinx_project = Path("data") / "sphinx"
app = make_app(srcdir=(this_file_dir / sphinx_project))
set_config_to_sphinx(codelinks_config, app.env.config)
codelinks_sphinx_config = CodeLinksConfig.from_sphinx(app.env.config)
errors = check_configuration(codelinks_sphinx_config)
assert sorted(errors) == sorted(result)
def test_src_tracing_config_positive(make_app: Callable[..., SphinxTestApp], tmp_path):
codelinks_config = {
"remote_url_field": "remote-url",
"local_url_field": "local-url",
"set_local_url": True,
"set_remote_url": True,
"outdir": tmp_path,
"projects": {
"dcdc": {
"source_discover": {
"comment_type": "cpp",
"src_dir": "../dcdc",
"exclude": ["**/*.hpp"],
"include": ["**/*.cpp"],
"gitignore": True,
},
"remote_url_pattern": "https://github.com/useblocks/sphinx-codelinks/blob/{commit}/{path}#L{line}",
"analyse": {
"oneline_comment_style": {
"start_sequence": "[[",
"end_sequence": "]]",
"field_split_char": ",",
"needs_fields": [
{
"name": "title",
"type": "str",
},
{
"name": "type",
"default": "impl",
"type": "str",
},
],
},
},
}
},
}
this_file_dir = Path(__file__).parent
sphinx_project = Path("data") / "sphinx"
app = make_app(srcdir=(this_file_dir / sphinx_project))
set_config_to_sphinx(codelinks_config, app.env.config)
codelinks_sphinx_config = CodeLinksConfig.from_sphinx(app.env.config)
errors = check_configuration(codelinks_sphinx_config)
assert not errors
@pytest.mark.parametrize(
("sphinx_project", "source_code"),
[
(Path("data") / "sphinx", Path("data") / "dcdc"),
(
Path("doc_test") / "recursive_dirs",
Path("doc_test") / "recursive_dirs" / "dummy_src_lv1",
),
(
Path("doc_test") / "minimum_config",
Path("doc_test") / "minimum_config",
),
(
Path("doc_test") / "id_required",
Path("doc_test") / "id_required",
),
],
)
def test_build_html(
tmpdir: Path,
make_app: Callable[..., SphinxTestApp],
sphinx_project,
source_code,
snapshot_doctree,
):
this_file_dir = Path(__file__).parent
sphinx_src_dir = tmpdir / sphinx_project
shutil.copytree(
this_file_dir / sphinx_project,
sphinx_src_dir,
dirs_exist_ok=True,
)
shutil.copytree(
this_file_dir / source_code,
tmpdir / source_code,
dirs_exist_ok=True,
)
app: SphinxTestApp = make_app(
srcdir=Path(sphinx_src_dir),
freshenv=True,
)
app.build()
html = Path(app.outdir, "index.html").read_text()
assert html
warnings = AnalyseProjects.load_warnings(Path(app.outdir) / SRC_TRACE_CACHE)
assert not warnings
assert app.env.get_doctree("index") == snapshot_doctree