66from pydantic import Field
77
88from tidy3d .config import get_manager , reload_config
9+ from tidy3d .config import loader as config_loader
910from tidy3d .config import registry as config_registry
11+ from tidy3d .config .legacy import finalize_legacy_migration
12+ from tidy3d .config .loader import migrate_legacy_config
1013from tidy3d .config .sections import ConfigSection
1114from tidy3d .web .cli .app import tidy3d_cli
1215
@@ -32,7 +35,7 @@ def test_save_includes_descriptions(config_manager, mock_config_dir):
3235 manager .save (include_defaults = True )
3336
3437 content = _config_path (mock_config_dir ).read_text (encoding = "utf-8" )
35- assert "# Web/HTTP configuration ." in content
38+ assert "Lowest logging level that will be emitted ." in content
3639
3740
3841def test_preserves_user_comments (config_manager , mock_config_dir ):
@@ -41,10 +44,7 @@ def test_preserves_user_comments(config_manager, mock_config_dir):
4144
4245 config_path = _config_path (mock_config_dir )
4346 text = config_path .read_text (encoding = "utf-8" )
44- text = text .replace (
45- "Web/HTTP configuration." ,
46- "user-modified comment" ,
47- )
47+ text = text .replace ("Lowest logging level that will be emitted." , "user-modified comment" )
4848 config_path .write_text (text , encoding = "utf-8" )
4949
5050 reload_config (profile = "default" )
@@ -53,7 +53,7 @@ def test_preserves_user_comments(config_manager, mock_config_dir):
5353
5454 updated = config_path .read_text (encoding = "utf-8" )
5555 assert "user-modified comment" in updated
56- assert "Web/HTTP configuration ." not in updated
56+ assert "Lowest logging level that will be emitted ." not in updated
5757
5858
5959def test_profile_preserves_comments (config_manager , mock_config_dir ):
@@ -118,7 +118,7 @@ class CLIPlugin(ConfigSection):
118118 assert result .exit_code == 0 , result .output
119119
120120 config_text = _config_path (mock_config_dir ).read_text (encoding = "utf-8" )
121- assert "Web/HTTP configuration ." in config_text
121+ assert "Lowest logging level that will be emitted ." in config_text
122122 assert "[web]" in config_text
123123 assert "secret" not in config_text
124124 assert not profiles_dir .exists ()
@@ -143,8 +143,58 @@ class CommentPlugin(ConfigSection):
143143 manager = get_manager ()
144144 manager .save (include_defaults = True )
145145 content = _config_path (mock_config_dir ).read_text (encoding = "utf-8" )
146- assert "Comment plugin configuration." in content
147146 assert "Plugin knob description." in content
148147 finally :
149148 config_registry ._SECTIONS .pop ("plugins.comment_test" , None )
150149 reload_config (profile = "default" )
150+
151+
152+ def test_finalize_legacy_migration_promotes_flat_file (tmp_path ):
153+ canonical_dir = tmp_path / "canonical"
154+ canonical_dir .mkdir ()
155+ legacy_file = canonical_dir / "config"
156+ legacy_file .write_text ('apikey = "legacy-key"\n ' , encoding = "utf-8" )
157+ extra_file = canonical_dir / "extra.txt"
158+ extra_file .write_text ("keep" , encoding = "utf-8" )
159+
160+ finalize_legacy_migration (canonical_dir )
161+
162+ config_toml = canonical_dir / "config.toml"
163+ assert config_toml .exists ()
164+ content = config_toml .read_text (encoding = "utf-8" )
165+ assert "[web]" in content
166+ assert "[logging]" in content
167+ assert "Lowest logging level that will be emitted." in content
168+ assert "legacy-key" in content
169+ assert not legacy_file .exists ()
170+ assert extra_file .exists ()
171+ assert extra_file .read_text (encoding = "utf-8" ) == "keep"
172+
173+
174+ def test_migrate_legacy_config_promotes_structured_config (tmp_path , monkeypatch ):
175+ legacy_dir = tmp_path / "legacy"
176+ legacy_dir .mkdir ()
177+ legacy_file = legacy_dir / "config"
178+ legacy_file .write_text ('apikey = "legacy-key"\n ' , encoding = "utf-8" )
179+ (legacy_dir / "extra.txt" ).write_text ("keep" , encoding = "utf-8" )
180+
181+ canonical_dir = tmp_path / "canonical"
182+
183+ monkeypatch .setattr (config_loader , "legacy_config_directory" , lambda : legacy_dir )
184+ monkeypatch .setattr (config_loader , "canonical_config_directory" , lambda : canonical_dir )
185+
186+ destination = migrate_legacy_config ()
187+
188+ assert destination == canonical_dir
189+ config_toml = canonical_dir / "config.toml"
190+ assert config_toml .exists ()
191+ content = config_toml .read_text (encoding = "utf-8" )
192+ assert "[web]" in content
193+ assert "[logging]" in content
194+ assert "Lowest logging level that will be emitted." in content
195+ assert "legacy-key" in content
196+ assert not (canonical_dir / "config" ).exists ()
197+ extra_file = canonical_dir / "extra.txt"
198+ assert extra_file .exists ()
199+ assert extra_file .read_text (encoding = "utf-8" ) == "keep"
200+ assert legacy_dir .exists ()
0 commit comments