-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_source_discover.py
More file actions
176 lines (163 loc) · 4.81 KB
/
Copy pathtest_source_discover.py
File metadata and controls
176 lines (163 loc) · 4.81 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
# @Test suite for source file discovery with gitignore support, TEST_DISC_1, test, [IMPL_DISC_1]
from pathlib import Path
import pytest
from sphinx_codelinks.source_discover.config import (
COMMENT_FILETYPE,
SourceDiscoverConfig,
SourceDiscoverConfigType,
)
from sphinx_codelinks.source_discover.source_discover import SourceDiscover
@pytest.mark.parametrize(
("config", "msgs"),
[
(
{
"src_dir": 123,
"exclude": ["exclude1", "exclude2"],
"include": ["include1", "include2"],
"gitignore": True,
"comment_type": "cpp",
},
["Schema validation error in field 'src_dir': 123 is not of type 'string'"],
),
(
{
"src_dir": "/path/to/root",
"exclude": ["exclude1", "exclude2"],
"include": ["include1", "include2"],
"gitignore": "TrueAsString",
"comment_type": "cpp",
},
[
"Schema validation error in field 'gitignore': 'TrueAsString' is not of type 'boolean'"
],
),
(
{
"src_dir": "/path/to/root",
"exclude": ["exclude1", "exclude2"],
"include": ["include1", "include2"],
"gitignore": True,
"comment_type": "java",
},
[
"Schema validation error in field 'comment_type': 'java' is not one of ['cpp', 'cs', 'python', 'rust', 'yaml']"
],
),
(
{
"src_dir": "/path/to/root",
"exclude": ["exclude1", "exclude2"],
"include": ["include1", "include2"],
"gitignore": True,
"comment_type": ["cpp", "hpp"],
},
[
"Schema validation error in field 'comment_type': ['cpp', 'hpp'] is not of type 'string'"
],
),
],
)
def test_schema_negative(config, msgs):
source_discover_config = SourceDiscoverConfig(**config)
errors = source_discover_config.check_schema()
assert sorted(errors) == sorted(msgs)
@pytest.mark.parametrize(
"config",
[
{},
{
"src_dir": "/path/to/root",
"exclude": ["exclude1", "exclude2"],
"include": ["include1", "include2"],
"gitignore": True,
"comment_type": "cpp",
},
{
"src_dir": "/path/to/root",
"exclude": ["exclude1", "exclude2"],
"include": ["include1", "include2"],
"gitignore": True,
"comment_type": "python",
},
],
)
def test_schema_positive(config):
source_discover_config = SourceDiscoverConfig(**config)
errors = source_discover_config.check_schema()
assert len(errors) == 0
@pytest.mark.parametrize(
("config", "num_files", "suffix"),
[
(
{
"gitignore": False,
},
4,
"",
),
(
{
"gitignore": True,
},
3,
"",
),
(
{
"gitignore": True,
"exclude": ["charge/*.cpp"],
"include": ["**/*.cpp"],
},
4,
"",
),
(
{
"gitignore": True,
"exclude": ["charge/*.cpp"],
},
2,
"",
),
(
{"gitignore": False, "comment_type": "cpp"},
4,
"cpp",
),
],
)
def test_source_discover(
config: SourceDiscoverConfigType,
num_files: int,
suffix: str,
source_directory: Path,
) -> None:
config["src_dir"] = source_directory
src_discover_config = SourceDiscoverConfig(**config)
source_discover = SourceDiscover(src_discover_config)
assert len(source_discover.source_paths) == num_files
if suffix:
assert all(path.suffix == ".cpp" for path in source_discover.source_paths)
@pytest.fixture(scope="function")
def create_source_files(tmp_path: Path) -> Path:
for file_types in COMMENT_FILETYPE.values():
for ext in file_types:
(tmp_path / f"file.{ext}").touch()
return tmp_path
@pytest.mark.parametrize(
("comment_type", "nums_files"),
[
("cpp", len(COMMENT_FILETYPE["cpp"])),
("python", len(COMMENT_FILETYPE["python"])),
],
)
def test_comment_filetype(
comment_type: str, nums_files: int, create_source_files: Path
) -> None:
src_dir = create_source_files
config = SourceDiscoverConfig(
src_dir=src_dir, comment_type=comment_type, gitignore=False
)
source_discover = SourceDiscover(config)
assert len(source_discover.source_paths) == nums_files