Skip to content

Commit

Permalink
[chore]: enable partially thelper linter
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Dec 16, 2024
1 parent 6a47030 commit bfa598c
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 66 deletions.
13 changes: 12 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,17 @@ linters-settings:
- "!**/*_test.go"

testifylint:
enable-all: true
enable-all: true

thelper:
test:
begin: false
benchmark:
begin: false
tb:
begin: false
fuzz:
begin: false

linters:
enable:
Expand All @@ -153,6 +163,7 @@ linters:
- staticcheck
- tenv
- testifylint
- thelper
- unconvert
- unused
- unparam
Expand Down
4 changes: 2 additions & 2 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ var replaceModules = []string{
"/service",
}

func newTestConfig(t testing.TB) *Config {
func newTestConfig(tb testing.TB) *Config {
cfg, err := NewDefaultConfig()
require.NoError(t, err)
require.NoError(tb, err)
cfg.downloadModules.wait = 0
cfg.downloadModules.numRetries = 1
return cfg
Expand Down
24 changes: 12 additions & 12 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,39 +446,39 @@ func TestDecompressorAvoidDecompressionBomb(t *testing.T) {
}
}

func compressGzip(t testing.TB, body []byte) *bytes.Buffer {
func compressGzip(tb testing.TB, body []byte) *bytes.Buffer {
var buf bytes.Buffer
gw := gzip.NewWriter(&buf)
_, err := gw.Write(body)
require.NoError(t, err)
require.NoError(t, gw.Close())
require.NoError(tb, err)
require.NoError(tb, gw.Close())
return &buf
}

func compressZlib(t testing.TB, body []byte) *bytes.Buffer {
func compressZlib(tb testing.TB, body []byte) *bytes.Buffer {
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
_, err := zw.Write(body)
require.NoError(t, err)
require.NoError(t, zw.Close())
require.NoError(tb, err)
require.NoError(tb, zw.Close())
return &buf
}

func compressSnappy(t testing.TB, body []byte) *bytes.Buffer {
func compressSnappy(tb testing.TB, body []byte) *bytes.Buffer {
var buf bytes.Buffer
sw := snappy.NewBufferedWriter(&buf)
_, err := sw.Write(body)
require.NoError(t, err)
require.NoError(t, sw.Close())
require.NoError(tb, err)
require.NoError(tb, sw.Close())
return &buf
}

func compressZstd(t testing.TB, body []byte) *bytes.Buffer {
func compressZstd(tb testing.TB, body []byte) *bytes.Buffer {
var buf bytes.Buffer
zw, _ := zstd.NewWriter(&buf)
_, err := zw.Write(body)
require.NoError(t, err)
require.NoError(t, zw.Close())
require.NoError(tb, err)
require.NoError(tb, zw.Close())
return &buf
}

Expand Down
6 changes: 3 additions & 3 deletions confmap/confmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,12 @@ func TestMarshaler(t *testing.T) {
}

// newConfFromFile creates a new Conf by reading the given file.
func newConfFromFile(t testing.TB, fileName string) map[string]any {
func newConfFromFile(tb testing.TB, fileName string) map[string]any {
content, err := os.ReadFile(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to read the file %v", fileName)
require.NoErrorf(tb, err, "unable to read the file %v", fileName)

var data map[string]any
require.NoError(t, yaml.Unmarshal(content, &data), "unable to parse yaml")
require.NoError(tb, yaml.Unmarshal(content, &data), "unable to parse yaml")

return NewFromStringMap(data).ToStringMap()
}
Expand Down
4 changes: 2 additions & 2 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type TargetConfig[T any] struct {
Field T `mapstructure:"field"`
}

func NewResolver(t testing.TB, path string) *confmap.Resolver {
func NewResolver(tb testing.TB, path string) *confmap.Resolver {
resolver, err := confmap.NewResolver(confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", path)},
ProviderFactories: []confmap.ProviderFactory{
Expand All @@ -48,7 +48,7 @@ func NewResolver(t testing.TB, path string) *confmap.Resolver {
},
DefaultScheme: "env",
})
require.NoError(t, err)
require.NoError(tb, err)
return resolver
}

Expand Down
8 changes: 4 additions & 4 deletions confmap/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ type fakeProvider struct {
logger *zap.Logger
}

func newFileProvider(t testing.TB) ProviderFactory {
func newFileProvider(tb testing.TB) ProviderFactory {
return newFakeProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) {
return NewRetrieved(newConfFromFile(t, uri[5:]))
return NewRetrieved(newConfFromFile(tb, uri[5:]))
})
}

Expand All @@ -76,9 +76,9 @@ func newFakeProvider(scheme string, ret func(ctx context.Context, uri string, wa
})
}

func newObservableFileProvider(t testing.TB) (ProviderFactory, *fakeProvider) {
func newObservableFileProvider(tb testing.TB) (ProviderFactory, *fakeProvider) {
return newObservableProvider("file", func(_ context.Context, uri string, _ WatcherFunc) (*Retrieved, error) {
return NewRetrieved(newConfFromFile(t, uri[5:]))
return NewRetrieved(newConfFromFile(tb, uri[5:]))
})
}

Expand Down
6 changes: 3 additions & 3 deletions exporter/exporterhelper/internal/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"go.opentelemetry.io/collector/featuregate"
)

func setFeatureGateForTest(t testing.TB, gate *featuregate.Gate, enabled bool) func() {
func setFeatureGateForTest(tb testing.TB, gate *featuregate.Gate, enabled bool) func() {
originalValue := gate.IsEnabled()
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), enabled))
require.NoError(tb, featuregate.GlobalRegistry().Set(gate.ID(), enabled))
return func() {
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), originalValue))
require.NoError(tb, featuregate.GlobalRegistry().Set(gate.ID(), originalValue))
}
}
12 changes: 6 additions & 6 deletions exporter/internal/queue/persistent_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@ func createTestPersistentQueueWithClient(client storage.Client) *persistentQueue
return pq
}

