|
3 | 3 |
|
4 | 4 | from yardang.build import generate_docs_configuration |
5 | 5 | from yardang.cli import build, debug |
| 6 | +from yardang.utils import get_config_flex |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def test_build(): |
@@ -82,3 +83,188 @@ def test_use_autoapi_none_falls_back_to_config(self, tmp_path): |
82 | 83 | assert "use_autoapi = True" in conf_content |
83 | 84 | finally: |
84 | 85 | os.chdir(original_cwd) |
| 86 | + |
| 87 | + |
| 88 | +class TestGetConfigFlex: |
| 89 | + """Tests for get_config_flex accepting both hyphens and underscores.""" |
| 90 | + |
| 91 | + def test_hyphen_key_found(self, tmp_path): |
| 92 | + """Test that hyphenated keys are found.""" |
| 93 | + pyproject_content = """ |
| 94 | +[project] |
| 95 | +name = "test-project" |
| 96 | +version = "1.0.0" |
| 97 | +
|
| 98 | +[tool.yardang] |
| 99 | +html-extra-path = ["docs/extra"] |
| 100 | +""" |
| 101 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 102 | + original_cwd = os.getcwd() |
| 103 | + try: |
| 104 | + os.chdir(tmp_path) |
| 105 | + result = get_config_flex(section="html-extra-path", base="tool.yardang") |
| 106 | + assert result == ["docs/extra"] |
| 107 | + finally: |
| 108 | + os.chdir(original_cwd) |
| 109 | + |
| 110 | + def test_underscore_key_found(self, tmp_path): |
| 111 | + """Test that underscored keys are found.""" |
| 112 | + pyproject_content = """ |
| 113 | +[project] |
| 114 | +name = "test-project" |
| 115 | +version = "1.0.0" |
| 116 | +
|
| 117 | +[tool.yardang] |
| 118 | +html_extra_path = ["docs/extra"] |
| 119 | +""" |
| 120 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 121 | + original_cwd = os.getcwd() |
| 122 | + try: |
| 123 | + os.chdir(tmp_path) |
| 124 | + result = get_config_flex(section="html_extra_path", base="tool.yardang") |
| 125 | + assert result == ["docs/extra"] |
| 126 | + finally: |
| 127 | + os.chdir(original_cwd) |
| 128 | + |
| 129 | + def test_hyphen_key_searched_when_underscore_queried(self, tmp_path): |
| 130 | + """Test that querying with underscores finds hyphenated keys.""" |
| 131 | + pyproject_content = """ |
| 132 | +[project] |
| 133 | +name = "test-project" |
| 134 | +version = "1.0.0" |
| 135 | +
|
| 136 | +[tool.yardang] |
| 137 | +html-extra-path = ["docs/extra"] |
| 138 | +""" |
| 139 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 140 | + original_cwd = os.getcwd() |
| 141 | + try: |
| 142 | + os.chdir(tmp_path) |
| 143 | + # Query with underscores, but TOML uses hyphens |
| 144 | + result = get_config_flex(section="html_extra_path", base="tool.yardang") |
| 145 | + assert result == ["docs/extra"] |
| 146 | + finally: |
| 147 | + os.chdir(original_cwd) |
| 148 | + |
| 149 | + def test_underscore_key_searched_when_hyphen_queried(self, tmp_path): |
| 150 | + """Test that querying with hyphens finds underscored keys.""" |
| 151 | + pyproject_content = """ |
| 152 | +[project] |
| 153 | +name = "test-project" |
| 154 | +version = "1.0.0" |
| 155 | +
|
| 156 | +[tool.yardang] |
| 157 | +html_extra_path = ["docs/extra"] |
| 158 | +""" |
| 159 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 160 | + original_cwd = os.getcwd() |
| 161 | + try: |
| 162 | + os.chdir(tmp_path) |
| 163 | + # Query with hyphens, but TOML uses underscores |
| 164 | + result = get_config_flex(section="html-extra-path", base="tool.yardang") |
| 165 | + assert result == ["docs/extra"] |
| 166 | + finally: |
| 167 | + os.chdir(original_cwd) |
| 168 | + |
| 169 | + def test_hyphen_takes_precedence(self, tmp_path): |
| 170 | + """Test that hyphenated key takes precedence when both exist.""" |
| 171 | + pyproject_content = """ |
| 172 | +[project] |
| 173 | +name = "test-project" |
| 174 | +version = "1.0.0" |
| 175 | +
|
| 176 | +[tool.yardang] |
| 177 | +html-extra-path = ["from-hyphens"] |
| 178 | +html_extra_path = ["from-underscores"] |
| 179 | +""" |
| 180 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 181 | + original_cwd = os.getcwd() |
| 182 | + try: |
| 183 | + os.chdir(tmp_path) |
| 184 | + result = get_config_flex(section="html_extra_path", base="tool.yardang") |
| 185 | + assert result == ["from-hyphens"] |
| 186 | + finally: |
| 187 | + os.chdir(original_cwd) |
| 188 | + |
| 189 | + def test_missing_key_returns_none(self, tmp_path): |
| 190 | + """Test that missing keys return None.""" |
| 191 | + pyproject_content = """ |
| 192 | +[project] |
| 193 | +name = "test-project" |
| 194 | +version = "1.0.0" |
| 195 | +
|
| 196 | +[tool.yardang] |
| 197 | +title = "Test" |
| 198 | +""" |
| 199 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 200 | + original_cwd = os.getcwd() |
| 201 | + try: |
| 202 | + os.chdir(tmp_path) |
| 203 | + result = get_config_flex(section="html_extra_path", base="tool.yardang") |
| 204 | + assert result is None |
| 205 | + finally: |
| 206 | + os.chdir(original_cwd) |
| 207 | + |
| 208 | + |
| 209 | +class TestHtmlExtraPath: |
| 210 | + """Tests for html_extra_path in generated conf.py.""" |
| 211 | + |
| 212 | + def test_html_extra_path_with_hyphens(self, tmp_path): |
| 213 | + """Test that html-extra-path (hyphens) is picked up in generated conf.py.""" |
| 214 | + extra_dir = tmp_path / "docs" / "extra" |
| 215 | + extra_dir.mkdir(parents=True) |
| 216 | + (extra_dir / "page.html").write_text("<html><body>test</body></html>") |
| 217 | + |
| 218 | + pyproject_content = """ |
| 219 | +[project] |
| 220 | +name = "test-project" |
| 221 | +version = "1.0.0" |
| 222 | +
|
| 223 | +[tool.yardang] |
| 224 | +title = "Test Project" |
| 225 | +root = "README.md" |
| 226 | +use-autoapi = false |
| 227 | +html-extra-path = ["docs/extra"] |
| 228 | +""" |
| 229 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 230 | + (tmp_path / "README.md").write_text("# Test\n\nContent.") |
| 231 | + |
| 232 | + original_cwd = os.getcwd() |
| 233 | + try: |
| 234 | + os.chdir(tmp_path) |
| 235 | + with generate_docs_configuration() as conf_dir: |
| 236 | + conf_content = (Path(conf_dir) / "conf.py").read_text() |
| 237 | + assert "html_extra_path" in conf_content |
| 238 | + assert "docs/extra" in conf_content or "docs\\\\extra" in conf_content or str(extra_dir) in conf_content |
| 239 | + finally: |
| 240 | + os.chdir(original_cwd) |
| 241 | + |
| 242 | + def test_html_extra_path_with_underscores(self, tmp_path): |
| 243 | + """Test that html_extra_path (underscores) is also accepted.""" |
| 244 | + extra_dir = tmp_path / "docs" / "extra" |
| 245 | + extra_dir.mkdir(parents=True) |
| 246 | + (extra_dir / "page.html").write_text("<html><body>test</body></html>") |
| 247 | + |
| 248 | + pyproject_content = """ |
| 249 | +[project] |
| 250 | +name = "test-project" |
| 251 | +version = "1.0.0" |
| 252 | +
|
| 253 | +[tool.yardang] |
| 254 | +title = "Test Project" |
| 255 | +root = "README.md" |
| 256 | +use-autoapi = false |
| 257 | +html_extra_path = ["docs/extra"] |
| 258 | +""" |
| 259 | + (tmp_path / "pyproject.toml").write_text(pyproject_content) |
| 260 | + (tmp_path / "README.md").write_text("# Test\n\nContent.") |
| 261 | + |
| 262 | + original_cwd = os.getcwd() |
| 263 | + try: |
| 264 | + os.chdir(tmp_path) |
| 265 | + with generate_docs_configuration() as conf_dir: |
| 266 | + conf_content = (Path(conf_dir) / "conf.py").read_text() |
| 267 | + assert "html_extra_path" in conf_content |
| 268 | + assert "docs/extra" in conf_content or "docs\\\\extra" in conf_content or str(extra_dir) in conf_content |
| 269 | + finally: |
| 270 | + os.chdir(original_cwd) |
0 commit comments