From 63f6514be50f30ae849df02f99fd8939dfc596e0 Mon Sep 17 00:00:00 2001 From: Yury Gargay Date: Tue, 21 Nov 2023 17:38:33 +0100 Subject: [PATCH] Add tenv linter (#1322) Tenv is analyzer that detects using `os.Setenv` instead of `t.Setenv` since Go 1.17. --- .golangci.yaml | 7 +++++++ client/internal/dns/server_test.go | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index b637c7ed1bd..d0926fffc36 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -19,6 +19,12 @@ linters-settings: enable: - nilness + tenv: + # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. + # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. + # Default: false + all: true + linters: disable-all: true enable: @@ -28,6 +34,7 @@ linters: - govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string - ineffassign # detects when assignments to existing variables are not used - staticcheck # is a go vet on steroids, applying a ton of static analysis checks + - tenv # Tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17. - typecheck # like the front-end of a Go compiler, parses and type-checks Go code - unused # checks for unused constants, variables, functions and types ## disable by default but the have interesting results so lets add them diff --git a/client/internal/dns/server_test.go b/client/internal/dns/server_test.go index 62b4e186718..875a1a46f95 100644 --- a/client/internal/dns/server_test.go +++ b/client/internal/dns/server_test.go @@ -322,9 +322,9 @@ func TestUpdateDNSServer(t *testing.T) { func TestDNSFakeResolverHandleUpdates(t *testing.T) { ov := os.Getenv("NB_WG_KERNEL_DISABLED") - defer os.Setenv("NB_WG_KERNEL_DISABLED", ov) + defer t.Setenv("NB_WG_KERNEL_DISABLED", ov) - _ = os.Setenv("NB_WG_KERNEL_DISABLED", "true") + t.Setenv("NB_WG_KERNEL_DISABLED", "true") newNet, err := stdnet.NewNet(nil) if err != nil { t.Errorf("create stdnet: %v", err) @@ -773,9 +773,9 @@ func TestDNSPermanent_matchOnly(t *testing.T) { func createWgInterfaceWithBind(t *testing.T) (*iface.WGIface, error) { t.Helper() ov := os.Getenv("NB_WG_KERNEL_DISABLED") - defer os.Setenv("NB_WG_KERNEL_DISABLED", ov) + defer t.Setenv("NB_WG_KERNEL_DISABLED", ov) - _ = os.Setenv("NB_WG_KERNEL_DISABLED", "true") + t.Setenv("NB_WG_KERNEL_DISABLED", "true") newNet, err := stdnet.NewNet(nil) if err != nil { t.Fatalf("create stdnet: %v", err)