|
1 | 1 | package vscode |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "runtime" |
7 | 8 | "testing" |
8 | 9 |
|
| 10 | + "github.com/databricks/cli/libs/cmdio" |
9 | 11 | "github.com/stretchr/testify/assert" |
10 | 12 | "github.com/stretchr/testify/require" |
11 | 13 | ) |
@@ -88,3 +90,208 @@ func TestCheckIDECommand_Found(t *testing.T) { |
88 | 90 | }) |
89 | 91 | } |
90 | 92 | } |
| 93 | + |
| 94 | +func TestParseExtensionVersion(t *testing.T) { |
| 95 | + tests := []struct { |
| 96 | + name string |
| 97 | + output string |
| 98 | + extensionID string |
| 99 | + wantVersion string |
| 100 | + wantFound bool |
| 101 | + minVersion string |
| 102 | + wantAtLeast bool |
| 103 | + }{ |
| 104 | + { |
| 105 | + name: "found and above minimum", |
| 106 | + output: "ms-python.python@2024.1.1\nms-vscode-remote.remote-ssh@0.123.0\n", |
| 107 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 108 | + wantVersion: "0.123.0", |
| 109 | + wantFound: true, |
| 110 | + minVersion: "0.120.0", |
| 111 | + wantAtLeast: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "found but below minimum", |
| 115 | + output: "ms-vscode-remote.remote-ssh@0.100.0\n", |
| 116 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 117 | + wantVersion: "0.100.0", |
| 118 | + wantFound: true, |
| 119 | + minVersion: "0.120.0", |
| 120 | + wantAtLeast: false, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "not found", |
| 124 | + output: "ms-python.python@2024.1.1\n", |
| 125 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 126 | + wantVersion: "", |
| 127 | + wantFound: false, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "empty output", |
| 131 | + output: "", |
| 132 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 133 | + wantVersion: "", |
| 134 | + wantFound: false, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "multiple extensions", |
| 138 | + output: "ext.a@1.0.0\next.b@2.0.0\next.c@3.0.0\n", |
| 139 | + extensionID: "ext.b", |
| 140 | + wantVersion: "2.0.0", |
| 141 | + wantFound: true, |
| 142 | + minVersion: "1.0.0", |
| 143 | + wantAtLeast: true, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "prerelease is less than release", |
| 147 | + output: "ms-vscode-remote.remote-ssh@0.120.0-beta.1\n", |
| 148 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 149 | + wantVersion: "0.120.0-beta.1", |
| 150 | + wantFound: true, |
| 151 | + minVersion: "0.120.0", |
| 152 | + wantAtLeast: false, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "line with whitespace", |
| 156 | + output: " ms-vscode-remote.remote-ssh@0.123.0 \n", |
| 157 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 158 | + wantVersion: "0.123.0", |
| 159 | + wantFound: true, |
| 160 | + minVersion: "0.120.0", |
| 161 | + wantAtLeast: true, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "windows CRLF line endings", |
| 165 | + output: "ms-python.python@2024.1.1\r\nms-vscode-remote.remote-ssh@0.123.0\r\n", |
| 166 | + extensionID: "ms-vscode-remote.remote-ssh", |
| 167 | + wantVersion: "0.123.0", |
| 168 | + wantFound: true, |
| 169 | + minVersion: "0.120.0", |
| 170 | + wantAtLeast: true, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + for _, tt := range tests { |
| 175 | + t.Run(tt.name, func(t *testing.T) { |
| 176 | + version, found := parseExtensionVersion(tt.output, tt.extensionID) |
| 177 | + assert.Equal(t, tt.wantFound, found) |
| 178 | + assert.Equal(t, tt.wantVersion, version) |
| 179 | + if found { |
| 180 | + assert.Equal(t, tt.wantAtLeast, isExtensionVersionAtLeast(version, tt.minVersion)) |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestIsExtensionVersionAtLeast(t *testing.T) { |
| 187 | + tests := []struct { |
| 188 | + name string |
| 189 | + version string |
| 190 | + minVersion string |
| 191 | + want bool |
| 192 | + }{ |
| 193 | + {name: "above minimum", version: "0.123.0", minVersion: "0.120.0", want: true}, |
| 194 | + {name: "exact minimum", version: "0.120.0", minVersion: "0.120.0", want: true}, |
| 195 | + {name: "below minimum", version: "0.100.0", minVersion: "0.120.0", want: false}, |
| 196 | + {name: "major version ahead", version: "1.0.0", minVersion: "0.120.0", want: true}, |
| 197 | + {name: "prerelease below release", version: "0.120.0-beta.1", minVersion: "0.120.0", want: false}, |
| 198 | + {name: "prerelease above prior release", version: "0.121.0-beta.1", minVersion: "0.120.0", want: true}, |
| 199 | + {name: "two-component version is valid", version: "1.0", minVersion: "0.120.0", want: true}, |
| 200 | + {name: "empty version", version: "", minVersion: "0.120.0", want: false}, |
| 201 | + {name: "garbage version", version: "abc", minVersion: "0.120.0", want: false}, |
| 202 | + {name: "four-component version is invalid", version: "0.120.0.1", minVersion: "0.120.0", want: false}, |
| 203 | + {name: "cursor exact minimum", version: "1.0.32", minVersion: "1.0.32", want: true}, |
| 204 | + {name: "cursor above minimum", version: "1.1.0", minVersion: "1.0.32", want: true}, |
| 205 | + {name: "cursor below minimum", version: "1.0.31", minVersion: "1.0.32", want: false}, |
| 206 | + } |
| 207 | + |
| 208 | + for _, tt := range tests { |
| 209 | + t.Run(tt.name, func(t *testing.T) { |
| 210 | + assert.Equal(t, tt.want, isExtensionVersionAtLeast(tt.version, tt.minVersion)) |
| 211 | + }) |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +// createFakeIDEExecutable creates a fake IDE command that outputs the given text |
| 216 | +// when called with --list-extensions --show-versions. |
| 217 | +func createFakeIDEExecutable(t *testing.T, dir, command, output string) { |
| 218 | + t.Helper() |
| 219 | + if runtime.GOOS == "windows" { |
| 220 | + // Write output to a temp file and use "type" to print it, avoiding escaping issues. |
| 221 | + payloadPath := filepath.Join(dir, command+"-payload.txt") |
| 222 | + err := os.WriteFile(payloadPath, []byte(output), 0o644) |
| 223 | + require.NoError(t, err) |
| 224 | + script := fmt.Sprintf("@echo off\ntype \"%s\"\n", payloadPath) |
| 225 | + err = os.WriteFile(filepath.Join(dir, command+".cmd"), []byte(script), 0o755) |
| 226 | + require.NoError(t, err) |
| 227 | + } else { |
| 228 | + // Use printf (a shell builtin) instead of cat to avoid PATH issues in tests. |
| 229 | + script := fmt.Sprintf("#!/bin/sh\nprintf '%%s' '%s'\n", output) |
| 230 | + err := os.WriteFile(filepath.Join(dir, command), []byte(script), 0o755) |
| 231 | + require.NoError(t, err) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +func TestCheckIDESSHExtension_UpToDate(t *testing.T) { |
| 236 | + tmpDir := t.TempDir() |
| 237 | + t.Setenv("PATH", tmpDir) |
| 238 | + ctx, _ := cmdio.NewTestContextWithStdout(t.Context()) |
| 239 | + |
| 240 | + extensionOutput := "ms-python.python@2024.1.1\nms-vscode-remote.remote-ssh@0.123.0\n" |
| 241 | + createFakeIDEExecutable(t, tmpDir, "code", extensionOutput) |
| 242 | + |
| 243 | + err := CheckIDESSHExtension(ctx, VSCodeOption) |
| 244 | + assert.NoError(t, err) |
| 245 | +} |
| 246 | + |
| 247 | +func TestCheckIDESSHExtension_ExactMinVersion(t *testing.T) { |
| 248 | + tmpDir := t.TempDir() |
| 249 | + t.Setenv("PATH", tmpDir) |
| 250 | + ctx, _ := cmdio.NewTestContextWithStdout(t.Context()) |
| 251 | + |
| 252 | + extensionOutput := "ms-vscode-remote.remote-ssh@0.120.0\n" |
| 253 | + createFakeIDEExecutable(t, tmpDir, "code", extensionOutput) |
| 254 | + |
| 255 | + err := CheckIDESSHExtension(ctx, VSCodeOption) |
| 256 | + assert.NoError(t, err) |
| 257 | +} |
| 258 | + |
| 259 | +func TestCheckIDESSHExtension_Missing(t *testing.T) { |
| 260 | + tmpDir := t.TempDir() |
| 261 | + t.Setenv("PATH", tmpDir) |
| 262 | + ctx, _ := cmdio.NewTestContextWithStdout(t.Context()) |
| 263 | + |
| 264 | + extensionOutput := "ms-python.python@2024.1.1\n" |
| 265 | + createFakeIDEExecutable(t, tmpDir, "code", extensionOutput) |
| 266 | + |
| 267 | + err := CheckIDESSHExtension(ctx, VSCodeOption) |
| 268 | + require.Error(t, err) |
| 269 | + assert.Contains(t, err.Error(), `"Remote - SSH"`) |
| 270 | + assert.Contains(t, err.Error(), "not installed") |
| 271 | +} |
| 272 | + |
| 273 | +func TestCheckIDESSHExtension_Outdated(t *testing.T) { |
| 274 | + tmpDir := t.TempDir() |
| 275 | + t.Setenv("PATH", tmpDir) |
| 276 | + ctx, _ := cmdio.NewTestContextWithStdout(t.Context()) |
| 277 | + |
| 278 | + extensionOutput := "ms-vscode-remote.remote-ssh@0.100.0\n" |
| 279 | + createFakeIDEExecutable(t, tmpDir, "code", extensionOutput) |
| 280 | + |
| 281 | + err := CheckIDESSHExtension(ctx, VSCodeOption) |
| 282 | + require.Error(t, err) |
| 283 | + assert.Contains(t, err.Error(), "0.100.0") |
| 284 | + assert.Contains(t, err.Error(), ">= 0.120.0") |
| 285 | +} |
| 286 | + |
| 287 | +func TestCheckIDESSHExtension_Cursor(t *testing.T) { |
| 288 | + tmpDir := t.TempDir() |
| 289 | + t.Setenv("PATH", tmpDir) |
| 290 | + ctx, _ := cmdio.NewTestContextWithStdout(t.Context()) |
| 291 | + |
| 292 | + extensionOutput := "anysphere.remote-ssh@1.0.32\n" |
| 293 | + createFakeIDEExecutable(t, tmpDir, "cursor", extensionOutput) |
| 294 | + |
| 295 | + err := CheckIDESSHExtension(ctx, CursorOption) |
| 296 | + assert.NoError(t, err) |
| 297 | +} |
0 commit comments