|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import subprocess |
| 4 | +import pytest |
| 5 | +from pathlib import Path |
| 6 | +import shutil |
| 7 | +from unittest.mock import patch, MagicMock |
| 8 | + |
| 9 | +from nemo_run.devspace.editor import find_editor_executable |
| 10 | + |
| 11 | + |
| 12 | +class TestFindEditorExecutable: |
| 13 | + def test_unsupported_editor(self): |
| 14 | + """Test that unsupported editors raise ValueError.""" |
| 15 | + with pytest.raises(ValueError, match="not supported"): |
| 16 | + find_editor_executable("unsupported_editor") |
| 17 | + |
| 18 | + def test_editor_not_installed(self, monkeypatch): |
| 19 | + """Test that missing editors raise EnvironmentError.""" |
| 20 | + # Monkeypatch shutil.which to return None (simulate editor not found) |
| 21 | + monkeypatch.setattr(shutil, "which", lambda x: None) |
| 22 | + |
| 23 | + with pytest.raises(EnvironmentError, match="is not installed"): |
| 24 | + find_editor_executable("code") |
| 25 | + |
| 26 | + with pytest.raises(EnvironmentError, match="is not installed"): |
| 27 | + find_editor_executable("cursor") |
| 28 | + |
| 29 | + def test_non_wsl_environment(self, tmp_path, monkeypatch): |
| 30 | + """Test editor detection in non-WSL environment using real file.""" |
| 31 | + # Create a fake editor executable in a temp directory |
| 32 | + bin_dir = tmp_path / "bin" |
| 33 | + bin_dir.mkdir() |
| 34 | + |
| 35 | + code_exec = bin_dir / "code" |
| 36 | + code_exec.touch(mode=0o755) # Make it executable |
| 37 | + |
| 38 | + cursor_exec = bin_dir / "cursor" |
| 39 | + cursor_exec.touch(mode=0o755) |
| 40 | + |
| 41 | + # Add our temp directory to PATH |
| 42 | + old_path = os.environ.get("PATH", "") |
| 43 | + os.environ["PATH"] = f"{bin_dir}:{old_path}" |
| 44 | + |
| 45 | + try: |
| 46 | + # Monkeypatch os.uname to return a non-WSL environment |
| 47 | + if hasattr(os, "uname"): # Skip on Windows |
| 48 | + monkeypatch.setattr(os, "uname", lambda: MagicMock(release="Linux 5.10.0")) |
| 49 | + |
| 50 | + # Test with actual executables in path |
| 51 | + assert find_editor_executable("code") == "code" |
| 52 | + assert find_editor_executable("cursor") == "cursor" |
| 53 | + finally: |
| 54 | + # Restore PATH |
| 55 | + os.environ["PATH"] = old_path |
| 56 | + |
| 57 | + @pytest.mark.skipif( |
| 58 | + platform.system() == "Windows", reason="WSL tests only relevant on Unix systems" |
| 59 | + ) |
| 60 | + def test_wsl_environment(self, tmp_path, monkeypatch): |
| 61 | + """Test editor detection in WSL environment.""" |
| 62 | + # Create directory structure with both Linux and "Windows" executables |
| 63 | + bin_dir = tmp_path / "bin" |
| 64 | + bin_dir.mkdir() |
| 65 | + |
| 66 | + # Linux executables |
| 67 | + code_exec = bin_dir / "code" |
| 68 | + code_exec.touch(mode=0o755) |
| 69 | + |
| 70 | + cursor_exec = bin_dir / "cursor" |
| 71 | + cursor_exec.touch(mode=0o755) |
| 72 | + |
| 73 | + # Windows .exe files at various levels |
| 74 | + exe_dir = tmp_path / "winbin" |
| 75 | + exe_dir.mkdir() |
| 76 | + code_exe = exe_dir / "Code.exe" |
| 77 | + code_exe.touch(mode=0o755) |
| 78 | + |
| 79 | + cursor_exe_dir = tmp_path / "apps" / "cursor" |
| 80 | + cursor_exe_dir.mkdir(parents=True) |
| 81 | + cursor_exe = cursor_exe_dir / "Cursor.exe" |
| 82 | + cursor_exe.touch(mode=0o755) |
| 83 | + |
| 84 | + # Add our temp directory to PATH |
| 85 | + old_path = os.environ.get("PATH", "") |
| 86 | + os.environ["PATH"] = f"{bin_dir}:{old_path}" |
| 87 | + |
| 88 | + try: |
| 89 | + # Mock WSL environment |
| 90 | + monkeypatch.setattr(os, "name", "posix") |
| 91 | + monkeypatch.setattr(os, "uname", lambda: MagicMock(release="Microsoft-WSL")) |
| 92 | + |
| 93 | + # Test cases with different configurations |
| 94 | + |
| 95 | + # 1. Case where we find the .exe file at a specific location |
| 96 | + with monkeypatch.context() as m: |
| 97 | + |
| 98 | + def mock_which(cmd): |
| 99 | + if cmd == "code": |
| 100 | + return str(code_exec) |
| 101 | + elif cmd == "cursor": |
| 102 | + return str(cursor_exec) |
| 103 | + return None |
| 104 | + |
| 105 | + # Only need to mock exists for the specific paths we want to test |
| 106 | + original_exists = Path.exists |
| 107 | + |
| 108 | + def mock_exists(self): |
| 109 | + if ( |
| 110 | + self == code_exe |
| 111 | + or self.name == "Code.exe" |
| 112 | + and str(self).startswith(str(tmp_path)) |
| 113 | + ): |
| 114 | + return True |
| 115 | + if ( |
| 116 | + self == cursor_exe |
| 117 | + or self.name == "Cursor.exe" |
| 118 | + and str(self).startswith(str(tmp_path)) |
| 119 | + ): |
| 120 | + return True |
| 121 | + return original_exists(self) |
| 122 | + |
| 123 | + m.setattr(shutil, "which", mock_which) |
| 124 | + m.setattr(Path, "exists", mock_exists) |
| 125 | + |
| 126 | + # Test code with .exe available |
| 127 | + result = find_editor_executable("code") |
| 128 | + assert "Code.exe" in result |
| 129 | + |
| 130 | + # Test cursor with .exe available |
| 131 | + result = find_editor_executable("cursor") |
| 132 | + assert "Cursor.exe" in result |
| 133 | + |
| 134 | + # 2. Case where we don't find the .exe file (should now raise error) |
| 135 | + with monkeypatch.context() as m: |
| 136 | + |
| 137 | + def mock_which(cmd): |
| 138 | + if cmd == "code": |
| 139 | + return str(code_exec) |
| 140 | + elif cmd == "cursor": |
| 141 | + return str(cursor_exec) |
| 142 | + return None |
| 143 | + |
| 144 | + # Make exists always return False for .exe files |
| 145 | + def mock_exists(self): |
| 146 | + if ".exe" in str(self).lower(): |
| 147 | + return False |
| 148 | + return original_exists(self) |
| 149 | + |
| 150 | + m.setattr(shutil, "which", mock_which) |
| 151 | + m.setattr(Path, "exists", mock_exists) |
| 152 | + |
| 153 | + # Test code with no .exe available (should now raise error) |
| 154 | + with pytest.raises(EnvironmentError, match="Running in WSL but couldn't find"): |
| 155 | + find_editor_executable("code") |
| 156 | + |
| 157 | + # Test cursor with no .exe available (should now raise error) |
| 158 | + with pytest.raises(EnvironmentError, match="Running in WSL but couldn't find"): |
| 159 | + find_editor_executable("cursor") |
| 160 | + |
| 161 | + finally: |
| 162 | + # Restore PATH |
| 163 | + os.environ["PATH"] = old_path |
0 commit comments