Skip to content

Commit

Permalink
tests: add missing calls to t.Helper()
Browse files Browse the repository at this point in the history
In a few places, we added calls t.Helper()
properly in test only funcs.

In the test safeBuf struct, we added support
for t.Helper() in all the receiver funcs
  • Loading branch information
jimlambrt committed Mar 6, 2024
1 parent bcbc309 commit 4508979
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 1 addition & 4 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ func TestMux_serve(t *testing.T) {
})
t.Run("default-route", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
buf := &safeBuf{
mu: &sync.Mutex{},
buf: &strings.Builder{},
}
buf := testSafeBuf(t)
testLogger := hclog.New(&hclog.LoggerOptions{
Name: "TestServer_Run-logger",
Level: hclog.Debug,
Expand Down
13 changes: 9 additions & 4 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func testAddRequestPacket(t *testing.T, m AddMessage) *packet {
}

func testRequestEnvelope(t *testing.T, messageID int) *ber.Packet {
t.Helper()
p := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
p.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, int64(messageID), "MessageID"))
return p
Expand Down Expand Up @@ -253,26 +254,30 @@ func TestEncodeString(t *testing.T, tag ber.Tag, s string, opt ...Option) string
}

type safeBuf struct {
t *testing.T
buf *strings.Builder
mu *sync.Mutex
mu *sync.RWMutex
}

func testSafeBuf(t *testing.T) *safeBuf {
t.Helper()
return &safeBuf{
mu: &sync.Mutex{},
t: t,
mu: &sync.RWMutex{},
buf: &strings.Builder{},
}
}

func (w *safeBuf) Write(p []byte) (n int, err error) {
w.t.Helper()
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Write(p)
}

func (w *safeBuf) String() string {
w.mu.Lock()
defer w.mu.Unlock()
w.t.Helper()
w.mu.RLock()
defer w.mu.RUnlock()
return w.buf.String()
}

0 comments on commit 4508979

Please sign in to comment.