From 3fca6c0bf5db9e93634079f8816ab922b2a8c25c Mon Sep 17 00:00:00 2001 From: TheFox0x7 Date: Wed, 17 Jul 2024 23:07:41 +0200 Subject: [PATCH] test: enforce tenv usage in tests --- .golangci.yml | 1 + modules/base/tool_test.go | 5 ++--- modules/user/user_test.go | 3 +-- tests/integration/git_helper_for_declarative_test.go | 6 +++--- tests/integration/gpg_git_test.go | 4 +--- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 57f3c86f05..3f1667aece 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,6 +19,7 @@ linters: - revive - staticcheck - stylecheck + - tenv - typecheck - unconvert - unused diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go index 62de7229ac..cc37fdd324 100644 --- a/modules/base/tool_test.go +++ b/modules/base/tool_test.go @@ -6,7 +6,6 @@ package base import ( "crypto/sha1" "fmt" - "os" "testing" "time" @@ -168,9 +167,9 @@ func TestInt64sToStrings(t *testing.T) { // TODO: Test EntryIcon func TestSetupGiteaRoot(t *testing.T) { - _ = os.Setenv("GITEA_ROOT", "test") + t.Setenv("GITEA_ROOT", "test") assert.Equal(t, "test", SetupGiteaRoot()) - _ = os.Setenv("GITEA_ROOT", "") + t.Setenv("GITEA_ROOT", "") assert.NotEqual(t, "test", SetupGiteaRoot()) } diff --git a/modules/user/user_test.go b/modules/user/user_test.go index 9129ae79a1..372a675d34 100644 --- a/modules/user/user_test.go +++ b/modules/user/user_test.go @@ -4,7 +4,6 @@ package user import ( - "os" "os/exec" "runtime" "strings" @@ -36,7 +35,7 @@ func TestCurrentUsername(t *testing.T) { if user != whoami { t.Errorf("expected %s as user, got: %s", whoami, user) } - os.Setenv("USER", "spoofed") + t.Setenv("USER", "spoofed") user = CurrentUsername() if user != whoami { t.Errorf("expected %s as user, got: %s", whoami, user) diff --git a/tests/integration/git_helper_for_declarative_test.go b/tests/integration/git_helper_for_declarative_test.go index 6fb07e8a01..795fd0d005 100644 --- a/tests/integration/git_helper_for_declarative_test.go +++ b/tests/integration/git_helper_for_declarative_test.go @@ -40,10 +40,10 @@ func withKeyFile(t *testing.T, keyname string, callback func(string)) { assert.NoError(t, err) // Setup ssh wrapper - os.Setenv("GIT_SSH", path.Join(tmpDir, "ssh")) - os.Setenv("GIT_SSH_COMMAND", + t.Setenv("GIT_SSH", path.Join(tmpDir, "ssh")) + t.Setenv("GIT_SSH_COMMAND", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \""+keyFile+"\"") - os.Setenv("GIT_SSH_VARIANT", "ssh") + t.Setenv("GIT_SSH_VARIANT", "ssh") callback(keyFile) } diff --git a/tests/integration/gpg_git_test.go b/tests/integration/gpg_git_test.go index d82b0e9414..5b23b6bd31 100644 --- a/tests/integration/gpg_git_test.go +++ b/tests/integration/gpg_git_test.go @@ -30,10 +30,8 @@ func TestGPGGit(t *testing.T) { err := os.Chmod(tmpDir, 0o700) assert.NoError(t, err) - oldGNUPGHome := os.Getenv("GNUPGHOME") - err = os.Setenv("GNUPGHOME", tmpDir) + t.Setenv("GNUPGHOME", tmpDir) assert.NoError(t, err) - defer os.Setenv("GNUPGHOME", oldGNUPGHome) // Need to create a root key rootKeyPair, err := importTestingKey()