WebUI/src/fixtures/testutil.go
Theta-Dev 60034b4815
All checks were successful
continuous-integration/drone/push Build is passing
add server base
2022-02-01 23:08:45 +01:00

60 lines
951 B
Go

package fixtures
import (
"os"
"path/filepath"
"strings"
)
var envPrefixes = []string{"TSGRAINWEB"}
func doesFileExist(filepath string) bool {
_, err := os.Stat(filepath)
return !os.IsNotExist(err)
}
func getProjectRoot() string {
p, err := os.Getwd()
if err != nil {
panic(err)
}
for i := 0; i < 10; i++ {
if doesFileExist(filepath.Join(p, "go.mod")) {
return p
}
p = filepath.Join(p, "..")
}
panic("Could not find project root")
}
func CdProjectRoot() {
root := getProjectRoot()
err := os.Chdir(root)
if err != nil {
panic(err)
}
}
func GetTestfilesDir() string {
CdProjectRoot()
return filepath.Join("src", "fixtures", "testfiles")
}
func ResetEnv() {
for _, envvar := range os.Environ() {
split := strings.SplitN(envvar, "=", 2)
if len(split) != 2 {
continue
}
key := split[0]
for _, prefix := range envPrefixes {
if strings.HasPrefix(key, prefix) {
_ = os.Unsetenv(key)
}
}
}
}