11"""Tests for JavaScript/TypeScript project initialization and package manager detection."""
22
33import json
4+ import tempfile
45from pathlib import Path
56from unittest .mock import patch
67
1718
1819
1920@pytest .fixture
20- def tmp_project (tmp_path : Path ) -> Path :
21- """Create a temporary project directory."""
22- return tmp_path
21+ def tmp_project () -> Path :
22+ """Create a temporary project directory with a deterministic parent chain."""
23+ with tempfile .TemporaryDirectory (dir = Path .cwd ()) as tmp_dir :
24+ yield Path (tmp_dir )
25+
26+
27+ def assert_install_command (actual : list [str ], executable : str , expected_args : list [str ]) -> None :
28+ assert Path (actual [0 ]).stem .lower () == executable
29+ assert actual [1 :] == expected_args
2330
2431
2532class TestDetermineJsPackageManager :
@@ -206,7 +213,7 @@ def test_npm_install_command(self, tmp_project: Path) -> None:
206213
207214 result = get_package_install_command (tmp_project , "codeflash" , dev = True )
208215
209- assert result == [ "npm" , "install" , "codeflash" , "--save-dev" ]
216+ assert_install_command ( result , "npm" , [ "install" , "codeflash" , "--save-dev" ])
210217
211218 def test_npm_install_command_non_dev (self , tmp_project : Path ) -> None :
212219 """Should return npm install command without --save-dev when dev=False."""
@@ -215,7 +222,7 @@ def test_npm_install_command_non_dev(self, tmp_project: Path) -> None:
215222
216223 result = get_package_install_command (tmp_project , "codeflash" , dev = False )
217224
218- assert result == [ "npm" , "install" , "codeflash" ]
225+ assert_install_command ( result , "npm" , [ "install" , "codeflash" ])
219226
220227 def test_pnpm_add_command (self , tmp_project : Path ) -> None :
221228 """Should return pnpm add command for pnpm projects."""
@@ -224,7 +231,7 @@ def test_pnpm_add_command(self, tmp_project: Path) -> None:
224231
225232 result = get_package_install_command (tmp_project , "codeflash" , dev = True )
226233
227- assert result == [ "pnpm" , "add" , "codeflash" , "--save-dev" ]
234+ assert_install_command ( result , "pnpm" , [ "add" , "codeflash" , "--save-dev" ])
228235
229236 def test_pnpm_add_command_non_dev (self , tmp_project : Path ) -> None :
230237 """Should return pnpm add command without --save-dev when dev=False."""
@@ -233,7 +240,7 @@ def test_pnpm_add_command_non_dev(self, tmp_project: Path) -> None:
233240
234241 result = get_package_install_command (tmp_project , "codeflash" , dev = False )
235242
236- assert result == [ "pnpm" , "add" , "codeflash" ]
243+ assert_install_command ( result , "pnpm" , [ "add" , "codeflash" ])
237244
238245 def test_yarn_add_command (self , tmp_project : Path ) -> None :
239246 """Should return yarn add command for yarn projects."""
@@ -242,7 +249,7 @@ def test_yarn_add_command(self, tmp_project: Path) -> None:
242249
243250 result = get_package_install_command (tmp_project , "codeflash" , dev = True )
244251
245- assert result == [ "yarn" , "add" , "codeflash" , "--dev" ]
252+ assert_install_command ( result , "yarn" , [ "add" , "codeflash" , "--dev" ])
246253
247254 def test_yarn_add_command_non_dev (self , tmp_project : Path ) -> None :
248255 """Should return yarn add command without --dev when dev=False."""
@@ -251,7 +258,7 @@ def test_yarn_add_command_non_dev(self, tmp_project: Path) -> None:
251258
252259 result = get_package_install_command (tmp_project , "codeflash" , dev = False )
253260
254- assert result == [ "yarn" , "add" , "codeflash" ]
261+ assert_install_command ( result , "yarn" , [ "add" , "codeflash" ])
255262
256263 def test_bun_add_command (self , tmp_project : Path ) -> None :
257264 """Should return bun add command for bun projects."""
@@ -260,7 +267,7 @@ def test_bun_add_command(self, tmp_project: Path) -> None:
260267
261268 result = get_package_install_command (tmp_project , "codeflash" , dev = True )
262269
263- assert result == [ "bun" , "add" , "codeflash" , "--dev" ]
270+ assert_install_command ( result , "bun" , [ "add" , "codeflash" , "--dev" ])
264271
265272 def test_bun_add_command_non_dev (self , tmp_project : Path ) -> None :
266273 """Should return bun add command without --dev when dev=False."""
@@ -269,14 +276,14 @@ def test_bun_add_command_non_dev(self, tmp_project: Path) -> None:
269276
270277 result = get_package_install_command (tmp_project , "codeflash" , dev = False )
271278
272- assert result == [ "bun" , "add" , "codeflash" ]
279+ assert_install_command ( result , "bun" , [ "add" , "codeflash" ])
273280
274281 def test_defaults_to_npm_for_unknown (self , tmp_project : Path ) -> None :
275282 """Should default to npm for unknown package manager."""
276283 # No lockfile, no package.json - unknown package manager
277284 result = get_package_install_command (tmp_project , "codeflash" , dev = True )
278285
279- assert result == [ "npm" , "install" , "codeflash" , "--save-dev" ]
286+ assert_install_command ( result , "npm" , [ "install" , "codeflash" , "--save-dev" ])
280287
281288 def test_different_package_name (self , tmp_project : Path ) -> None :
282289 """Should work with different package names."""
@@ -285,7 +292,7 @@ def test_different_package_name(self, tmp_project: Path) -> None:
285292
286293 result = get_package_install_command (tmp_project , "typescript" , dev = True )
287294
288- assert result == [ "pnpm" , "add" , "typescript" , "--save-dev" ]
295+ assert_install_command ( result , "pnpm" , [ "add" , "typescript" , "--save-dev" ])
289296
290297
291298class TestShouldModifySkipConfirm :
0 commit comments