Skip to content

Commit

Permalink
[chore]: enable require-error rule from testifylint (#11199)
Browse files Browse the repository at this point in the history
#### Description

Testifylint is a linter that provides best practices with the use of
testify.

This PR enables
[require-error](https://github.com/Antonboom/testifylint?tab=readme-ov-file#require-error)
rule from [testifylint](https://github.com/Antonboom/testifylint)

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Sep 18, 2024
1 parent a0d5966 commit 37f7833
Show file tree
Hide file tree
Showing 160 changed files with 933 additions and 859 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ linters-settings:
- "!**/*_test.go"

testifylint:
# TODO: enable all rules
disable:
- require-error
enable-all: true

linters:
Expand Down
2 changes: 1 addition & 1 deletion Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SEMCONVGEN := $(TOOLS_BIN_DIR)/semconvgen
SEMCONVKIT := $(TOOLS_BIN_DIR)/semconvkit
TESTIFYLINT := $(TOOLS_BIN_DIR)/testifylint

TESTIFYLINT_OPT?= --enable-all --disable=require-error
TESTIFYLINT_OPT?= --enable-all

.PHONY: install-tools
install-tools: $(TOOLS_BIN_NAMES)
Expand Down
10 changes: 5 additions & 5 deletions cmd/builder/internal/builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestParseModules(t *testing.T) {

// test
err := cfg.ParseModules()
assert.NoError(t, err)
require.NoError(t, err)

// verify
assert.Equal(t, "github.com/org/repo v0.1.2", cfg.Extensions[0].GoMod)
Expand All @@ -47,7 +47,7 @@ func TestRelativePath(t *testing.T) {

// test
err := cfg.ParseModules()
assert.NoError(t, err)
require.NoError(t, err)

// verify
cwd, err := os.Getwd()
Expand All @@ -72,7 +72,7 @@ func TestModuleFromCore(t *testing.T) {

// test
err := cfg.ParseModules()
assert.NoError(t, err)
require.NoError(t, err)

// verify
assert.True(t, strings.HasPrefix(cfg.Extensions[0].Name, "otlpreceiver"))
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestDebugOptionSetConfig(t *testing.T) {
SkipCompilation: true,
SkipGetModules: true,
}
assert.NoError(t, cfg.Validate())
require.NoError(t, cfg.Validate())
assert.True(t, cfg.Distribution.DebugCompilation)
}

Expand Down Expand Up @@ -314,7 +314,7 @@ func TestConfmapFactoryVersions(t *testing.T) {
func TestAddsDefaultProviders(t *testing.T) {
cfg := NewDefaultConfig()
cfg.Providers = nil
assert.NoError(t, cfg.ParseModules())
require.NoError(t, cfg.ParseModules())
assert.Len(t, *cfg.Providers, 5)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/mdatagen/internal/embeded_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEnsureTemplatesLoaded(t *testing.T) {
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestEnsureTemplatesLoaded(t *testing.T) {
}
count = 0
)
assert.NoError(t, fs.WalkDir(TemplateFS, ".", func(path string, d fs.DirEntry, _ error) error {
require.NoError(t, fs.WalkDir(TemplateFS, ".", func(path string, d fs.DirEntry, _ error) error {
if d != nil && d.IsDir() {
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions component/identifiable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMarshalText(t *testing.T) {
id := NewIDWithName(MustNewType("test"), "name")
got, err := id.MarshalText()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, id.String(), string(got))
}

Expand Down Expand Up @@ -92,7 +93,7 @@ func TestUnmarshalText(t *testing.T) {
return
}

assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tt.expectedID, id)
assert.Equal(t, tt.expectedID.Type(), id.Type())
assert.Equal(t, tt.expectedID.Name(), id.Name())
Expand Down
13 changes: 7 additions & 6 deletions config/configauth/configauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"
Expand Down Expand Up @@ -53,10 +54,10 @@ func TestGetServer(t *testing.T) {

// verify
if tt.expected != nil {
assert.ErrorIs(t, err, tt.expected)
require.ErrorIs(t, err, tt.expected)
assert.Nil(t, authenticator)
} else {
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, authenticator)
}
})
Expand All @@ -69,7 +70,7 @@ func TestGetServerFails(t *testing.T) {
}

authenticator, err := cfg.GetServerAuthenticator(context.Background(), map[component.ID]component.Component{})
assert.ErrorIs(t, err, errAuthenticatorNotFound)
require.ErrorIs(t, err, errAuthenticatorNotFound)
assert.Nil(t, authenticator)
}

Expand Down Expand Up @@ -104,10 +105,10 @@ func TestGetClient(t *testing.T) {

// verify
if tt.expected != nil {
assert.ErrorIs(t, err, tt.expected)
require.ErrorIs(t, err, tt.expected)
assert.Nil(t, authenticator)
} else {
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, authenticator)
}
})
Expand All @@ -119,6 +120,6 @@ func TestGetClientFails(t *testing.T) {
AuthenticatorID: component.MustNewID("does_not_exist"),
}
authenticator, err := cfg.GetClientAuthenticator(context.Background(), map[component.ID]component.Component{})
assert.ErrorIs(t, err, errAuthenticatorNotFound)
require.ErrorIs(t, err, errAuthenticatorNotFound)
assert.Nil(t, authenticator)
}
44 changes: 22 additions & 22 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestDefaultGrpcClientSettings(t *testing.T) {
},
}
opts, err := gcs.toDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, opts, 2)
}

Expand Down Expand Up @@ -232,7 +232,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts, err := test.settings.toDialOptions(context.Background(), test.host, tt.TelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, opts, 9)
})
}
Expand All @@ -245,7 +245,7 @@ func TestDefaultGrpcServerSettings(t *testing.T) {
},
}
opts, err := gss.toServerOption(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, opts, 3)
}