func createTestPersistentQueueWithRequestsCapacity(t testing.TB, ext storage.Extension, capacity int64) *persistentQueue[tracesRequest] {
return createTestPersistentQueueWithCapacityLimiter(t, ext, &RequestSizer[tracesRequest]{}, capacity)
func createTestPersistentQueueWithRequestsCapacity(tb testing.TB, ext storage.Extension, capacity int64) *persistentQueue[tracesRequest] {
return createTestPersistentQueueWithCapacityLimiter(tb, ext, &RequestSizer[tracesRequest]{}, capacity)
}

func createTestPersistentQueueWithItemsCapacity(t testing.TB, ext storage.Extension, capacity int64) *persistentQueue[tracesRequest] {
return createTestPersistentQueueWithCapacityLimiter(t, ext, &itemsSizer[tracesRequest]{}, capacity)
func createTestPersistentQueueWithItemsCapacity(tb testing.TB, ext storage.Extension, capacity int64) *persistentQueue[tracesRequest] {
return createTestPersistentQueueWithCapacityLimiter(tb, ext, &itemsSizer[tracesRequest]{}, capacity)
}

func createTestPersistentQueueWithCapacityLimiter(t testing.TB, ext storage.Extension, sizer Sizer[tracesRequest],
func createTestPersistentQueueWithCapacityLimiter(tb testing.TB, ext storage.Extension, sizer Sizer[tracesRequest],
capacity int64,
) *persistentQueue[tracesRequest] {
pq := NewPersistentQueue[tracesRequest](PersistentQueueSettings[tracesRequest]{
Expand All @@ -284,7 +284,7 @@ func createTestPersistentQueueWithCapacityLimiter(t testing.TB, ext storage.Exte
Unmarshaler: unmarshalTracesRequest,
ExporterSettings: exportertest.NewNopSettings(),
}).(*persistentQueue[tracesRequest])
require.NoError(t, pq.Start(context.Background(), &mockHost{ext: map[component.ID]component.Component{{}: ext}}))
require.NoError(tb, pq.Start(context.Background(), &mockHost{ext: map[component.ID]component.Component{{}: ext}}))
return pq
}

Expand Down
44 changes: 22 additions & 22 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ type portpair struct {
// describing it. The port is available for opening when this function returns
// provided that there is no race by some other code to grab the same port
// immediately.
func GetAvailableLocalAddress(t testing.TB) string {
return findAvailable(t, "tcp4")
func GetAvailableLocalAddress(tb testing.TB) string {
return findAvailable(tb, "tcp4")
}

// GetAvailableLocalIPv6Address is IPv6 version of GetAvailableLocalAddress.
func GetAvailableLocalIPv6Address(t testing.TB) string {
return findAvailable(t, "tcp6")
func GetAvailableLocalIPv6Address(tb testing.TB) string {
return findAvailable(tb, "tcp6")
}

func findAvailable(t testing.TB, network string) string {
func findAvailable(tb testing.TB, network string) string {
// Retry has been added for windows as net.Listen can return a port that is not actually available. Details can be
// found in https://github.com/docker/for-win/issues/3171 but to summarize Hyper-V will reserve ranges of ports
// which do not show up under the "netstat -ano" but can only be found by
Expand All @@ -41,14 +41,14 @@ func findAvailable(t testing.TB, network string) string {
var exclusions []portpair
portFound := false
if runtime.GOOS == "windows" {
exclusions = getExclusionsList(network, t)
exclusions = getExclusionsList(tb, network)

Check warning on line 44 in internal/testutil/testutil.go

View check run for this annotation

Codecov / codecov/patch

internal/testutil/testutil.go#L44

Added line #L44 was not covered by tests
}

var endpoint string
for !portFound {
endpoint = findAvailableAddress(network, t)
endpoint = findAvailableAddress(tb, network)
_, port, err := net.SplitHostPort(endpoint)
require.NoError(t, err)
require.NoError(tb, err)
portFound = true
if runtime.GOOS == "windows" {
for _, pair := range exclusions {
Expand All @@ -63,61 +63,61 @@ func findAvailable(t testing.TB, network string) string {
return endpoint
}

func findAvailableAddress(network string, t testing.TB) string {
func findAvailableAddress(tb testing.TB, network string) string {
var host string
switch network {
case "tcp", "tcp4":
host = "localhost"
case "tcp6":
host = "[::1]"
}
require.NotZero(t, host, "network must be either of tcp, tcp4 or tcp6")
require.NotZero(tb, host, "network must be either of tcp, tcp4 or tcp6")

ln, err := net.Listen("tcp", host+":0")
require.NoError(t, err, "Failed to get a free local port")
require.NoError(tb, err, "Failed to get a free local port")
// There is a possible race if something else takes this same port before
// the test uses it, however, that is unlikely in practice.
defer func() {
assert.NoError(t, ln.Close())
assert.NoError(tb, ln.Close())
}()
return ln.Addr().String()
}

// Get excluded ports on Windows from the command: netsh interface ipv4 show excludedportrange protocol=tcp
func getExclusionsList(network string, t testing.TB) []portpair {
func getExclusionsList(tb testing.TB, network string) []portpair {

Check warning on line 87 in internal/testutil/testutil.go

View check run for this annotation

Codecov / codecov/patch

internal/testutil/testutil.go#L87

Added line #L87 was not covered by tests
var cmdTCP *exec.Cmd
switch network {
case "tcp", "tcp4":
cmdTCP = exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=tcp")
case "tcp6":
cmdTCP = exec.Command("netsh", "interface", "ipv6", "show", "excludedportrange", "protocol=tcp")
}
require.NotZero(t, cmdTCP, "network must be either of tcp, tcp4 or tcp6")
require.NotZero(tb, cmdTCP, "network must be either of tcp, tcp4 or tcp6")

Check warning on line 95 in internal/testutil/testutil.go

View check run for this annotation

Codecov / codecov/patch

internal/testutil/testutil.go#L95

Added line #L95 was not covered by tests

outputTCP, errTCP := cmdTCP.CombinedOutput()
require.NoError(t, errTCP)
exclusions := createExclusionsList(string(outputTCP), t)
require.NoError(tb, errTCP)
exclusions := createExclusionsList(tb, string(outputTCP))

Check warning on line 99 in internal/testutil/testutil.go

View check run for this annotation

Codecov / codecov/patch

internal/testutil/testutil.go#L98-L99

Added lines #L98 - L99 were not covered by tests

cmdUDP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=udp")
outputUDP, errUDP := cmdUDP.CombinedOutput()
require.NoError(t, errUDP)
exclusions = append(exclusions, createExclusionsList(string(outputUDP), t)...)
require.NoError(tb, errUDP)
exclusions = append(exclusions, createExclusionsList(tb, string(outputUDP))...)

Check warning on line 104 in internal/testutil/testutil.go

View check run for this annotation

Codecov / codecov/patch

internal/testutil/testutil.go#L103-L104

Added lines #L103 - L104 were not covered by tests

return exclusions
}

func createExclusionsList(exclusionsText string, t testing.TB) []portpair {
func createExclusionsList(tb testing.TB, exclusionsText string) []portpair {
var exclusions []portpair

parts := strings.Split(exclusionsText, "--------")
require.Len(t, parts, 3)
require.Len(tb, parts, 3)
portsText := strings.Split(parts[2], "*")
require.Greater(t, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions."
require.Greater(tb, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions."
lines := strings.Split(portsText[0], "\n")
for _, line := range lines {
if strings.TrimSpace(line) != "" {
entries := strings.Fields(strings.TrimSpace(line))
require.Len(t, entries, 2)
require.Len(tb, entries, 2)
pair := portpair{entries[0], entries[1]}
exclusions = append(exclusions, pair)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ Start Port End Port
* - Administered port exclusions.
`
exclusions := createExclusionsList(exclusionsText, t)
exclusions := createExclusionsList(t, exclusionsText)
require.Len(t, exclusions, 2)

emptyExclusions := createExclusionsList(emptyExclusionsText, t)
emptyExclusions := createExclusionsList(t, emptyExclusionsText)
require.Empty(t, emptyExclusions)
}
10 changes: 5 additions & 5 deletions otelcol/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ func newEnvProvider() confmap.ProviderFactory {
})
}

func newDefaultConfigProviderSettings(t testing.TB, uris []string) ConfigProviderSettings {
func newDefaultConfigProviderSettings(tb testing.TB, uris []string) ConfigProviderSettings {
fileProvider := newFakeProvider("file", func(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
return confmap.NewRetrieved(newConfFromFile(t, uri[5:]))
return confmap.NewRetrieved(newConfFromFile(tb, uri[5:]))
})
return ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
Expand All @@ -585,12 +585,12 @@ func newDefaultConfigProviderSettings(t testing.TB, uris []string) ConfigProvide
}

// newConfFromFile creates a new Conf by reading the given file.
func newConfFromFile(t testing.TB, fileName string) map[string]any {
func newConfFromFile(tb testing.TB, fileName string) map[string]any {
content, err := os.ReadFile(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to read the file %v", fileName)
require.NoErrorf(tb, err, "unable to read the file %v", fileName)

var data map[string]any
require.NoError(t, yaml.Unmarshal(content, &data), "unable to parse yaml")
require.NoError(tb, yaml.Unmarshal(content, &data), "unable to parse yaml")

return confmap.NewFromStringMap(data).ToStringMap()
}
8 changes: 4 additions & 4 deletions service/internal/promtest/server_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"go.opentelemetry.io/collector/internal/testutil"
)

func GetAvailableLocalIPv6AddressPrometheus(t testing.TB) *config.Prometheus {
return addrToPrometheus(testutil.GetAvailableLocalIPv6Address(t))
func GetAvailableLocalIPv6AddressPrometheus(tb testing.TB) *config.Prometheus {
return addrToPrometheus(testutil.GetAvailableLocalIPv6Address(tb))
}

func GetAvailableLocalAddressPrometheus(t testing.TB) *config.Prometheus {
return addrToPrometheus(testutil.GetAvailableLocalAddress(t))
func GetAvailableLocalAddressPrometheus(tb testing.TB) *config.Prometheus {
return addrToPrometheus(testutil.GetAvailableLocalAddress(tb))
}

func addrToPrometheus(address string) *config.Prometheus {
Expand Down

0 comments on commit bfa598c

Please sign in to comment.