Skip to content
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

Linter: usetesting - Should we enable it? #16412

Open
zak-pawel opened this issue Jan 19, 2025 · 1 comment
Open

Linter: usetesting - Should we enable it? #16412

zak-pawel opened this issue Jan 19, 2025 · 1 comment
Assignees
Labels

Comments

@zak-pawel
Copy link
Collaborator

Description

This issue starts a discussion about enabling:

  • linter: usetesting - Detects when some calls can be replaced by methods from the testing package.

Example configuration

  usetesting:
    # Enable/disable `os.CreateTemp("", ...)` detections.
    # Default: true
    os-create-temp: false
    # Enable/disable `os.MkdirTemp()` detections.
    # Default: true
    os-mkdir-temp: false
    # Enable/disable `os.Setenv()` detections.
    # Default: false
    os-setenv: true
    # Enable/disable `os.TempDir()` detections.
    # Default: false
    os-temp-dir: true
    # Enable/disable `os.Chdir()` detections.
    # Disabled if Go < 1.24.
    # Default: true
    os-chdir: false
    # Enable/disable `context.Background()` detections.
    # Disabled if Go < 1.24.
    # Default: true
    context-background: false
    # Enable/disable `context.TODO()` detections.
    # Disabled if Go < 1.24.
    # Default: true
    context-todo: false

Examples

os.MkdirTemp

func TestExample(t *testing.T) {
	os.MkdirTemp("a", "b")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.TempDir()
    // ...
}

os.TempDir

func TestExample(t *testing.T) {
	os.TempDir()
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.TempDir()
    // ...
}

os.CreateTemp

func TestExample(t *testing.T) {
	os.CreateTemp("", "x")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
    os.CreateTemp(t.TempDir(), "x")
    // ...
}

os.Setenv

func TestExample(t *testing.T) {
	os.Setenv("A", "b")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.Setenv("A", "b")
    // ...
}

os.Chdir (Go >= 1.24)

func TestExample(t *testing.T) {
	os.Chdir("x")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.Chdir("x")
    // ...
}

context.Background (Go >= 1.24)

func TestExample(t *testing.T) {
    ctx := context.Background()
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
    ctx := t.Context()
    // ...
}

context.TODO (Go >= 1.24)

func TestExample(t *testing.T) {
    ctx := context.TODO()
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
    ctx := t.Context()
    // ...
}

Expected output

Decision about enabling or not enabling this rule.

Findings

For this rule (with default configuration), the following findings were found in the current codebase:

config/config_test.go:1093:15                                    usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestPersisterInputStoreLoad
logger/structured_logger_test.go:30:18                           usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredFile
logger/structured_logger_test.go:62:18                           usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredFileDebug
logger/structured_logger_test.go:94:18                           usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredFileError
logger/structured_logger_test.go:128:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredAddDefaultLogLevel
logger/structured_logger_test.go:162:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredDerivedLogger
logger/structured_logger_test.go:199:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredDerivedLoggerWithAttributes
logger/structured_logger_test.go:239:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredWriteToTruncatedFile
logger/structured_logger_test.go:313:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestStructuredLogMessageKey
logger/text_logger_test.go:28:18                                 usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextFile
logger/text_logger_test.go:49:18                                 usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextFileDebug
logger/text_logger_test.go:70:18                                 usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextFileError
logger/text_logger_test.go:92:18                                 usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextAddDefaultLogLevel
logger/text_logger_test.go:113:18                                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextWriteToTruncatedFile
logger/text_logger_test.go:165:18                                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextWriteDerivedLogger
logger/text_logger_test.go:189:18                                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTextWriteDerivedLoggerWithAttributes
plugins/inputs/bcache/bcache_test.go:27:17                       usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestBcacheGeneratesMetrics
plugins/inputs/conntrack/conntrack_test.go:39:17                 usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestDefaultsUsed
plugins/inputs/conntrack/conntrack_test.go:64:17                 usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestConfigsUsed
plugins/inputs/dpdk/dpdk_test.go:855:18                          usetesting  os.MkdirTemp() could be replaced by t.TempDir() in createSocketForTest
plugins/inputs/http_listener_v2/http_listener_v2_test.go:739:15  usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestUnixSocket
plugins/inputs/intel_baseband/intel_baseband_test.go:132:18      usetesting  os.MkdirTemp() could be replaced by t.TempDir() in newTempSocket
plugins/inputs/intel_baseband/intel_baseband_test.go:163:15      usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in newTempLogFile
plugins/inputs/intel_dlb/intel_dlb_test.go:882:18                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in Test_rasReader
plugins/inputs/kernel_vmstat/kernel_vmstat_test.go:301:18        usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in makeFakeVMStatFile
plugins/inputs/logparser/logparser_test.go:121:19                usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestGrokParseLogFilesAppearLater
plugins/inputs/lustre2/lustre2_test.go:175:17                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestLustre2GeneratesHealth
plugins/inputs/lustre2/lustre2_test.go:205:17                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestLustre2GeneratesMetrics
plugins/inputs/lustre2/lustre2_test.go:276:17                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestLustre2GeneratesClientMetrics
plugins/inputs/lustre2/lustre2_test.go:341:17                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestLustre2GeneratesJobstatsMetrics
plugins/inputs/lustre2/lustre2_test.go:502:17                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestLustre2GeneratesBrwstatsMetrics
plugins/inputs/lustre2/lustre2_test.go:577:18                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestLustre2GeneratesEvictionMetrics
plugins/inputs/socket_listener/socket_listener_test.go:133:18    usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestSocketListener
plugins/inputs/socket_listener/socket_listener_test.go:280:15    usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestLargeReadBufferUnixgram
plugins/inputs/tail/tail_test.go:197:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestGrokParseLogFilesWithMultilineTimeout
plugins/inputs/tail/tail_test.go:608:18                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTailEOF
plugins/inputs/tail/tail_test.go:648:16                          usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestCSVBehavior
plugins/inputs/x509_cert/x509_cert_test.go:42:18                 usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestGatherRemoteIntegration
plugins/inputs/x509_cert/x509_cert_test.go:167:14                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestGatherLocal
plugins/inputs/x509_cert/x509_cert_test.go:200:12                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestTags
plugins/inputs/x509_cert/x509_cert_test.go:249:12                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestGatherExcludeRootCerts
plugins/inputs/x509_cert/x509_cert_test.go:286:14                usetesting  os.CreateTemp("", ...) could be replaced by os.CreateTemp(t.TempDir(), ...) in TestGatherChain
plugins/inputs/x509_cert/x509_cert_test.go:490:17                usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestClassification
plugins/inputs/zfs/zfs_linux_test.go:223:17                      usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestZfsPoolMetrics
plugins/inputs/zfs/zfs_linux_test.go:282:17                      usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestZfsGeneratesMetrics
plugins/outputs/remotefile/remotefile_test.go:33:17              usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestStaticFileCreation
plugins/outputs/remotefile/remotefile_test.go:80:17              usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestStaticFileAppend
plugins/outputs/remotefile/remotefile_test.go:177:17             usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestDynamicFiles
plugins/outputs/remotefile/remotefile_test.go:245:17             usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestCustomTemplateFunctions
plugins/outputs/remotefile/remotefile_test.go:300:17             usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestCSVSerialization
plugins/outputs/remotefile/remotefile_test.go:360:17             usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestForgettingFiles
plugins/outputs/remotefile/remotefile_test.go:470:17             usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestTrackingMetrics
plugins/secretstores/jose/jose_test.go:60:18                     usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestSetListGet
plugins/secretstores/jose/jose_test.go:112:18                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestResolver
plugins/secretstores/jose/jose_test.go:140:18                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestResolverInvalid
plugins/secretstores/jose/jose_test.go:166:18                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestGetNonExistent
plugins/secretstores/jose/jose_test.go:189:18                    usetesting  os.MkdirTemp() could be replaced by t.TempDir() in TestGetInvalidPassword
@zak-pawel
Copy link
Collaborator Author

I would enable it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants