-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
86 lines (74 loc) · 1.86 KB
/
helpers_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package nu
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/neilotoole/slogt"
"github.com/vmihailenco/msgpack/v5"
)
/*
PluginResponse returns plugin "p" response to the message "msg".
The message is pointer to Go nu-protocol message structure, ie
PluginResponse(ctx, p, &Call{ID: 1, Call: Signature{}})
*/
func PluginResponse(ctx context.Context, p *Plugin, msg any) ([]byte, error) {
outBuf := &bytes.Buffer{}
p.out = outBuf
r, w := io.Pipe()
p.in = r
done := make(chan error, 1)
go func() {
defer close(done)
if err := p.mainMsgLoop(ctx); err != nil {
done <- fmt.Errorf("plugin loop exited with error: %w", err)
}
}()
if err := msgpack.NewEncoder(w).Encode(msg); err != nil {
done <- fmt.Errorf("encoding the message: %w", err)
}
w.Close()
var err error
for e := range done {
err = errors.Join(err, e)
}
return outBuf.Bytes(), err
}
/*
Parses all nu-plugin-protocol messages (both server and client).
*/
func decodeNuMsgAll(next func(d *msgpack.Decoder, name string) (_ interface{}, err error)) func(*msgpack.Decoder) (interface{}, error) {
return func(dec *msgpack.Decoder) (interface{}, error) {
name, err := decodeWrapperMap(dec)
if err != nil {
return nil, fmt.Errorf("decode message: %w", err)
}
switch name {
case "CallResponse":
cr := callResponse{}
return cr, dec.DecodeValue(reflect.ValueOf(&cr))
case "PipelineData":
cr := pipelineData{}
return cr, dec.DecodeValue(reflect.ValueOf(&cr))
default:
return next(dec, name)
}
}
}
func logger(t *testing.T) *slog.Logger {
return slogt.New(t)
}
func expectErrorMsg(t *testing.T, err error, msg string) {
t.Helper()
if err == nil {
t.Fatal("expected error, got none")
}
if diff := cmp.Diff(err.Error(), msg); diff != "" {
t.Errorf("error message mismatch (-want +got):\n%s", diff)
}
}