Skip to content

[chore] use new linter usetesting #38834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ linters:
- reassign
- revive
- staticcheck
- tenv
- testifylint
- thelper
- unconvert
- unparam
- unused
- usestdlibvars
- usetesting
- wastedassign
- whitespace

Expand Down
11 changes: 4 additions & 7 deletions cmd/opampsupervisor/supervisor/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,15 @@ func TestCapabilities_SupportedCapabilities(t *testing.T) {
}

func TestLoad(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "*")
require.NoError(t, err)
tmpDir := t.TempDir()

t.Cleanup(func() {
require.NoError(t, os.Chmod(tmpDir, 0o700))
require.NoError(t, os.RemoveAll(tmpDir))
})

executablePath := filepath.Join(tmpDir, "binary")
err = os.WriteFile(executablePath, []byte{}, 0o600)
err := os.WriteFile(executablePath, []byte{}, 0o600)
require.NoError(t, err)

testCases := []struct {
Expand Down Expand Up @@ -704,10 +703,8 @@ agent:
func setupSupervisorConfigFile(t *testing.T, tmpDir, configString string) string {
t.Helper()

testDir, err := os.MkdirTemp(tmpDir, "*")
require.NoError(t, err)
cfgPath := filepath.Join(testDir, "config.yaml")
err = os.WriteFile(cfgPath, []byte(configString), 0o600)
cfgPath := filepath.Join(tmpDir, "config.yaml")
err := os.WriteFile(cfgPath, []byte(configString), 0o600)
require.NoError(t, err)
return cfgPath
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/opampsupervisor/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ telemetry:
func setupSupervisorConfig(t *testing.T, configuration string) config.Supervisor {
t.Helper()

tmpDir, err := os.MkdirTemp(os.TempDir(), "*")
require.NoError(t, err)
tmpDir := t.TempDir()

executablePath := filepath.Join(tmpDir, "binary")
err = os.WriteFile(executablePath, []byte{}, 0o600)
err := os.WriteFile(executablePath, []byte{}, 0o600)
require.NoError(t, err)
configuration = fmt.Sprintf(configuration, filepath.Join(tmpDir, "storage"), executablePath)

Expand Down Expand Up @@ -1513,8 +1512,7 @@ service:
}

func TestSupervisor_configStrictUnmarshal(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "*")
require.NoError(t, err)
tmpDir := t.TempDir()

configuration := `
server:
Expand All @@ -1528,7 +1526,7 @@ capabilities:
`

cfgPath := filepath.Join(tmpDir, "config.yaml")
err = os.WriteFile(cfgPath, []byte(configuration), 0o600)
err := os.WriteFile(cfgPath, []byte(configuration), 0o600)
require.NoError(t, err)

_, err = config.Load(cfgPath)
Expand Down
6 changes: 1 addition & 5 deletions confmap/provider/aesprovider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package aesprovider

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -95,11 +94,8 @@ func TestAESCredentialProvider(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Clearenv()
for k, v := range tt.envVars {
if err := os.Setenv(k, v); err != nil {
t.Fatalf("Failed to set env var %s: %v", k, err)
}
t.Setenv(k, v)
}

p := NewFactory().Create(confmap.ProviderSettings{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package azuremonitorexporter

import (
"os"
"strings"
"testing"

Expand Down Expand Up @@ -146,8 +145,7 @@ func TestParseConnectionString(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
os.Setenv(ApplicationInsightsConnectionString, tt.envValue)
defer os.Unsetenv(ApplicationInsightsConnectionString)
t.Setenv(ApplicationInsightsConnectionString, tt.envValue)
}

got, err := parseConnectionString(tt.config)
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestExamples(t *testing.T) {
require.NotEmpty(t, out.Data.YAML)

data := []byte(out.Data.YAML)
f, err := os.CreateTemp("", "ddexporter-yaml-test-")
f, err := os.CreateTemp(t.TempDir(), "ddexporter-yaml-test-")
require.NoError(t, err)
n, err := f.Write(data)
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func newRecreatableOtelCol(tb testing.TB) *recreatableOtelCol {
}
}

func (c *recreatableOtelCol) PrepareConfig(configStr string) (func(), error) {
func (c *recreatableOtelCol) PrepareConfig(_ *testing.T, configStr string) (func(), error) {
configCleanup := func() {
// NoOp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func runner(t *testing.T, eventType string, restartCollector, mockESFailure bool
cfg := createConfigYaml(t, sender, receiver, nil, nil, eventType, getDebugFlag(t))
t.Log("test otel collector configuration:", cfg)
collector := newRecreatableOtelCol(t)
cleanup, err := collector.PrepareConfig(cfg)
cleanup, err := collector.PrepareConfig(t, cfg)
require.NoError(t, err)
defer cleanup()

Expand Down
4 changes: 2 additions & 2 deletions extension/basicauthextension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var credentials = [][]string{

func TestBasicAuth_Valid(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", ".htpasswd")
f, err := os.CreateTemp(t.TempDir(), ".htpasswd")
require.NoError(t, err)
defer os.Remove(f.Name())

Expand Down Expand Up @@ -146,7 +146,7 @@ func TestBasicAuth_InvalidFormat(t *testing.T) {

func TestBasicAuth_HtpasswdInlinePrecedence(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", ".htpasswd")
f, err := os.CreateTemp(t.TempDir(), ".htpasswd")
require.NoError(t, err)
defer os.Remove(f.Name())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func TestDeepCopy(t *testing.T) {
}

func TestAutoUpdateStrategyWithFile(t *testing.T) {
tempFile, _ := os.CreateTemp("", "for_go_test_*.json")
tempFile, _ := os.CreateTemp(t.TempDir(), "for_go_test_*.json")
require.NoError(t, tempFile.Close())
defer func() {
require.NoError(t, os.Remove(tempFile.Name()))
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestAutoUpdateStrategyWithURL(t *testing.T) {
}

func TestAutoUpdateStrategyErrors(t *testing.T) {
tempFile, _ := os.CreateTemp("", "for_go_test_*.json")
tempFile, _ := os.CreateTemp(t.TempDir(), "for_go_test_*.json")
require.NoError(t, tempFile.Close())
defer func() {
_ = os.Remove(tempFile.Name())
Expand Down
2 changes: 1 addition & 1 deletion extension/pprofextension/pprofextension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestPerformanceProfilerShutdownWithoutStart(t *testing.T) {
}

func TestPerformanceProfilerLifecycleWithFile(t *testing.T) {
tmpFile, err := os.CreateTemp("", "pprof*.yaml")
tmpFile, err := os.CreateTemp(t.TempDir(), "pprof*.yaml")
require.NoError(t, err)
defer func() {
os.Remove(tmpFile.Name())
Expand Down
2 changes: 1 addition & 1 deletion extension/storage/filestorage/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestHandleProvidingFilePathAsDirWithAnError(t *testing.T) {
f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)

file, err := os.CreateTemp("", "")
file, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, file.Close())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import (
)

func TestCredentialsStoreLocalFs(t *testing.T) {
dir, err := os.MkdirTemp("", "otelcol-sumo-credentials-store-local-fs-test-*")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(dir)
})
dir := t.TempDir()

const key = "my_storage_key"

Expand Down
Loading