|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from hypothesis import given, settings |
| 5 | +from hypothesis import strategies as st |
| 6 | + |
| 7 | +from render_engine_api.config import ApiConfig |
| 8 | + |
| 9 | + |
| 10 | +def _write_config(tmp_path, content): |
| 11 | + """Helper to write a pyproject.toml in tmp_path.""" |
| 12 | + config_file = tmp_path / "pyproject.toml" |
| 13 | + config_file.write_text(content) |
| 14 | + return config_file |
| 15 | + |
| 16 | + |
| 17 | +# Strategy for valid TOML-safe identifier strings (no quotes, newlines, etc.) |
| 18 | +toml_safe_text = st.text( |
| 19 | + alphabet=st.characters(whitelist_categories=("L", "N"), whitelist_characters="_-"), |
| 20 | + min_size=1, |
| 21 | + max_size=50, |
| 22 | +) |
| 23 | + |
| 24 | +# Strategy that produces an optional config key (present or absent) |
| 25 | +optional_toml_value = st.one_of(st.none(), toml_safe_text) |
| 26 | + |
| 27 | + |
| 28 | +class TestAPIConfigLoadConfig: |
| 29 | + """Tests for APIConfig.load_config parsing pyproject.toml.""" |
| 30 | + |
| 31 | + @given( |
| 32 | + module=optional_toml_value, |
| 33 | + site=optional_toml_value, |
| 34 | + collection=optional_toml_value, |
| 35 | + ) |
| 36 | + @settings(max_examples=50, deadline=None) |
| 37 | + def test_loads_module_site_from_valid_config(self, module, site, collection): |
| 38 | + """Config correctly populates module, site, and collection from any combination of keys.""" |
| 39 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 40 | + tmp_path = Path(tmpdir) |
| 41 | + lines = ["[tool.render-engine.cli]"] |
| 42 | + if module is not None: |
| 43 | + lines.append(f'module = "{module}"') |
| 44 | + if site is not None: |
| 45 | + lines.append(f'site = "{site}"') |
| 46 | + if collection is not None: |
| 47 | + lines.append(f'collection = "{collection}"') |
| 48 | + |
| 49 | + temp_config_file = _write_config(tmp_path, "\n".join(lines) + "\n") |
| 50 | + config = ApiConfig(config_file=temp_config_file) |
| 51 | + config.load_config() |
| 52 | + |
| 53 | + assert config._module == module |
| 54 | + assert config._site == site |
| 55 | + assert config._collection == collection |
| 56 | + |
| 57 | + def test_config_file_not_passed(self): |
| 58 | + """Returns None for all properties when no config_file is passed.""" |
| 59 | + config = ApiConfig(config_file=None) # intentional path that doesn't exist |
| 60 | + assert config.module is None |
| 61 | + assert config.site is None |
| 62 | + assert config.collection is None |
| 63 | + # Editor pulls from editor by default |
| 64 | + |
| 65 | + def test_config_file_not_found(self, tmp_path): |
| 66 | + """Returns None for all properties when the supplied config file does not exist.""" |
| 67 | + config = ApiConfig(config_file=tmp_path / "no-pyproject.toml") # intentional path that doesn't exist |
| 68 | + assert config.module is None |
| 69 | + assert config.site is None |
| 70 | + assert config.collection is None |
| 71 | + # Editor pulls from editor by default |
| 72 | + |
| 73 | + def test_invalid_toml_prints_error_and_returns_none(self, tmp_path): |
| 74 | + """Returns None for all properties when the config file contains invalid TOML.""" |
| 75 | + config_file = _write_config(tmp_path, "not valid toml [[[") |
| 76 | + config = ApiConfig(config_file=config_file) |
| 77 | + assert config.module is None |
| 78 | + assert config.site is None |
| 79 | + assert config.collection is None |
| 80 | + |
| 81 | + def test_config_file_not_ran_if_self_config_loaded_equals_true(self, tmp_path): |
| 82 | + config_file = _write_config( |
| 83 | + tmp_path, |
| 84 | + content=""" |
| 85 | +module="app" |
| 86 | +site="app" |
| 87 | +editor="nvim" |
| 88 | +""", |
| 89 | + ) |
| 90 | + config = ApiConfig(config_file=config_file, _config_loaded=True) |
| 91 | + assert config.module is None |
| 92 | + assert config.site is None |
| 93 | + assert config.collection is None |
| 94 | + |
| 95 | + def test_editor_from_config(self, tmp_path, monkeypatch): |
| 96 | + """Reads the editor value from the config file.""" |
| 97 | + config_file = _write_config(tmp_path, content='[tool.render-engine.cli]\neditor="nvim"\n') |
| 98 | + config = ApiConfig(config_file=config_file) |
| 99 | + assert config.editor == "nvim" |
| 100 | + |
| 101 | + def test_editor_falls_back_to_env(self, tmp_path, monkeypatch): |
| 102 | + """Falls back to the EDITOR environment variable when not in config.""" |
| 103 | + monkeypatch.setenv("EDITOR", "fake-editor") |
| 104 | + config = ApiConfig() |
| 105 | + assert config.editor == "fake-editor" |
| 106 | + |
| 107 | + def test_editor_none_when_not_set(self, monkeypatch): |
| 108 | + """Returns None when editor is not in config or environment.""" |
| 109 | + monkeypatch.delenv("EDITOR") |
| 110 | + config = ApiConfig() |
| 111 | + assert config.editor is None |
| 112 | + |
| 113 | + |
| 114 | +class TestApiConfigLazyLoading: |
| 115 | + """Tests that ApiConfig lazily loads the config on first property access.""" |
| 116 | + |
| 117 | + def test_config_not_loaded_until_property_accessed(self, tmp_path): |
| 118 | + """Config file is not read until a property is first accessed.""" |
| 119 | + config_file = _write_config( |
| 120 | + tmp_path, |
| 121 | + """ |
| 122 | +[tool.render-engine.cli] |
| 123 | +module = "myapp" |
| 124 | +site = "MySite" |
| 125 | +""", |
| 126 | + ) |
| 127 | + config = ApiConfig(config_file=config_file) |
| 128 | + assert config._config_loaded is False |
| 129 | + _ = config.module |
| 130 | + assert config._config_loaded is True |
0 commit comments