Expand Down Expand Up @@ -295,7 +295,7 @@ func TestGrpcServerValidate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.err, func(t *testing.T) {
err := tt.gss.Validate()
assert.Error(t, err)
require.Error(t, err)
assert.Regexp(t, tt.err, err)
})
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) {
},
}
opts, err := gss.toServerOption(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, opts, 10)
}

Expand All @@ -349,7 +349,7 @@ func TestGrpcServerAuthSettings(t *testing.T) {
},
}
srv, err := gss.ToServer(context.Background(), host, componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, srv)
}

Expand Down Expand Up @@ -470,7 +470,7 @@ func TestGRPCClientSettingsError(t *testing.T) {
for _, test := range tests {
t.Run(test.err, func(t *testing.T) {
_, err := test.settings.ToClientConn(context.Background(), test.host, tt.TelemetrySettings())
assert.Error(t, err)
require.Error(t, err)
assert.Regexp(t, test.err, err)
})
}
Expand All @@ -489,7 +489,7 @@ func TestUseSecure(t *testing.T) {
Keepalive: nil,
}
dialOpts, err := gcs.toDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, dialOpts, 2)
}

Expand Down Expand Up @@ -735,9 +735,9 @@ func TestHttpReception(t *testing.T) {
TLSSetting: test.tlsServerCreds,
}
ln, err := gss.NetAddr.Listen(context.Background())
assert.NoError(t, err)
require.NoError(t, err)
s, err := gss.ToServer(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
ptraceotlp.RegisterGRPCServer(s, &grpcTraceServer{})

go func() {
Expand All @@ -749,15 +749,15 @@ func TestHttpReception(t *testing.T) {
TLSSetting: *test.tlsClientCreds,
}
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
assert.NoError(t, errClient)
require.NoError(t, errClient)
defer func() { assert.NoError(t, grpcClientConn.Close()) }()
c := ptraceotlp.NewGRPCClient(grpcClientConn)
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
resp, errResp := c.Export(ctx, ptraceotlp.NewExportRequest(), grpc.WaitForReady(true))
if test.hasError {
assert.Error(t, errResp)
require.Error(t, errResp)
} else {
assert.NoError(t, errResp)
require.NoError(t, errResp)
assert.NotNil(t, resp)
}
cancelFunc()
Expand All @@ -782,9 +782,9 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
},
}
ln, err := gss.NetAddr.Listen(context.Background())
assert.NoError(t, err)
require.NoError(t, err)
srv, err := gss.ToServer(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)
require.NoError(t, err)
ptraceotlp.RegisterGRPCServer(srv, &grpcTraceServer{})

