[docs]  1# @Test suite for source file discovery with gitignore support, TEST_DISC_1, test, [IMPL_DISC_1]
  2from pathlib import Path
  3
  4import pytest
  5
  6from sphinx_codelinks.source_discover.config import (
  7    COMMENT_FILETYPE,
  8    SourceDiscoverConfig,
  9    SourceDiscoverConfigType,
 10)
 11from sphinx_codelinks.source_discover.source_discover import SourceDiscover
 12
 13
 14@pytest.mark.parametrize(
 15    ("config", "msgs"),
 16    [
 17        (
 18            {
 19                "src_dir": 123,
 20                "exclude": ["exclude1", "exclude2"],
 21                "include": ["include1", "include2"],
 22                "gitignore": True,
 23                "comment_type": "cpp",
 24            },
 25            ["Schema validation error in field 'src_dir': 123 is not of type 'string'"],
 26        ),
 27        (
 28            {
 29                "src_dir": "/path/to/root",
 30                "exclude": ["exclude1", "exclude2"],
 31                "include": ["include1", "include2"],
 32                "gitignore": "TrueAsString",
 33                "comment_type": "cpp",
 34            },
 35            [
 36                "Schema validation error in field 'gitignore': 'TrueAsString' is not of type 'boolean'"
 37            ],
 38        ),
 39        (
 40            {
 41                "src_dir": "/path/to/root",
 42                "exclude": ["exclude1", "exclude2"],
 43                "include": ["include1", "include2"],
 44                "gitignore": True,
 45                "comment_type": "java",
 46            },
 47            [
 48                "Schema validation error in field 'comment_type': 'java' is not one of ['cpp', 'cs', 'python', 'rust', 'yaml']"
 49            ],
 50        ),
 51        (
 52            {
 53                "src_dir": "/path/to/root",
 54                "exclude": ["exclude1", "exclude2"],
 55                "include": ["include1", "include2"],
 56                "gitignore": True,
 57                "comment_type": ["cpp", "hpp"],
 58            },
 59            [
 60                "Schema validation error in field 'comment_type': ['cpp', 'hpp'] is not of type 'string'"
 61            ],
 62        ),
 63    ],
 64)
 65def test_schema_negative(config, msgs):
 66    source_discover_config = SourceDiscoverConfig(**config)
 67    errors = source_discover_config.check_schema()
 68    assert sorted(errors) == sorted(msgs)
 69
 70
 71@pytest.mark.parametrize(
 72    "config",
 73    [
 74        {},
 75        {
 76            "src_dir": "/path/to/root",
 77            "exclude": ["exclude1", "exclude2"],
 78            "include": ["include1", "include2"],
 79            "gitignore": True,
 80            "comment_type": "cpp",
 81        },
 82        {
 83            "src_dir": "/path/to/root",
 84            "exclude": ["exclude1", "exclude2"],
 85            "include": ["include1", "include2"],
 86            "gitignore": True,
 87            "comment_type": "python",
 88        },
 89    ],
 90)
 91def test_schema_positive(config):
 92    source_discover_config = SourceDiscoverConfig(**config)
 93    errors = source_discover_config.check_schema()
 94    assert len(errors) == 0
 95
 96
 97@pytest.mark.parametrize(
 98    ("config", "num_files", "suffix"),
 99    [
100        (
101            {
102                "gitignore": False,
103            },
104            4,
105            "",
106        ),
107        (
108            {
109                "gitignore": True,
110            },
111            3,
112            "",
113        ),
114        (
115            {
116                "gitignore": True,
117                "exclude": ["charge/*.cpp"],
118                "include": ["**/*.cpp"],
119            },
120            4,
121            "",
122        ),
123        (
124            {
125                "gitignore": True,
126                "exclude": ["charge/*.cpp"],
127            },
128            2,
129            "",
130        ),
131        (
132            {"gitignore": False, "comment_type": "cpp"},
133            4,
134            "cpp",
135        ),
136    ],
137)
138def test_source_discover(
139    config: SourceDiscoverConfigType,
140    num_files: int,
141    suffix: str,
142    source_directory: Path,
143) -> None:
144    config["src_dir"] = source_directory
145    src_discover_config = SourceDiscoverConfig(**config)
146    source_discover = SourceDiscover(src_discover_config)
147    assert len(source_discover.source_paths) == num_files
148    if suffix:
149        assert all(path.suffix == ".cpp" for path in source_discover.source_paths)
150
151
152@pytest.fixture(scope="function")
153def create_source_files(tmp_path: Path) -> Path:
154    for file_types in COMMENT_FILETYPE.values():
155        for ext in file_types:
156            (tmp_path / f"file.{ext}").touch()
157    return tmp_path
158
159
160@pytest.mark.parametrize(
161    ("comment_type", "nums_files"),
162    [
163        ("cpp", len(COMMENT_FILETYPE["cpp"])),
164        ("python", len(COMMENT_FILETYPE["python"])),
165    ],
166)
167def test_comment_filetype(
168    comment_type: str, nums_files: int, create_source_files: Path
169) -> None:
170    src_dir = create_source_files
171
172    config = SourceDiscoverConfig(
173        src_dir=src_dir, comment_type=comment_type, gitignore=False
174    )
175    source_discover = SourceDiscover(config)
176    assert len(source_discover.source_paths) == nums_files