-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_test_harness.go
More file actions
300 lines (253 loc) · 9.47 KB
/
Copy pathprovider_test_harness.go
File metadata and controls
300 lines (253 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package runtime
import (
"strings"
"testing"
)
// ProviderTestHarness runs a suite of contract tests against a Provider implementation
// This ensures all providers behave consistently and implement the interface correctly
type ProviderTestHarness struct {
Provider Provider
T *testing.T
// Expected values for validation
ExpectedName string
ExpectedDisplayName string
SampleVersion string // A valid version string for this runtime (e.g., "3.11.0")
}
// RunAllTests executes the complete test suite
func (h *ProviderTestHarness) RunAllTests() {
h.T.Run("Name", func(t *testing.T) { h.TestName(t) })
h.T.Run("DisplayName", func(t *testing.T) { h.TestDisplayName(t) })
h.T.Run("GetInstallPath", func(t *testing.T) { h.TestGetInstallPath(t) })
h.T.Run("GetExecutablePath", func(t *testing.T) { h.TestGetExecutablePath(t) })
h.T.Run("GetGlobalPackages", func(t *testing.T) { h.TestGetGlobalPackages(t) })
h.T.Run("GetManualPackageInstallCommand", func(t *testing.T) { h.TestGetManualPackageInstallCommand(t) })
h.T.Run("ListInstalled", func(t *testing.T) { h.TestListInstalled(t) })
h.T.Run("ListAvailable", func(t *testing.T) { h.TestListAvailable(t) })
h.T.Run("IsInstalled", func(t *testing.T) { h.TestIsInstalled(t) })
h.T.Run("GetGlobalVersion", func(t *testing.T) { h.TestGetGlobalVersion(t) })
h.T.Run("GetLocalVersion", func(t *testing.T) { h.TestGetLocalVersion(t) })
h.T.Run("GetCurrentVersion", func(t *testing.T) { h.TestGetCurrentVersion(t) })
}
// TestName verifies the provider returns the expected name
func (h *ProviderTestHarness) TestName(t *testing.T) {
name := h.Provider.Name()
if name == "" {
t.Error("Name() returned empty string")
}
if name != h.ExpectedName {
t.Errorf("Name() = %q, want %q", name, h.ExpectedName)
}
// Name should be lowercase (convention)
if name != strings.ToLower(name) {
t.Errorf("Name() = %q should be lowercase", name)
}
}
// TestDisplayName verifies the provider returns a human-readable name
func (h *ProviderTestHarness) TestDisplayName(t *testing.T) {
displayName := h.Provider.DisplayName()
if displayName == "" {
t.Error("DisplayName() returned empty string")
}
if displayName != h.ExpectedDisplayName {
t.Errorf("DisplayName() = %q, want %q", displayName, h.ExpectedDisplayName)
}
}
// TestGetInstallPath verifies install path follows expected patterns
func (h *ProviderTestHarness) TestGetInstallPath(t *testing.T) {
if h.SampleVersion == "" {
t.Skip("No sample version provided")
}
path, err := h.Provider.InstallPath(h.SampleVersion)
// It's OK to return error if version isn't installed
// But the path format should still be correct if returned
if err == nil {
if path == "" {
t.Error("GetInstallPath() returned empty path without error")
}
// Path should contain the runtime name
if !strings.Contains(strings.ToLower(path), h.Provider.Name()) {
t.Errorf("GetInstallPath() = %q does not contain runtime name %q", path, h.Provider.Name())
}
// Path should contain the version
if !strings.Contains(path, h.SampleVersion) {
t.Errorf("GetInstallPath() = %q does not contain version %q", path, h.SampleVersion)
}
}
}
// TestGetExecutablePath verifies executable path follows expected patterns
func (h *ProviderTestHarness) TestGetExecutablePath(t *testing.T) {
if h.SampleVersion == "" {
t.Skip("No sample version provided")
}
path, err := h.Provider.ExecutablePath(h.SampleVersion)
// It's OK to return error if version isn't installed
if err == nil {
if path == "" {
t.Error("GetExecutablePath() returned empty path without error")
}
// Path should be non-empty and look like a file path
if !strings.Contains(path, "/") && !strings.Contains(path, "\\") {
t.Errorf("GetExecutablePath() = %q does not look like a valid path", path)
}
}
}
// TestGetGlobalPackages verifies package detection doesn't panic and returns valid data
func (h *ProviderTestHarness) TestGetGlobalPackages(t *testing.T) {
// Test with a fake install path
packages, err := h.Provider.GlobalPackages("/fake/path")
// Should not panic and should return a slice (even if empty or error)
if err == nil && packages == nil {
t.Error("GetGlobalPackages() returned nil slice without error (should return empty slice)")
}
// If no error, should return valid slice
if err == nil {
for i, pkg := range packages {
if pkg == "" {
t.Errorf("GetGlobalPackages()[%d] is empty string", i)
}
}
}
}
// TestGetManualPackageInstallCommand verifies manual install commands are properly formatted
func (h *ProviderTestHarness) TestGetManualPackageInstallCommand(t *testing.T) {
tests := []struct {
name string
packages []string
wantNil bool
}{
{
name: "empty package list",
packages: []string{},
wantNil: true, // Empty input should return empty string
},
{
name: "single package",
packages: []string{"test-package"},
wantNil: false,
},
{
name: "multiple packages",
packages: []string{"package1", "package2", "package3"},
wantNil: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := h.Provider.ManualPackageInstallCommand(tt.packages)
if tt.wantNil && cmd != "" {
t.Errorf("GetManualPackageInstallCommand(%v) = %q, want empty string", tt.packages, cmd)
}
if !tt.wantNil && cmd == "" {
t.Errorf("GetManualPackageInstallCommand(%v) returned empty string", tt.packages)
}
// If command is returned, verify it contains the packages
if cmd != "" {
for _, pkg := range tt.packages {
if !strings.Contains(cmd, pkg) {
t.Errorf("GetManualPackageInstallCommand() = %q does not contain package %q", cmd, pkg)
}
}
}
})
}
}
// TestListInstalled verifies listing installed versions returns valid data
func (h *ProviderTestHarness) TestListInstalled(t *testing.T) {
versions, err := h.Provider.ListInstalled()
// Should not panic
// It's OK to have error or no versions if none are installed
if err == nil && versions == nil {
t.Error("ListInstalled() returned nil slice without error (should return empty slice)")
}
// If versions returned, validate structure
if err == nil {
for i, version := range versions {
if version.Version.Raw == "" {
t.Errorf("ListInstalled()[%d].Version.Raw is empty", i)
}
if version.InstallPath == "" {
t.Errorf("ListInstalled()[%d].InstallPath is empty", i)
}
}
}
}
// TestListAvailable verifies listing available versions returns valid data
func (h *ProviderTestHarness) TestListAvailable(t *testing.T) {
// Note: This may require network access, so we just verify it doesn't panic
// and returns proper structure
versions, err := h.Provider.ListAvailable()
// Should not panic
if err == nil && versions == nil {
t.Error("ListAvailable() returned nil slice without error (should return empty slice)")
}
// If versions returned, validate structure
if err == nil && len(versions) > 0 {
for i, version := range versions {
if version.Version.Raw == "" {
t.Errorf("ListAvailable()[%d].Version.Raw is empty", i)
}
// DownloadURL might be empty for some versions, so we don't check it
}
}
}
// TestIsInstalled verifies version checking works correctly
func (h *ProviderTestHarness) TestIsInstalled(t *testing.T) {
if h.SampleVersion == "" {
t.Skip("No sample version provided")
}
// Check a version (may or may not be installed)
installed, err := h.Provider.IsInstalled(h.SampleVersion)
// Should return boolean without panic
// Error is acceptable
_ = installed
_ = err
// Test with obviously invalid version
invalidInstalled, invalidErr := h.Provider.IsInstalled("999.999.999")
if invalidErr == nil && invalidInstalled {
t.Error("IsInstalled(\"999.999.999\") returned true (should be false)")
}
}
// TestGetGlobalVersion verifies global version retrieval
func (h *ProviderTestHarness) TestGetGlobalVersion(t *testing.T) {
version, err := h.Provider.GlobalVersion()
// ("", nil) is valid — means no global version configured
// If a version is returned, it should be non-empty
if err == nil && version != "" {
// Valid: a global version is configured
}
if err != nil {
// Valid: an actual error occurred (I/O, corrupt config)
t.Logf("GetGlobalVersion() returned error (may be expected): %v", err)
}
}
// TestGetLocalVersion verifies local version retrieval
func (h *ProviderTestHarness) TestGetLocalVersion(t *testing.T) {
version, err := h.Provider.LocalVersion()
// It's OK to have error if no local version is set
// But if version is returned, it should be non-empty
if err == nil && version == "" {
t.Error("GetLocalVersion() returned empty string without error")
}
}
// TestGetCurrentVersion verifies current version resolution
func (h *ProviderTestHarness) TestGetCurrentVersion(t *testing.T) {
version, err := h.Provider.CurrentVersion()
// It's OK to have error if no version is configured
// But if version is returned, it should be non-empty
if err == nil && version == "" {
t.Error("GetCurrentVersion() returned empty string without error")
}
}
// TestInstallGlobalPackages verifies package installation interface
func (h *ProviderTestHarness) TestInstallGlobalPackages(t *testing.T) {
if h.SampleVersion == "" {
t.Skip("No sample version provided")
}
// Test with empty package list (should not error)
err := h.Provider.InstallGlobalPackages(h.SampleVersion, []string{})
if err != nil {
t.Errorf("InstallGlobalPackages() with empty packages returned error: %v", err)
}
// Note: We don't test with actual packages as that would require
// the version to be installed and would modify the system
}