1+ import re
12import tempfile
23import unittest
34from pathlib import Path
@@ -109,21 +110,134 @@ def test_load_project_config_from_returns_root_product_and_marker(self):
109110 project_root / "scripts" / "ci" / "package_windows_portable.py"
110111 )
111112 tauri_config_path = project_root / "src-tauri" / "tauri.conf.json"
113+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
112114 marker_path = project_root / MODULE .PORTABLE_RUNTIME_MARKER_RELATIVE_PATH
113115
114116 script_path .parent .mkdir (parents = True )
115117 script_path .write_text ("# placeholder" )
116118 tauri_config_path .parent .mkdir (parents = True )
117119 tauri_config_path .write_text ('{"productName":"AstrBot"}' )
120+ cargo_toml_path .write_text ('[package]\n name = "astrbot-desktop-tauri"\n ' )
118121 marker_path .parent .mkdir (parents = True , exist_ok = True )
119122 marker_path .write_text ("portable.flag\n " )
120123
121124 project_config = MODULE .load_project_config_from (script_path )
122125
123126 self .assertEqual (project_config .root , project_root .resolve ())
124127 self .assertEqual (project_config .product_name , "AstrBot" )
128+ self .assertEqual (project_config .binary_name , "astrbot-desktop-tauri" )
125129 self .assertEqual (project_config .portable_marker_name , "portable.flag" )
126130
131+ def test_load_cargo_package_name_supports_inline_comments (self ):
132+ with tempfile .TemporaryDirectory () as tmpdir :
133+ project_root = Path (tmpdir )
134+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
135+ cargo_toml_path .parent .mkdir (parents = True )
136+ cargo_toml_path .write_text (
137+ '[package]\n name = "astrbot-desktop-tauri" # main binary\n '
138+ )
139+
140+ self .assertEqual (
141+ MODULE .load_binary_name_from_cargo (project_root ),
142+ "astrbot-desktop-tauri" ,
143+ )
144+
145+ def test_load_cargo_package_name_missing_cargo_toml_raises_file_not_found (self ):
146+ with tempfile .TemporaryDirectory () as tmpdir :
147+ project_root = Path (tmpdir )
148+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
149+
150+ with self .assertRaisesRegex (
151+ FileNotFoundError , re .escape (str (cargo_toml_path ))
152+ ):
153+ MODULE .load_binary_name_from_cargo (project_root )
154+
155+ def test_load_cargo_package_name_missing_package_table_raises_value_error (self ):
156+ with tempfile .TemporaryDirectory () as tmpdir :
157+ project_root = Path (tmpdir )
158+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
159+ cargo_toml_path .parent .mkdir (parents = True )
160+ cargo_toml_path .write_text ('[workspace]\n members = ["crates/*"]\n ' )
161+
162+ with self .assertRaisesRegex (ValueError , re .escape (str (cargo_toml_path ))):
163+ MODULE .load_binary_name_from_cargo (project_root )
164+
165+ def test_load_cargo_package_name_missing_package_name_raises_value_error (self ):
166+ with tempfile .TemporaryDirectory () as tmpdir :
167+ project_root = Path (tmpdir )
168+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
169+ cargo_toml_path .parent .mkdir (parents = True )
170+ cargo_toml_path .write_text ('[package]\n version = "0.1.0"\n ' )
171+
172+ with self .assertRaisesRegex (ValueError , re .escape (str (cargo_toml_path ))):
173+ MODULE .load_binary_name_from_cargo (project_root )
174+
175+ def test_load_cargo_package_name_empty_package_name_raises_value_error (self ):
176+ with tempfile .TemporaryDirectory () as tmpdir :
177+ project_root = Path (tmpdir )
178+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
179+ cargo_toml_path .parent .mkdir (parents = True )
180+ cargo_toml_path .write_text ('[package]\n name = ""\n ' )
181+
182+ with self .assertRaisesRegex (ValueError , re .escape (str (cargo_toml_path ))):
183+ MODULE .load_binary_name_from_cargo (project_root )
184+
185+ def test_load_cargo_package_name_falls_back_to_package_when_bin_missing_name (self ):
186+ with tempfile .TemporaryDirectory () as tmpdir :
187+ project_root = Path (tmpdir )
188+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
189+ cargo_toml_path .parent .mkdir (parents = True )
190+ cargo_toml_path .write_text (
191+ "[package]\n "
192+ 'name = "astrbot-desktop-tauri"\n \n '
193+ "[[bin]]\n "
194+ 'path = "src/main.rs"\n '
195+ )
196+
197+ self .assertEqual (
198+ MODULE .load_binary_name_from_cargo (project_root ),
199+ "astrbot-desktop-tauri" ,
200+ )
201+
202+ def test_load_cargo_package_name_prefers_explicit_bin_name (self ):
203+ with tempfile .TemporaryDirectory () as tmpdir :
204+ project_root = Path (tmpdir )
205+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
206+ cargo_toml_path .parent .mkdir (parents = True )
207+ cargo_toml_path .write_text (
208+ "[package]\n "
209+ 'name = "astrbot-desktop-tauri"\n \n '
210+ "[[bin]]\n "
211+ 'name = "AstrBot"\n '
212+ )
213+
214+ self .assertEqual (
215+ MODULE .load_binary_name_from_cargo (project_root ), "AstrBot"
216+ )
217+
218+ def test_resolve_main_executable_path_uses_binary_name_not_product_name (self ):
219+ with tempfile .TemporaryDirectory () as tmpdir :
220+ project_root = Path (tmpdir )
221+ bundle_dir = (
222+ project_root / "src-tauri" / "target" / "release" / "bundle" / "nsis"
223+ )
224+ release_dir = project_root / "src-tauri" / "target" / "release"
225+ bundle_dir .mkdir (parents = True )
226+ release_dir .mkdir (parents = True , exist_ok = True )
227+ (release_dir / "astrbot-desktop-tauri.exe" ).write_text ("exe" )
228+
229+ project_config = MODULE .ProjectConfig (
230+ root = project_root ,
231+ product_name = "AstrBot" ,
232+ binary_name = "astrbot-desktop-tauri" ,
233+ portable_marker_name = "portable.flag" ,
234+ )
235+
236+ self .assertEqual (
237+ MODULE .resolve_main_executable_path (bundle_dir , project_config ),
238+ release_dir / "astrbot-desktop-tauri.exe" ,
239+ )
240+
127241 def test_iter_installer_paths_only_returns_installer_style_executables (self ):
128242 with tempfile .TemporaryDirectory () as tmpdir :
129243 bundle_dir = Path (tmpdir )
@@ -156,6 +270,7 @@ def test_populate_portable_root_copies_release_bundle_contents(self):
156270 webui_dir = project_root / "resources" / "webui"
157271 windows_dir = project_root / "src-tauri" / "windows"
158272 tauri_config_path = project_root / "src-tauri" / "tauri.conf.json"
273+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
159274 marker_path = project_root / MODULE .PORTABLE_RUNTIME_MARKER_RELATIVE_PATH
160275
161276 script_path .parent .mkdir (parents = True )
@@ -167,8 +282,9 @@ def test_populate_portable_root_copies_release_bundle_contents(self):
167282 windows_dir .mkdir (parents = True )
168283
169284 tauri_config_path .write_text ('{"productName":"AstrBot"}' )
285+ cargo_toml_path .write_text ('[package]\n name = "astrbot-desktop-tauri"\n ' )
170286 marker_path .write_text ("portable.flag\n " )
171- (release_dir / "AstrBot .exe" ).write_text ("exe" )
287+ (release_dir / "astrbot-desktop-tauri .exe" ).write_text ("exe" )
172288 (release_dir / "WebView2Loader.dll" ).write_text ("dll" )
173289 (backend_dir / "runtime-manifest.json" ).write_text ("{}" )
174290 (backend_dir / "launch_backend.py" ).write_text ("print('ok')" )
@@ -183,7 +299,7 @@ def test_populate_portable_root_copies_release_bundle_contents(self):
183299 project_config = MODULE .load_project_config_from (script_path ),
184300 )
185301
186- self .assertTrue ((destination_root / "AstrBot .exe" ).is_file ())
302+ self .assertTrue ((destination_root / "astrbot-desktop-tauri .exe" ).is_file ())
187303 self .assertTrue ((destination_root / "WebView2Loader.dll" ).is_file ())
188304 self .assertTrue (
189305 (
@@ -210,6 +326,7 @@ def test_populate_portable_root_rejects_missing_main_executable(self):
210326 backend_dir = project_root / "resources" / "backend"
211327 webui_dir = project_root / "resources" / "webui"
212328 tauri_config_path = project_root / "src-tauri" / "tauri.conf.json"
329+ cargo_toml_path = project_root / "src-tauri" / "Cargo.toml"
213330 marker_path = project_root / MODULE .PORTABLE_RUNTIME_MARKER_RELATIVE_PATH
214331
215332 script_path .parent .mkdir (parents = True )
@@ -219,6 +336,7 @@ def test_populate_portable_root_rejects_missing_main_executable(self):
219336 webui_dir .mkdir (parents = True )
220337 tauri_config_path .parent .mkdir (parents = True , exist_ok = True )
221338 tauri_config_path .write_text ('{"productName":"AstrBot"}' )
339+ cargo_toml_path .write_text ('[package]\n name = "astrbot-desktop-tauri"\n ' )
222340 marker_path .parent .mkdir (parents = True , exist_ok = True )
223341 marker_path .write_text ("portable.flag\n " )
224342 (backend_dir / "runtime-manifest.json" ).write_text ("{}" )
@@ -237,6 +355,7 @@ def test_add_portable_runtime_files_writes_marker_and_readme(self):
237355 project_config = MODULE .ProjectConfig (
238356 root = Path (tmpdir ),
239357 product_name = "AstrBot" ,
358+ binary_name = "astrbot-desktop-tauri" ,
240359 portable_marker_name = "portable.flag" ,
241360 )
242361
0 commit comments