|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from click.testing import CliRunner |
| 6 | +from pydantic import Field |
| 7 | + |
| 8 | +from tidy3d.config import get_manager, reload_config |
| 9 | +from tidy3d.config import registry as config_registry |
| 10 | +from tidy3d.config.sections import ConfigSection |
| 11 | +from tidy3d.web.cli.app import tidy3d_cli |
| 12 | + |
| 13 | + |
| 14 | +def _config_path(config_dir: Path) -> Path: |
| 15 | + return config_dir / "config.toml" |
| 16 | + |
| 17 | + |
| 18 | +def test_save_includes_descriptions(config_manager, mock_config_dir): |
| 19 | + manager = config_manager |
| 20 | + manager.save(include_defaults=True) |
| 21 | + |
| 22 | + content = _config_path(mock_config_dir).read_text(encoding="utf-8") |
| 23 | + assert "# Web/HTTP configuration." in content |
| 24 | + |
| 25 | + |
| 26 | +def test_preserves_user_comments(config_manager, mock_config_dir): |
| 27 | + manager = config_manager |
| 28 | + manager.save(include_defaults=True) |
| 29 | + |
| 30 | + config_path = _config_path(mock_config_dir) |
| 31 | + text = config_path.read_text(encoding="utf-8") |
| 32 | + text = text.replace( |
| 33 | + "Web/HTTP configuration.", |
| 34 | + "user-modified comment", |
| 35 | + ) |
| 36 | + config_path.write_text(text, encoding="utf-8") |
| 37 | + |
| 38 | + reload_config(profile="default") |
| 39 | + manager = get_manager() |
| 40 | + manager.save(include_defaults=True) |
| 41 | + |
| 42 | + updated = config_path.read_text(encoding="utf-8") |
| 43 | + assert "user-modified comment" in updated |
| 44 | + assert "Web/HTTP configuration." not in updated |
| 45 | + |
| 46 | + |
| 47 | +def test_profile_preserves_comments(config_manager, mock_config_dir): |
| 48 | + @config_registry.register_plugin("profile_comment") |
| 49 | + class ProfileComment(ConfigSection): |
| 50 | + """Profile comment plugin.""" |
| 51 | + |
| 52 | + knob: int = Field( |
| 53 | + 1, |
| 54 | + description="Profile knob description.", |
| 55 | + json_schema_extra={"persist": True}, |
| 56 | + ) |
| 57 | + |
| 58 | + try: |
| 59 | + manager = config_manager |
| 60 | + manager.switch_profile("custom") |
| 61 | + manager.update_section("plugins.profile_comment", knob=5) |
| 62 | + manager.save() |
| 63 | + |
| 64 | + profile_path = mock_config_dir / "profiles" / "custom.toml" |
| 65 | + text = profile_path.read_text(encoding="utf-8") |
| 66 | + assert "Profile knob description." in text |
| 67 | + text = text.replace("Profile knob description.", "user comment") |
| 68 | + profile_path.write_text(text, encoding="utf-8") |
| 69 | + |
| 70 | + manager.update_section("plugins.profile_comment", knob=7) |
| 71 | + manager.save() |
| 72 | + |
| 73 | + updated = profile_path.read_text(encoding="utf-8") |
| 74 | + assert "user comment" in updated |
| 75 | + assert "Profile knob description." not in updated |
| 76 | + finally: |
| 77 | + config_registry._SECTIONS.pop("plugins.profile_comment", None) |
| 78 | + reload_config(profile="default") |
| 79 | + |
| 80 | + |
| 81 | +def test_cli_reset_config(mock_config_dir): |
| 82 | + @config_registry.register_plugin("cli_comment") |
| 83 | + class CLIPlugin(ConfigSection): |
| 84 | + """CLI plugin configuration.""" |
| 85 | + |
| 86 | + knob: int = Field( |
| 87 | + 3, |
| 88 | + description="CLI knob description.", |
| 89 | + json_schema_extra={"persist": True}, |
| 90 | + ) |
| 91 | + |
| 92 | + try: |
| 93 | + reload_config(profile="default") |
| 94 | + manager = get_manager() |
| 95 | + manager.update_section("web", apikey="secret") |
| 96 | + manager.save(include_defaults=True) |
| 97 | + manager.switch_profile("custom") |
| 98 | + manager.update_section("plugins.cli_comment", knob=42) |
| 99 | + manager.save() |
| 100 | + |
| 101 | + profiles_dir = mock_config_dir / "profiles" |
| 102 | + assert profiles_dir.exists() |
| 103 | + |
| 104 | + runner = CliRunner() |
| 105 | + result = runner.invoke(tidy3d_cli, ["config", "reset", "--yes"]) |
| 106 | + assert result.exit_code == 0, result.output |
| 107 | + |
| 108 | + config_text = _config_path(mock_config_dir).read_text(encoding="utf-8") |
| 109 | + assert "Web/HTTP configuration." in config_text |
| 110 | + assert "[web]" in config_text |
| 111 | + assert "secret" not in config_text |
| 112 | + assert not profiles_dir.exists() |
| 113 | + finally: |
| 114 | + config_registry._SECTIONS.pop("plugins.cli_comment", None) |
| 115 | + reload_config(profile="default") |
| 116 | + |
| 117 | + |
| 118 | +def test_plugin_descriptions(mock_config_dir): |
| 119 | + @config_registry.register_plugin("comment_test") |
| 120 | + class CommentPlugin(ConfigSection): |
| 121 | + """Comment plugin configuration.""" |
| 122 | + |
| 123 | + knob: int = Field( |
| 124 | + 3, |
| 125 | + description="Plugin knob description.", |
| 126 | + json_schema_extra={"persist": True}, |
| 127 | + ) |
| 128 | + |
| 129 | + try: |
| 130 | + reload_config(profile="default") |
| 131 | + manager = get_manager() |
| 132 | + manager.save(include_defaults=True) |
| 133 | + content = _config_path(mock_config_dir).read_text(encoding="utf-8") |
| 134 | + assert "Comment plugin configuration." in content |
| 135 | + assert "Plugin knob description." in content |
| 136 | + finally: |
| 137 | + config_registry._SECTIONS.pop("plugins.comment_test", None) |
| 138 | + reload_config(profile="default") |
0 commit comments