-
Notifications
You must be signed in to change notification settings - Fork 3
/
backend_mock.go
48 lines (37 loc) · 1.18 KB
/
backend_mock.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
//go:build gowslmock
package gowsl
import (
"context"
"github.com/ubuntu/gowsl/internal/backend"
"github.com/ubuntu/gowsl/internal/backend/windows"
"github.com/ubuntu/gowsl/mock"
)
type backendQueryType int
const backendQuery backendQueryType = 0
// MockAvailable indicates if the mock can be accessed at runtime.
// It is always accessible at compile-time to make writing tests easier, but you should use:
//
// ctx := context.Background()
// if wsl.MockAvailable() {
// m := mock.New()
// // set up the mock ...
// ctx = wsl.WithMock(ctx, m)
// }
func MockAvailable() bool {
return true
}
// WithMock adds the mock back-end to the context when GoWSL has been compiled with the gowslmock tag.
// Otherwise, it panics.
func WithMock(ctx context.Context, m *mock.Backend) context.Context {
return context.WithValue(ctx, backendQuery, m)
}
func selectBackend(ctx context.Context) backend.Backend {
v := ctx.Value(backendQuery)
if v == nil {
return windows.Backend{}
}
//nolint:forcetypeassert // The panic is expected and welcome
return v.(backend.Backend)
}
// ErrNotExist is the error returned when a distro does not exist.
var ErrNotExist = mock.ErrNotExist