This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"gitea.dev/modules/log"
|
||||
)
|
||||
|
||||
type LogChecker struct {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"gitea.dev/modules/log"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -11,10 +11,17 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// RedirectURL returns the redirect URL of a http response.
|
||||
@@ -47,7 +54,7 @@ func ParseJSONError(buf []byte) (ret struct {
|
||||
}
|
||||
|
||||
func ParseJSONRedirect(buf []byte) (ret struct {
|
||||
Redirect string `json:"redirect"`
|
||||
Redirect *string `json:"redirect"`
|
||||
},
|
||||
) {
|
||||
_ = json.Unmarshal(buf, &ret)
|
||||
@@ -141,3 +148,86 @@ 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
|
||||
}
|
||||
|
||||
var normalizeHTMLSpacesRegexp = sync.OnceValue(func() (ret struct {
|
||||
afterRt, beforeLt *regexp.Regexp
|
||||
},
|
||||
) {
|
||||
ret.afterRt = regexp.MustCompile(`>\s*`)
|
||||
ret.beforeLt = regexp.MustCompile(`\s*<`)
|
||||
return ret
|
||||
})
|
||||
|
||||
func NormalizeHTMLSpaces(s string) string {
|
||||
vars := normalizeHTMLSpacesRegexp()
|
||||
s = vars.afterRt.ReplaceAllString(s, ">\n")
|
||||
s = vars.beforeLt.ReplaceAllString(s, "\n<")
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func NormalizeHTMLAttributes(t TestingT, s string) string {
|
||||
nodes, err := html.Parse(strings.NewReader(s))
|
||||
if err != nil {
|
||||
t.Errorf("failed to parse expected HTML: %v", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
var normalize func(n *html.Node)
|
||||
normalize = func(n *html.Node) {
|
||||
slices.SortFunc(n.Attr, func(a, b html.Attribute) int {
|
||||
if cmp := strings.Compare(a.Namespace, b.Namespace); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
if cmp := strings.Compare(a.Key, b.Key); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
return strings.Compare(a.Val, b.Val)
|
||||
})
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
normalize(c)
|
||||
}
|
||||
}
|
||||
var sb strings.Builder
|
||||
if err = html.Render(&sb, nodes); err != nil {
|
||||
t.Errorf("failed to render HTML: %v", err)
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user