mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-18 19:11:06 +00:00
chore: clean up tests (#37715)
1. use MockVariableValue as much as possible 2. use wg.Go as much as possible instead of Add/Done 3. simplify global lock's DefaultLocker logic to make it easier to test 4. introduce a general approach for getting external service config in CI 5. remove unclear & unnecessary "t.Skip" 6. use modern generic syntax for remaining "DecodeJSON" calls 7. clarify test result for "list gitignore templates" and "list licenses"
This commit is contained in:
@@ -11,7 +11,10 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
@@ -141,3 +144,41 @@ func CompressGzip(content string) *bytes.Buffer {
|
||||
_ = cw.Close()
|
||||
return buf
|
||||
}
|
||||
|
||||
var AllowSkipExternalService = sync.OnceValue(func() bool {
|
||||
isLocalTesting := os.Getenv("CI") == ""
|
||||
ciSkipExternal, _ := strconv.ParseBool(os.Getenv("GITEA_TEST_CI_SKIP_EXTERNAL"))
|
||||
return isLocalTesting || ciSkipExternal
|
||||
})
|
||||
|
||||
type TestingT interface {
|
||||
Helper()
|
||||
Skipf(format string, args ...any)
|
||||
Errorf(format string, args ...any)
|
||||
Fatalf(format string, args ...any)
|
||||
}
|
||||
|
||||
func ExternalServiceHTTP(t TestingT, envVarName, def string) string {
|
||||
t.Helper()
|
||||
val := util.IfZero(os.Getenv(envVarName), def)
|
||||
if val == "" {
|
||||
if AllowSkipExternalService() {
|
||||
t.Skipf("skipping test because %s is not set", envVarName)
|
||||
} else {
|
||||
t.Fatalf("%s is not set, but skipping is not allowed in CI", envVarName)
|
||||
}
|
||||
}
|
||||
// minio's endpoint is "host:port" pattern
|
||||
testURL := util.Iif(strings.Contains(val, "://"), val, "http://"+val)
|
||||
resp, err := http.Get(testURL)
|
||||
if err != nil {
|
||||
if AllowSkipExternalService() {
|
||||
t.Skipf("skipping test because %s is not ready", val)
|
||||
} else {
|
||||
t.Fatalf("%s is not ready, but skipping is not allowed in CI", val)
|
||||
}
|
||||
} else {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user