From 56613418137623926492e818412f7d2caa3bff60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Mon, 14 Aug 2023 23:18:20 +0200 Subject: [PATCH] chore: Enable filepathJoin checker for gocritic (#13758) Co-authored-by: Pawel Zak --- .golangci.yml | 1 + config/config_test.go | 2 +- internal/globpath/globpath_test.go | 2 ++ plugins/inputs/diskio/diskio_linux.go | 2 +- plugins/inputs/file/file_test.go | 8 ++++---- plugins/inputs/powerdns_recursor/powerdns_recursor.go | 3 +-- plugins/inputs/prometheus/kubernetes.go | 2 +- plugins/inputs/system/ps.go | 2 +- plugins/processors/port_name/services_path.go | 2 +- testutil/socket.go | 2 +- 10 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ab110bfd4..86320cceb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -95,6 +95,7 @@ linters-settings: - evalOrder - exitAfterDefer - externalErrorReassign + - filepathJoin - flagName - mapKey - nilValReturn diff --git a/config/config_test.go b/config/config_test.go index 0d989db20..a9befa4d2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -907,7 +907,7 @@ func TestConfig_MultipleProcessorsOrder(t *testing.T) { c := config.NewConfig() filenames := make([]string, 0, len(test.filename)) for _, fn := range test.filename { - filenames = append(filenames, filepath.Join("./testdata/processor_order", fn)) + filenames = append(filenames, filepath.Join(".", "testdata", "processor_order", fn)) } require.NoError(t, c.LoadAll(filenames...)) diff --git a/internal/globpath/globpath_test.go b/internal/globpath/globpath_test.go index 0ea9c7649..0cc4673c6 100644 --- a/internal/globpath/globpath_test.go +++ b/internal/globpath/globpath_test.go @@ -42,8 +42,10 @@ func TestCompileAndMatch(t *testing.T) { // test exclamation mark creates non-matching list without a range {path: filepath.Join(testdataDir, "log[!2]*"), matches: 2}, // test exclamation mark creates non-matching list without a range + //nolint:gocritic // filepathJoin - '\\' used to escape in glob, not path separator {path: filepath.Join(testdataDir, "log\\[!*"), matches: 1}, // test exclamation mark creates non-matching list without a range + //nolint:gocritic // filepathJoin - '\\' used to escape in glob, not path separator {path: filepath.Join(testdataDir, "log\\[^*"), matches: 0}, } diff --git a/plugins/inputs/diskio/diskio_linux.go b/plugins/inputs/diskio/diskio_linux.go index c8416c26e..5308e6062 100644 --- a/plugins/inputs/diskio/diskio_linux.go +++ b/plugins/inputs/diskio/diskio_linux.go @@ -132,7 +132,7 @@ func resolveName(name string) string { return name } // Try to prepend "/dev" - resolved, err = filepath.EvalSymlinks(filepath.Join("/dev", name)) + resolved, err = filepath.EvalSymlinks("/dev/" + name) if err != nil { return name } diff --git a/plugins/inputs/file/file_test.go b/plugins/inputs/file/file_test.go index 1dac3ea44..41a9b08e5 100644 --- a/plugins/inputs/file/file_test.go +++ b/plugins/inputs/file/file_test.go @@ -27,7 +27,7 @@ func TestRefreshFilePaths(t *testing.T) { require.NoError(t, err) r := File{ - Files: []string{filepath.Join(wd, "dev/testfiles/**.log")}, + Files: []string{filepath.Join(wd, "dev", "testfiles", "**.log")}, } err = r.Init() require.NoError(t, err) @@ -42,7 +42,7 @@ func TestFileTag(t *testing.T) { wd, err := os.Getwd() require.NoError(t, err) r := File{ - Files: []string{filepath.Join(wd, "dev/testfiles/json_a.log")}, + Files: []string{filepath.Join(wd, "dev", "testfiles", "json_a.log")}, FileTag: "filename", } require.NoError(t, r.Init()) @@ -67,7 +67,7 @@ func TestJSONParserCompile(t *testing.T) { var acc testutil.Accumulator wd, _ := os.Getwd() r := File{ - Files: []string{filepath.Join(wd, "dev/testfiles/json_a.log")}, + Files: []string{filepath.Join(wd, "dev", "testfiles", "json_a.log")}, } require.NoError(t, r.Init()) @@ -86,7 +86,7 @@ func TestGrokParser(t *testing.T) { wd, _ := os.Getwd() var acc testutil.Accumulator r := File{ - Files: []string{filepath.Join(wd, "dev/testfiles/grok_a.log")}, + Files: []string{filepath.Join(wd, "dev", "testfiles", "grok_a.log")}, } err := r.Init() require.NoError(t, err) diff --git a/plugins/inputs/powerdns_recursor/powerdns_recursor.go b/plugins/inputs/powerdns_recursor/powerdns_recursor.go index ecd3d20f5..48e83179a 100644 --- a/plugins/inputs/powerdns_recursor/powerdns_recursor.go +++ b/plugins/inputs/powerdns_recursor/powerdns_recursor.go @@ -4,7 +4,6 @@ package powerdns_recursor import ( _ "embed" "fmt" - "path/filepath" "strconv" "time" @@ -44,7 +43,7 @@ func (p *PowerdnsRecursor) Init() error { } if p.SocketDir == "" { - p.SocketDir = filepath.Join("/", "var", "run") + p.SocketDir = "/var/run" } switch p.ControlProtocolVersion { diff --git a/plugins/inputs/prometheus/kubernetes.go b/plugins/inputs/prometheus/kubernetes.go index 46ee97afc..866654d8a 100644 --- a/plugins/inputs/prometheus/kubernetes.go +++ b/plugins/inputs/prometheus/kubernetes.go @@ -63,7 +63,7 @@ func (p *Prometheus) startK8s(ctx context.Context) error { return fmt.Errorf("failed to get current user: %w", err) } - kubeconfig := filepath.Join(u.HomeDir, ".kube/config") + kubeconfig := filepath.Join(u.HomeDir, ".kube", "config") config, err = loadConfig(kubeconfig) if err != nil { diff --git a/plugins/inputs/system/ps.go b/plugins/inputs/system/ps.go index 0cd6ff32d..3e68f19b2 100644 --- a/plugins/inputs/system/ps.go +++ b/plugins/inputs/system/ps.go @@ -172,7 +172,7 @@ partitionRange: continue } - du.Path = filepath.Join("/", strings.TrimPrefix(p.Mountpoint, hostMountPrefix)) + du.Path = filepath.Join(string(os.PathSeparator), strings.TrimPrefix(p.Mountpoint, hostMountPrefix)) du.Fstype = p.Fstype usage = append(usage, du) partitions = append(partitions, &p) diff --git a/plugins/processors/port_name/services_path.go b/plugins/processors/port_name/services_path.go index 291d4eb1b..32d75a6f2 100644 --- a/plugins/processors/port_name/services_path.go +++ b/plugins/processors/port_name/services_path.go @@ -8,5 +8,5 @@ import ( ) func servicesPath() string { - return filepath.Join(os.Getenv("WINDIR"), `system32\drivers\etc\services`) + return filepath.Join(os.Getenv("WINDIR"), "system32", "drivers", "etc", "services") } diff --git a/testutil/socket.go b/testutil/socket.go index 9d897da75..5f490f539 100644 --- a/testutil/socket.go +++ b/testutil/socket.go @@ -19,7 +19,7 @@ func TempSocket(tb testing.TB) string { // If the name of the test is long, the path length could exceed 104 // characters, and this would result in listen unix ...: bind: invalid argument if runtime.GOOS == "darwin" { - sock := filepath.Join("/tmp", "sock") + sock := "/tmp/sock" tb.Cleanup(func() { require.NoError(tb, os.RemoveAll(sock))