diff --git a/run-server.sh b/run-server.sh index bf7deb0..ac9b373 100755 --- a/run-server.sh +++ b/run-server.sh @@ -35,6 +35,14 @@ readonly DESKTOP_CONFIG_FLAG=".desktop_configured" readonly LOG_DIR="logs" readonly LOG_FILE="mcp_server.log" +# Determine portable arguments for sed -i (GNU vs BSD) +declare -a SED_INPLACE_ARGS +if sed --version >/dev/null 2>&1; then + SED_INPLACE_ARGS=(-i) +else + SED_INPLACE_ARGS=(-i "") +fi + # ---------------------------------------------------------------------------- # Utility Functions # ---------------------------------------------------------------------------- @@ -1057,14 +1065,6 @@ setup_env_file() { cp .env.example .env print_success "Created .env from .env.example" - # Detect sed version for cross-platform compatibility - local sed_cmd - if sed --version >/dev/null 2>&1; then - sed_cmd="sed -i" # GNU sed (Linux) - else - sed_cmd="sed -i ''" # BSD sed (macOS) - fi - # Update API keys from environment if present local api_keys=( "GEMINI_API_KEY:your_gemini_api_key_here" @@ -1080,7 +1080,7 @@ setup_env_file() { local key_value="${!key_name:-}" if [[ -n "$key_value" ]]; then - $sed_cmd "s/$placeholder/$key_value/" .env + sed "${SED_INPLACE_ARGS[@]}" "s/$placeholder/$key_value/" .env print_success "Updated .env with $key_name from environment" fi done @@ -1100,16 +1100,8 @@ migrate_env_file() { # Create backup cp .env .env.backup_$(date +%Y%m%d_%H%M%S) - # Detect sed version for cross-platform compatibility - local sed_cmd - if sed --version >/dev/null 2>&1; then - sed_cmd="sed -i" # GNU sed (Linux) - else - sed_cmd="sed -i ''" # BSD sed (macOS) - fi - # Replace host.docker.internal with localhost - $sed_cmd 's/host\.docker\.internal/localhost/g' .env + sed "${SED_INPLACE_ARGS[@]}" 's/host\.docker\.internal/localhost/g' .env print_success "Migrated Docker URLs to localhost in .env" echo " (Backup saved as .env.backup_*)" @@ -2499,5 +2491,6 @@ main() { # Script Entry Point # ---------------------------------------------------------------------------- -# Run main function with all arguments -main "$@" +if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then + main "$@" +fi diff --git a/tests/test_pip_detection_fix.py b/tests/test_pip_detection_fix.py index 3e8ec2e..9c88107 100644 --- a/tests/test_pip_detection_fix.py +++ b/tests/test_pip_detection_fix.py @@ -4,6 +4,7 @@ This test file ensures our pip detection improvements work correctly and don't break existing functionality. """ +import os import subprocess import tempfile from pathlib import Path @@ -99,6 +100,29 @@ class TestPipDetectionFix: for pattern in expected_diagnostic_patterns: assert pattern in content, f"Enhanced diagnostic pattern '{pattern}' should be in script" + def test_setup_env_file_does_not_create_bsd_backup(self, tmp_path): + """Ensure setup_env_file avoids creating .env'' artifacts (BSD sed behavior).""" + script_path = Path("./run-server.sh").resolve() + + # Prepare temp workspace with example env + env_example = Path(".env.example").read_text() + target_example = tmp_path / ".env.example" + target_example.write_text(env_example) + + # Run setup_env_file inside isolated shell session + command = f""" + set -e + cd "{tmp_path}" + source "{script_path}" + setup_env_file + """ + env = os.environ.copy() + subprocess.run(["bash", "-lc", command], check=True, env=env, text=True) + + artifacts = {p.name for p in tmp_path.glob(".env*")} + assert ".env''" not in artifacts, "setup_env_file should not create BSD sed backup artifacts" + assert ".env" in artifacts, ".env should be created from .env.example" + if __name__ == "__main__": pytest.main([__file__, "-v"])