Skip to content

Commit 2bcb098

Browse files
authored
Merge pull request #3566 from jsternberg/dap-build-integration-tests
tests: add integration tests for `dap build`
2 parents e794584 + fdfba30 commit 2bcb098

File tree

10 files changed

+1151
-72
lines changed

10 files changed

+1151
-72
lines changed

commands/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, debugOpts debuggerOpti
400400
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
401401
}
402402
if options.imageIDFile != "" {
403-
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse)), 0644); err != nil {
403+
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse)), 0o644); err != nil {
404404
return errors.Wrap(err, "writing image ID file")
405405
}
406406
}
@@ -655,7 +655,7 @@ func writeMetadataFile(filename string, dt any) error {
655655
if err != nil {
656656
return err
657657
}
658-
return atomicwriter.WriteFile(filename, b, 0644)
658+
return atomicwriter.WriteFile(filename, b, 0o644)
659659
}
660660

661661
func decodeExporterResponse(exporterResponse map[string]string) map[string]any {

dap/adapter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,9 @@ func newBreakpointMap() *breakpointMap {
568568
func (b *breakpointMap) Set(fname string, sbps []dap.SourceBreakpoint) (breakpoints []dap.Breakpoint) {
569569
b.mu.Lock()
570570
defer b.mu.Unlock()
571-
// explicitly initialize breakpoints so that
572-
// we do not send a null back in the JSON if there are no breakpoints
571+
572+
// Explicitly initialize breakpoints so that we do not send a
573+
// null back in the JSON if there are no breakpoints
573574
breakpoints = []dap.Breakpoint{}
574575

575576
prev := b.byPath[fname]

dap/adapter_test.go

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package dap
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"io"
87
"path/filepath"
98
"testing"
109
"time"
1110

1211
"github.com/docker/buildx/dap/common"
12+
"github.com/docker/buildx/util/daptest"
1313
"github.com/google/go-dap"
1414
"github.com/moby/buildkit/solver/pb"
1515
"github.com/stretchr/testify/assert"
@@ -36,20 +36,20 @@ func TestLaunch(t *testing.T) {
3636

3737
client.RegisterEvent("initialized", func(em dap.EventMessage) {
3838
// Send configuration done since we don't do any configuration.
39-
configurationDone = DoRequest[*dap.ConfigurationDoneResponse](t, client, &dap.ConfigurationDoneRequest{
39+
configurationDone = daptest.DoRequest[*dap.ConfigurationDoneResponse](t, client, &dap.ConfigurationDoneRequest{
4040
Request: dap.Request{Command: "configurationDone"},
4141
})
4242
close(initialized)
4343
})
4444

4545
eg.Go(func() error {
46-
initializeResp := <-DoRequest[*dap.InitializeResponse](t, client, &dap.InitializeRequest{
46+
initializeResp := <-daptest.DoRequest[*dap.InitializeResponse](t, client, &dap.InitializeRequest{
4747
Request: dap.Request{Command: "initialize"},
4848
})
4949
assert.True(t, initializeResp.Success)
5050
assert.True(t, initializeResp.Body.SupportsConfigurationDoneRequest)
5151

52-
launchResp := <-DoRequest[*dap.LaunchResponse](t, client, &dap.LaunchRequest{
52+
launchResp := <-daptest.DoRequest[*dap.LaunchResponse](t, client, &dap.LaunchRequest{
5353
Request: dap.Request{Command: "launch"},
5454
})
5555
assert.True(t, launchResp.Success)
@@ -93,7 +93,7 @@ func TestSetBreakpoints(t *testing.T) {
9393
)
9494

9595
client.RegisterEvent("initialized", func(em dap.EventMessage) {
96-
setBreakpoints = DoRequest[*dap.SetBreakpointsResponse](t, client, &dap.SetBreakpointsRequest{
96+
setBreakpoints = daptest.DoRequest[*dap.SetBreakpointsResponse](t, client, &dap.SetBreakpointsRequest{
9797
Request: dap.Request{Command: "setBreakpoints"},
9898
Arguments: dap.SetBreakpointsArguments{
9999
Source: dap.Source{Name: "Dockerfile", Path: filepath.Join(t.TempDir(), "Dockerfile")},
@@ -104,13 +104,13 @@ func TestSetBreakpoints(t *testing.T) {
104104
})
105105

106106
eg.Go(func() error {
107-
initializeResp := <-DoRequest[*dap.InitializeResponse](t, client, &dap.InitializeRequest{
107+
initializeResp := <-daptest.DoRequest[*dap.InitializeResponse](t, client, &dap.InitializeRequest{
108108
Request: dap.Request{Command: "initialize"},
109109
})
110110
assert.True(t, initializeResp.Success)
111111
assert.True(t, initializeResp.Body.SupportsConfigurationDoneRequest)
112112

113-
launchResp := <-DoRequest[*dap.LaunchResponse](t, client, &dap.LaunchRequest{
113+
launchResp := <-daptest.DoRequest[*dap.LaunchResponse](t, client, &dap.LaunchRequest{
114114
Request: dap.Request{Command: "launch"},
115115
})
116116
assert.True(t, launchResp.Success)
@@ -234,66 +234,29 @@ func TestBreakpointMapIntersectVerified(t *testing.T) {
234234
}
235235
}
236236

237-
func NewTestAdapter[C LaunchConfig](t *testing.T) (*Adapter[C], Conn, *Client) {
237+
func NewTestAdapter[C LaunchConfig](t *testing.T) (*Adapter[C], Conn, *daptest.Client) {
238238
t.Helper()
239239

240240
rd1, wr1 := io.Pipe()
241241
rd2, wr2 := io.Pipe()
242242

243-
srvConn := logConn(t, "server", NewConn(rd1, wr2))
243+
srvConn := daptest.LogConn(t, "server", NewConn(rd1, wr2))
244244
t.Cleanup(func() {
245245
srvConn.Close()
246246
})
247247

248-
clientConn := logConn(t, "client", NewConn(rd2, wr1))
248+
clientConn := daptest.LogConn(t, "client", NewConn(rd2, wr1))
249249
t.Cleanup(func() { clientConn.Close() })
250250

251251
adapter := New[C]()
252252
t.Cleanup(func() { adapter.Stop() })
253253

254-
client := NewClient(clientConn)
254+
client := daptest.NewClient(clientConn)
255255
t.Cleanup(func() { client.Close() })
256256

257257
return adapter, srvConn, client
258258
}
259259

260-
func logConn(t *testing.T, prefix string, conn Conn) Conn {
261-
return &loggingConn{
262-
Conn: conn,
263-
t: t,
264-
prefix: prefix,
265-
}
266-
}
267-
268-
type loggingConn struct {
269-
Conn
270-
t *testing.T
271-
prefix string
272-
}
273-
274-
func (c *loggingConn) SendMsg(m dap.Message) error {
275-
b, _ := json.Marshal(m)
276-
c.t.Logf("[%s] send: %v", c.prefix, string(b))
277-
278-
err := c.Conn.SendMsg(m)
279-
if err != nil {
280-
c.t.Logf("[%s] send error: %v", c.prefix, err)
281-
}
282-
return err
283-
}
284-
285-
func (c *loggingConn) RecvMsg(ctx context.Context) (dap.Message, error) {
286-
m, err := c.Conn.RecvMsg(ctx)
287-
if err != nil {
288-
c.t.Logf("[%s] recv error: %v", c.prefix, err)
289-
return nil, err
290-
}
291-
292-
b, _ := json.Marshal(m)
293-
c.t.Logf("[%s] recv: %v", c.prefix, string(b))
294-
return m, nil
295-
}
296-
297260
type breakpointTestContext struct {
298261
context.Context
299262
messages chan dap.Message

dap/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package dap

dap/common/types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"io"
6+
7+
"github.com/google/go-dap"
8+
)
9+
10+
type Conn interface {
11+
SendMsg(m dap.Message) error
12+
RecvMsg(ctx context.Context) (dap.Message, error)
13+
io.Closer
14+
}

dap/conn.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import (
66
"io"
77
"sync"
88

9+
"github.com/docker/buildx/dap/common"
910
"github.com/google/go-dap"
1011
"github.com/pkg/errors"
1112
"golang.org/x/sync/errgroup"
1213
)
1314

14-
type Conn interface {
15-
SendMsg(m dap.Message) error
16-
RecvMsg(ctx context.Context) (dap.Message, error)
17-
io.Closer
18-
}
15+
type Conn = common.Conn
1916

2017
type conn struct {
2118
recvCh <-chan dap.Message

0 commit comments

Comments
 (0)