-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmockstorage.go
70 lines (59 loc) · 1.98 KB
/
mockstorage.go
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
package testutil
import (
"context"
"errors"
"net/http"
"os"
"sort"
"github.com/coder/code-marketplace/storage"
)
var _ storage.Storage = (*MockStorage)(nil)
// MockStorage implements storage.Storage for tests.
type MockStorage struct{}
func NewMockStorage() *MockStorage {
return &MockStorage{}
}
func (s *MockStorage) AddExtension(ctx context.Context, manifest *storage.VSIXManifest, vsix []byte, extra ...storage.File) (string, error) {
return "", errors.New("not implemented")
}
func (s *MockStorage) FileServer() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/nonexistent" {
http.Error(rw, "not found", http.StatusNotFound)
} else {
_, _ = rw.Write([]byte("foobar"))
}
})
}
func (s *MockStorage) Manifest(ctx context.Context, publisher, name string, version storage.Version) (*storage.VSIXManifest, error) {
for _, ext := range Extensions {
if ext.Publisher == publisher && ext.Name == name {
for _, ver := range ext.Versions {
// Use the string encoding to match since that is how the real storage
// implementations will do it too.
if ver.String() == version.String() {
return ConvertExtensionToManifest(ext, ver), nil
}
}
break
}
}
return nil, os.ErrNotExist
}
func (s *MockStorage) RemoveExtension(ctx context.Context, publisher, name string, version storage.Version) error {
return errors.New("not implemented")
}
func (s *MockStorage) WalkExtensions(ctx context.Context, fn func(manifest *storage.VSIXManifest, versions []storage.Version) error) error {
for _, ext := range Extensions {
versions := make([]storage.Version, len(ext.Versions))
copy(versions, ext.Versions)
sort.Sort(storage.ByVersion(versions))
if err := fn(ConvertExtensionToManifest(ext, versions[0]), versions); err != nil {
return nil
}
}
return nil
}
func (s *MockStorage) Versions(ctx context.Context, publisher, name string) ([]storage.Version, error) {
return nil, errors.New("not implemented")
}