go func() {
Expand All @@ -798,12 +798,12 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
},
}
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
assert.NoError(t, errClient)
require.NoError(t, errClient)
defer func() { assert.NoError(t, grpcClientConn.Close()) }()
c := ptraceotlp.NewGRPCClient(grpcClientConn)
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
resp, errResp := c.Export(ctx, ptraceotlp.NewExportRequest(), grpc.WaitForReady(true))
assert.NoError(t, errResp)
require.NoError(t, errResp)
assert.NotNil(t, resp)
cancelFunc()
srv.Stop()
Expand Down Expand Up @@ -933,7 +933,7 @@ func TestStreamInterceptorEnhancesClient(t *testing.T) {
err := enhanceStreamWithClientInformation(false)(nil, stream, nil, handler)

// verify
assert.NoError(t, err)
require.NoError(t, err)

cl := client.FromContext(outContext)
assert.Equal(t, "1.1.1.1", cl.Addr.String())
Expand Down Expand Up @@ -1052,7 +1052,7 @@ func TestDefaultUnaryInterceptorAuthSucceeded(t *testing.T) {

// verify
assert.Nil(t, res)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, authCalled)
assert.True(t, handlerCalled)
}
Expand All @@ -1076,7 +1076,7 @@ func TestDefaultUnaryInterceptorAuthFailure(t *testing.T) {

// verify
assert.Nil(t, res)
assert.ErrorContains(t, err, expectedErr.Error())
require.ErrorContains(t, err, expectedErr.Error())
assert.Equal(t, codes.Unauthenticated, status.Code(err))
assert.True(t, authCalled)
}
Expand Down Expand Up @@ -1127,7 +1127,7 @@ func TestDefaultStreamInterceptorAuthSucceeded(t *testing.T) {
err := authStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, auth.NewServer(auth.WithServerAuthenticate(authFunc)))

// verify
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, authCalled)
assert.True(t, handlerCalled)
}
Expand All @@ -1153,7 +1153,7 @@ func TestDefaultStreamInterceptorAuthFailure(t *testing.T) {
err := authStreamServerInterceptor(nil, streamServer, &grpc.StreamServerInfo{}, handler, auth.NewServer(auth.WithServerAuthenticate(authFunc)))

// verify
assert.ErrorContains(t, err, expectedErr.Error()) // unfortunately, grpc errors don't wrap the original ones
require.ErrorContains(t, err, expectedErr.Error()) // unfortunately, grpc errors don't wrap the original ones
assert.Equal(t, codes.Unauthenticated, status.Code(err))
assert.True(t, authCalled)
}
Expand Down
16 changes: 8 additions & 8 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
assert.Error(t, err)
return
}
assert.NoError(t, err)
require.NoError(t, err)
switch transport := client.Transport.(type) {
case *http.Transport:
assert.EqualValues(t, 1024, transport.ReadBufferSize)
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestPartialHTTPClientSettings(t *testing.T) {
tel := componenttest.NewNopTelemetrySettings()
tel.TracerProvider = nil
client, err := tt.settings.ToClient(context.Background(), host, tel)
assert.NoError(t, err)
require.NoError(t, err)
transport := client.Transport.(*http.Transport)
assert.EqualValues(t, 1024, transport.ReadBufferSize)
assert.EqualValues(t, 512, transport.WriteBufferSize)
Expand Down Expand Up @@ -446,7 +446,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
assert.Error(t, err)
return
}
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, client)
transport := client.Transport

Expand Down Expand Up @@ -734,11 +734,11 @@ func TestHttpReception(t *testing.T) {

resp, errResp := client.Get(hcs.Endpoint)
if tt.hasError {
assert.Error(t, errResp)
require.Error(t, errResp)
} else {
assert.NoError(t, errResp)
require.NoError(t, errResp)
body, errRead := io.ReadAll(resp.Body)
assert.NoError(t, errRead)
require.NoError(t, errRead)
assert.Equal(t, "tt", string(body))
assert.Equal(t, expectedProto, resp.Proto)
}
Expand Down Expand Up @@ -1065,7 +1065,7 @@ func TestHttpClientHeaders(t *testing.T) {
}
client, _ := setting.ToClient(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
req, err := http.NewRequest(http.MethodGet, setting.Endpoint, nil)
assert.NoError(t, err)
require.NoError(t, err)
_, err = client.Do(req)
assert.NoError(t, err)
})
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func TestHttpClientHostHeader(t *testing.T) {
}
client, _ := setting.ToClient(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
req, err := http.NewRequest(http.MethodGet, setting.Endpoint, nil)
assert.NoError(t, err)
require.NoError(t, err)
_, err = client.Do(req)
assert.NoError(t, err)
})
Expand Down
Loading

0 comments on commit 37f7833

Please sign in to comment.