Bump the go directive and mise tool pin to Go 1.27.1, the newest toolchain available in the build environment; module versions already resolve to the newest releases present in the pinned module cache. Add .gitea/workflows/ci.yml, which runs on every pull request and every push to main and invokes scripts/check.sh (gofmt, go mod verify, go vet, go build, go test -cover). Also cover the Gitea client token/slug handling with unit tests and document the CI workflow. Add the mise check task so the CI checks can be run locally.
101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package gitea
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIsPRsDisabledError(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
want bool
|
|
}{
|
|
{name: "nil error", err: nil, want: false},
|
|
{name: "pulls unit disabled", err: errors.New(`Get "https://gitea.example.com/api/v1/repos/o/r/pulls": Can't read pulls`), want: true},
|
|
{name: "code unit disabled", err: errors.New("repository unit UnitTypeCode is disabled"), want: true},
|
|
{name: "unrelated error", err: errors.New("connection refused"), want: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := isPRsDisabledError(tt.err); got != tt.want {
|
|
t.Fatalf("isPRsDisabledError(%v) = %v, want %v", tt.err, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// newVersionServer starts a stub Gitea API server that only answers the
|
|
// version probe performed by the SDK client constructor.
|
|
func newVersionServer(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/v1/version" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"version":"1.22.0"}`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func TestNewClientRequiresToken(t *testing.T) {
|
|
t.Setenv("STAMP_GITEA_TOKEN", "")
|
|
t.Setenv("GITEA_TOKEN", "")
|
|
t.Setenv("GITEA_BASE_URL", "https://gitea.example.com")
|
|
|
|
_, err := NewClient("owner/repo")
|
|
if err == nil {
|
|
t.Fatal("expected an error when no token is configured")
|
|
}
|
|
if !strings.Contains(err.Error(), "STAMP_GITEA_TOKEN") {
|
|
t.Fatalf("expected error to mention STAMP_GITEA_TOKEN, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNewClientPrefersStampToken(t *testing.T) {
|
|
srv := newVersionServer(t)
|
|
t.Setenv("STAMP_GITEA_TOKEN", "stamp-token")
|
|
t.Setenv("GITEA_TOKEN", "fallback-token")
|
|
t.Setenv("GITEA_BASE_URL", srv.URL)
|
|
|
|
c, err := NewClient("owner/repo")
|
|
if err != nil {
|
|
t.Fatalf("NewClient returned error: %v", err)
|
|
}
|
|
if c == nil || c.owner != "owner" || c.repo != "repo" {
|
|
t.Fatalf("unexpected client: %+v", c)
|
|
}
|
|
}
|
|
|
|
func TestNewClientFallsBackToGiteaToken(t *testing.T) {
|
|
srv := newVersionServer(t)
|
|
t.Setenv("STAMP_GITEA_TOKEN", "")
|
|
t.Setenv("GITEA_TOKEN", "fallback-token")
|
|
t.Setenv("GITEA_BASE_URL", srv.URL)
|
|
|
|
if _, err := NewClient("owner/repo"); err != nil {
|
|
t.Fatalf("NewClient returned error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNewClientRejectsInvalidSlug(t *testing.T) {
|
|
t.Setenv("STAMP_GITEA_TOKEN", "stamp-token")
|
|
t.Setenv("GITEA_TOKEN", "")
|
|
t.Setenv("GITEA_BASE_URL", "https://gitea.example.com")
|
|
|
|
_, err := NewClient("not-a-slug")
|
|
if err == nil {
|
|
t.Fatal("expected an error for a slug without owner/repo")
|
|
}
|
|
if !strings.Contains(err.Error(), "invalid repo slug") {
|
|
t.Fatalf("expected an invalid slug error, got %v", err)
|
|
}
|
|
}
|