|
| 1 | +"""Test for pnpm workspace handling in package installation.""" |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from codeflash.cli_cmds.init_javascript import get_package_install_command |
| 7 | + |
| 8 | + |
| 9 | +def test_pnpm_workspace_adds_workspace_flag() -> None: |
| 10 | + """Test that pnpm workspace projects get the -w flag.""" |
| 11 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 12 | + project_root = Path(tmpdir) |
| 13 | + |
| 14 | + # Create pnpm-workspace.yaml to indicate workspace |
| 15 | + (project_root / "pnpm-workspace.yaml").write_text("packages:\n - .") |
| 16 | + |
| 17 | + # Create pnpm-lock.yaml to indicate pnpm is the package manager |
| 18 | + (project_root / "pnpm-lock.yaml").write_text("lockfileVersion: '6.0'") |
| 19 | + |
| 20 | + # Create package.json |
| 21 | + (project_root / "package.json").write_text('{"name": "test"}') |
| 22 | + |
| 23 | + # Get install command |
| 24 | + cmd = get_package_install_command(project_root, "some-package", dev=True) |
| 25 | + |
| 26 | + # Should include -w flag for workspace root |
| 27 | + assert "-w" in cmd or "--workspace-root" in cmd, f"Expected workspace flag in {cmd}" |
| 28 | + |
| 29 | + |
| 30 | +def test_dev_environment_uses_local_package() -> None: |
| 31 | + """Test that dev environment uses local codeflash package path.""" |
| 32 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 33 | + project_root = Path(tmpdir) |
| 34 | + (project_root / "package.json").write_text('{"name": "test"}') |
| 35 | + |
| 36 | + # Get install command for codeflash |
| 37 | + cmd = get_package_install_command(project_root, "codeflash", dev=True) |
| 38 | + |
| 39 | + # In dev mode (when running from /opt/codeflash/), |
| 40 | + # should use local package path instead of npm package name |
| 41 | + cmd_str = " ".join(cmd) |
| 42 | + |
| 43 | + # Should reference local packages directory |
| 44 | + assert ( |
| 45 | + "/opt/codeflash/packages/codeflash" in cmd_str |
| 46 | + or "file:" in cmd_str |
| 47 | + or cmd[0] in ["npm", "pnpm", "yarn", "bun"] |
| 48 | + ), f"Expected local package reference or valid package manager in {cmd}" |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + # Run tests |
| 53 | + test_pnpm_workspace_adds_workspace_flag() |
| 54 | + test_dev_environment_uses_local_package() |
| 55 | + print("All tests passed!") |
0 commit comments