mirror of https://github.com/go-gitea/gitea.git
Filter out gitea-specific variables in tests
There is no particular reason to set gitea variables globally. Nonetheless, some misguided packagers set GITEA_CUSTOM in /etc/profile breaking tests. Fixes: go-gitea/gitea#36042pull/36070/head
parent
c287a8cdb5
commit
9acb9377af
@ -0,0 +1,31 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package env
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Filter(include, exclude []string) {
|
||||
env := os.Environ()
|
||||
for _, v := range env {
|
||||
included := false
|
||||
for _, i := range include {
|
||||
if strings.HasPrefix(v, i) {
|
||||
included = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !included {
|
||||
for _, e := range exclude {
|
||||
if strings.HasPrefix(v, e) {
|
||||
parts := strings.SplitN(v, "=", 2)
|
||||
os.Unsetenv(parts[0])
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package env
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
t.Setenv("GITEA_FOO", "bar")
|
||||
t.Setenv("FOO", "bar")
|
||||
Filter([]string{}, []string{"GITEA_"})
|
||||
if os.Getenv("GITEA_FOO") != "" {
|
||||
t.FailNow()
|
||||
}
|
||||
if os.Getenv("FOO") != "bar" {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
t.Setenv("GITEA_TEST_FOO", "bar")
|
||||
t.Setenv("GITEA_BAR", "foo")
|
||||
t.Setenv("GITEA_BAR_BAZ", "foo")
|
||||
t.Setenv("GITEA_BAZ", "huz")
|
||||
Filter([]string{"GITEA_TEST_", "GITEA_BAR="}, []string{"GITEA_"})
|
||||
if os.Getenv("GITEA_BAR") != "foo" {
|
||||
t.Fail()
|
||||
}
|
||||
if os.Getenv("GITEA_TEST_FOO") != "bar" {
|
||||
t.Fail()
|
||||
}
|
||||
if os.Getenv("GITEA_BAZ") != "" {
|
||||
t.Fail()
|
||||
}
|
||||
if os.Getenv("GITEA_BAR_BAZ") != "" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue