|
| 1 | +"""Tests for llms.txt generation (sphinx-llm) in yardang.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +class TestLlmsConfiguration: |
| 8 | + """Tests for llms.txt configuration loading and generation.""" |
| 9 | + |
| 10 | + def test_llms_config_loading_from_pyproject(self, tmp_path): |
| 11 | + """Test that llms configuration is loaded from pyproject.toml.""" |
| 12 | + pyproject_content = """ |
| 13 | +[project] |
| 14 | +name = "test-project" |
| 15 | +version = "1.0.0" |
| 16 | +
|
| 17 | +[tool.yardang] |
| 18 | +title = "Test Project" |
| 19 | +root = "README.md" |
| 20 | +use-autoapi = false |
| 21 | +
|
| 22 | +[tool.yardang.llms] |
| 23 | +enabled = true |
| 24 | +description = "A project for LLMs" |
| 25 | +build-parallel = false |
| 26 | +suffix-mode = "replace" |
| 27 | +full-build = false |
| 28 | +""" |
| 29 | + pyproject_path = tmp_path / "pyproject.toml" |
| 30 | + pyproject_path.write_text(pyproject_content) |
| 31 | + |
| 32 | + readme_path = tmp_path / "README.md" |
| 33 | + readme_path.write_text("# Test Project\n\nTest content.") |
| 34 | + |
| 35 | + original_cwd = os.getcwd() |
| 36 | + try: |
| 37 | + os.chdir(tmp_path) |
| 38 | + |
| 39 | + from yardang.utils import get_config |
| 40 | + |
| 41 | + assert get_config(section="enabled", base="tool.yardang.llms") is True |
| 42 | + assert get_config(section="description", base="tool.yardang.llms") == "A project for LLMs" |
| 43 | + assert get_config(section="build-parallel", base="tool.yardang.llms") is False |
| 44 | + assert get_config(section="suffix-mode", base="tool.yardang.llms") == "replace" |
| 45 | + assert get_config(section="full-build", base="tool.yardang.llms") is False |
| 46 | + finally: |
| 47 | + os.chdir(original_cwd) |
| 48 | + |
| 49 | + def test_llms_config_defaults(self, tmp_path): |
| 50 | + """Test that llms configuration returns None when not specified.""" |
| 51 | + pyproject_content = """ |
| 52 | +[project] |
| 53 | +name = "test-project" |
| 54 | +version = "1.0.0" |
| 55 | +
|
| 56 | +[tool.yardang] |
| 57 | +title = "Test Project" |
| 58 | +root = "README.md" |
| 59 | +""" |
| 60 | + pyproject_path = tmp_path / "pyproject.toml" |
| 61 | + pyproject_path.write_text(pyproject_content) |
| 62 | + |
| 63 | + readme_path = tmp_path / "README.md" |
| 64 | + readme_path.write_text("# Test Project\n\nTest content.") |
| 65 | + |
| 66 | + original_cwd = os.getcwd() |
| 67 | + try: |
| 68 | + os.chdir(tmp_path) |
| 69 | + |
| 70 | + from yardang.utils import get_config |
| 71 | + |
| 72 | + assert get_config(section="enabled", base="tool.yardang.llms") is None |
| 73 | + assert get_config(section="suffix-mode", base="tool.yardang.llms") is None |
| 74 | + finally: |
| 75 | + os.chdir(original_cwd) |
| 76 | + |
| 77 | + def test_generate_docs_with_llms_enabled(self, tmp_path): |
| 78 | + """Test that generate_docs_configuration wires in the sphinx-llm extension.""" |
| 79 | + pyproject_content = """ |
| 80 | +[project] |
| 81 | +name = "test-project" |
| 82 | +version = "1.0.0" |
| 83 | +
|
| 84 | +[tool.yardang] |
| 85 | +title = "Test Project" |
| 86 | +root = "README.md" |
| 87 | +use-autoapi = false |
| 88 | +
|
| 89 | +[tool.yardang.llms] |
| 90 | +enabled = true |
| 91 | +description = "A project for LLMs" |
| 92 | +suffix-mode = "replace" |
| 93 | +""" |
| 94 | + pyproject_path = tmp_path / "pyproject.toml" |
| 95 | + pyproject_path.write_text(pyproject_content) |
| 96 | + |
| 97 | + readme_path = tmp_path / "README.md" |
| 98 | + readme_path.write_text("# Test Project\n\nTest content.") |
| 99 | + |
| 100 | + original_cwd = os.getcwd() |
| 101 | + try: |
| 102 | + os.chdir(tmp_path) |
| 103 | + |
| 104 | + from yardang.build import generate_docs_configuration |
| 105 | + |
| 106 | + with generate_docs_configuration() as conf_dir: |
| 107 | + conf_content = (Path(conf_dir) / "conf.py").read_text() |
| 108 | + |
| 109 | + assert "use_llms = True" in conf_content |
| 110 | + assert 'extensions.append("sphinx_llm.txt")' in conf_content |
| 111 | + assert "llms_txt_suffix_mode" in conf_content |
| 112 | + assert "replace" in conf_content |
| 113 | + assert "A project for LLMs" in conf_content |
| 114 | + finally: |
| 115 | + os.chdir(original_cwd) |
| 116 | + |
| 117 | + def test_generate_docs_llms_disabled_by_default(self, tmp_path): |
| 118 | + """Test that llms.txt generation is off unless explicitly enabled.""" |
| 119 | + pyproject_content = """ |
| 120 | +[project] |
| 121 | +name = "test-project" |
| 122 | +version = "1.0.0" |
| 123 | +
|
| 124 | +[tool.yardang] |
| 125 | +title = "Test Project" |
| 126 | +root = "README.md" |
| 127 | +use-autoapi = false |
| 128 | +""" |
| 129 | + pyproject_path = tmp_path / "pyproject.toml" |
| 130 | + pyproject_path.write_text(pyproject_content) |
| 131 | + |
| 132 | + readme_path = tmp_path / "README.md" |
| 133 | + readme_path.write_text("# Test Project\n\nTest content.") |
| 134 | + |
| 135 | + original_cwd = os.getcwd() |
| 136 | + try: |
| 137 | + os.chdir(tmp_path) |
| 138 | + |
| 139 | + from yardang.build import generate_docs_configuration |
| 140 | + |
| 141 | + with generate_docs_configuration() as conf_dir: |
| 142 | + conf_content = (Path(conf_dir) / "conf.py").read_text() |
| 143 | + |
| 144 | + assert "use_llms = False" in conf_content |
| 145 | + finally: |
| 146 | + os.chdir(original_cwd) |
0 commit comments