test(gitea): add tests for config and templates
This commit is contained in:
parent
6507d44725
commit
a71976604a
8 changed files with 208 additions and 12 deletions
32
.drone.yml
32
.drone.yml
|
@ -10,41 +10,57 @@ trigger:
|
|||
- tag
|
||||
|
||||
steps:
|
||||
- name: Setup
|
||||
image: docker.io/golang:1.20-bullseye
|
||||
network_mode: host
|
||||
commands:
|
||||
- apt-get update && apt-get install -y jq
|
||||
- go mod download
|
||||
- go install "github.com/vektra/mockery/v2@$(curl https://api.github.com/repos/vektra/mockery/releases | jq -r '. | first | .tag_name')"
|
||||
- go generate ./...
|
||||
volumes:
|
||||
- name: go-cache
|
||||
path: /go
|
||||
|
||||
- name: Lint
|
||||
image: docker.io/golangci/golangci-lint:latest
|
||||
environment:
|
||||
GO111MODULE: "on"
|
||||
CGO_ENABLED: "0"
|
||||
GOMEMLIMIT: "1150MiB"
|
||||
NITTER_BASE_ADDRESS: https://code.icb4dc0.de
|
||||
NITTER_TOKEN:
|
||||
from_secret: gitea_token
|
||||
volumes:
|
||||
- name: go-cache
|
||||
path: /go
|
||||
commands:
|
||||
- mkdir out
|
||||
- golangci-lint run --out-format json --issues-exit-code 0 > out/results.json
|
||||
- if [ -n "$DRONE_PULL_REQUEST}" ]; then go run main.go gitea pr --namespace "$${DRONE_REPO_NAMESPACE}" --repo "$${DRONE_REPO_NAME}" --result-file out/results.json --pull-index "$${DRONE_PULL_REQUEST}"; fi
|
||||
depends_on:
|
||||
- Setup
|
||||
volumes:
|
||||
- name: go-cache
|
||||
path: /go
|
||||
|
||||
- name: Test
|
||||
image: docker.io/golang:1.20-bullseye
|
||||
network_mode: host
|
||||
environment:
|
||||
GO111MODULE: "on"
|
||||
CGO_ENABLED: "1"
|
||||
DOCKER_HOST: tcp://localhost:2375
|
||||
volumes:
|
||||
- name: go-cache
|
||||
path: /go
|
||||
commands:
|
||||
- go install gotest.tools/gotestsum@latest
|
||||
- gotestsum --junitfile out/results.xml --format pkgname-and-test-fails -- -race -shuffle=on ./...
|
||||
depends_on:
|
||||
- Setup
|
||||
volumes:
|
||||
- name: go-cache
|
||||
path: /go
|
||||
|
||||
- name: junit-reports
|
||||
image: ghcr.io/rohit-gohri/drone-junit:v0
|
||||
settings:
|
||||
paths: out/results.xml
|
||||
depends_on:
|
||||
- Test
|
||||
|
||||
volumes:
|
||||
- name: go-cache
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -11,6 +11,7 @@
|
|||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
mock_*_test.go
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
|
4
.mockery.yaml
Normal file
4
.mockery.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
inpackage: true
|
||||
with-expecter: true
|
||||
testonly: true
|
||||
keeptree: false
|
|
@ -25,7 +25,7 @@ func Gitea() *cobra.Command {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Validate(); err != nil {
|
||||
if err = cfg.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
107
nitters/gitea/config_test.go
Normal file
107
nitters/gitea/config_test.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
package gitea_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"code.icb4dc0.de/prskr/nitter/nitters"
|
||||
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
|
||||
)
|
||||
|
||||
func TestGiteaPRConfig_Validate(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg gitea.GiteaPRConfig
|
||||
errorMatcher func(err error) error
|
||||
}{
|
||||
{
|
||||
name: "Missing namespace",
|
||||
cfg: gitea.GiteaPRConfig{},
|
||||
errorMatcher: expectError(nitters.ErrMissingNamespace),
|
||||
},
|
||||
{
|
||||
name: "Missing repo",
|
||||
cfg: gitea.GiteaPRConfig{
|
||||
GiteaConfig: gitea.GiteaConfig{
|
||||
Config: nitters.Config{
|
||||
Namespace: "some",
|
||||
},
|
||||
},
|
||||
},
|
||||
errorMatcher: expectError(nitters.ErrMissingRepo),
|
||||
},
|
||||
{
|
||||
name: "Missing base address",
|
||||
cfg: gitea.GiteaPRConfig{
|
||||
GiteaConfig: gitea.GiteaConfig{
|
||||
Config: nitters.Config{
|
||||
Namespace: "some",
|
||||
Repo: "repo",
|
||||
},
|
||||
},
|
||||
},
|
||||
errorMatcher: expectError(gitea.ErrMissingBaseAddress),
|
||||
},
|
||||
{
|
||||
name: "Missing token",
|
||||
cfg: gitea.GiteaPRConfig{
|
||||
GiteaConfig: gitea.GiteaConfig{
|
||||
Config: nitters.Config{
|
||||
Namespace: "some",
|
||||
Repo: "repo",
|
||||
},
|
||||
BaseAddress: "http://my-forgejo:3000",
|
||||
},
|
||||
},
|
||||
errorMatcher: expectError(gitea.ErrMissingToken),
|
||||
},
|
||||
{
|
||||
name: "Missing pull request index",
|
||||
cfg: gitea.GiteaPRConfig{
|
||||
GiteaConfig: gitea.GiteaConfig{
|
||||
Config: nitters.Config{
|
||||
Namespace: "some",
|
||||
Repo: "repo",
|
||||
},
|
||||
BaseAddress: "http://my-forgejo:3000",
|
||||
Token: "sup3rS3cur3",
|
||||
},
|
||||
},
|
||||
errorMatcher: expectError(gitea.ErrMissingPRIndex),
|
||||
},
|
||||
{
|
||||
name: "All set",
|
||||
cfg: gitea.GiteaPRConfig{
|
||||
GiteaConfig: gitea.GiteaConfig{
|
||||
Config: nitters.Config{
|
||||
Namespace: "some",
|
||||
Repo: "repo",
|
||||
},
|
||||
BaseAddress: "http://my-forgejo:3000",
|
||||
Token: "sup3rS3cur3",
|
||||
},
|
||||
PRIndex: 11,
|
||||
},
|
||||
errorMatcher: expectError(nil),
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tt := tc
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if err := tt.errorMatcher(tt.cfg.Validate()); err != nil {
|
||||
t.Errorf("Validate() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func expectError(errorToIgnore error) func(err error) error {
|
||||
return func(err error) error {
|
||||
if errors.Is(err, errorToIgnore) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
|
@ -16,12 +16,12 @@ var (
|
|||
_ nitters.Nitter = (*prNitter)(nil)
|
||||
|
||||
//go:embed templates/*
|
||||
templatesFs embed.FS
|
||||
templatesFS embed.FS
|
||||
templates *template.Template
|
||||
)
|
||||
|
||||
func init() {
|
||||
if tmpl, err := template.New("").ParseFS(templatesFs, "templates/*.tmpl.md"); err != nil {
|
||||
if tmpl, err := template.New("issue_templates").ParseFS(templatesFS, "templates/*.tmpl.md"); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
templates = tmpl
|
||||
|
@ -35,6 +35,7 @@ func NewPRNitter(cli PullReviewCreator, cfg *GiteaPRConfig) *prNitter {
|
|||
}
|
||||
}
|
||||
|
||||
//go:generate mockery --name PullReviewCreator
|
||||
type PullReviewCreator interface {
|
||||
CreatePullReview(owner, repo string, index int64, opt gitea.CreatePullReviewOptions) (*gitea.PullReview, *gitea.Response, error)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{{- if .Issue.Severity }}
|
||||
Linter({{ .Issue.severity }}): {{ .Issue.FromLinter }}
|
||||
{{- else }}
|
||||
Linter: {{ .Issue.FromLinter }}
|
||||
**Linter:** {{ .Issue.FromLinter }}
|
||||
{{- end }}
|
||||
|
||||
{{ .Issue.Text }}
|
||||
|
|
67
nitters/gitea/templates/templates_test.go
Normal file
67
nitters/gitea/templates/templates_test.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package templates_test
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io"
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/report"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed *
|
||||
templatesFS embed.FS
|
||||
templates *template.Template
|
||||
)
|
||||
|
||||
func init() {
|
||||
if tmpl, err := template.New("issue_templates").ParseFS(templatesFS, "*.tmpl.md"); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
templates = tmpl
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderTemplates(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
templateName string
|
||||
templateData any
|
||||
}{
|
||||
{
|
||||
name: "Render issue_summary.tmpl.md",
|
||||
templateName: "issue_summary.tmpl.md",
|
||||
templateData: map[string]any{
|
||||
"Report": &report.Data{
|
||||
Error: "Some error",
|
||||
},
|
||||
"Issue": result.Issue{
|
||||
FromLinter: "Sample",
|
||||
Text: "Sample description",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Render issue_comment.tmpl.md",
|
||||
templateName: "issue_comment.tmpl.md",
|
||||
templateData: map[string]any{
|
||||
"Issue": result.Issue{
|
||||
FromLinter: "Sample",
|
||||
Text: "Sample description",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tt := tc
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if err := templates.ExecuteTemplate(io.Discard, tt.templateName, tt.templateData); err != nil {
|
||||
t.Errorf("Failed to render emplate: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue