69 lines
2.9 KiB
Go
69 lines
2.9 KiB
Go
package releasekit_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
releasekit "gitea.maximumdirect.net/eric/ReleaseKit"
|
|
)
|
|
|
|
func TestValidationErrorPresentsFindingsAlongsideCause(t *testing.T) {
|
|
cause := errors.New("inspection failed")
|
|
for _, withCause := range []bool{false, true} {
|
|
diagnostic := &releasekit.ValidationError{
|
|
Check: "file-size",
|
|
Findings: []releasekit.ValidationFinding{{Path: "main.go", Line: 3, Column: 2, Message: "oversized source", Target: "linked.go", Directive: "example directive"}},
|
|
Measurement: &releasekit.FileSizeMeasurement{Bytes: 12, LimitBytes: 10},
|
|
}
|
|
if withCause {
|
|
diagnostic.Cause = cause
|
|
}
|
|
for _, part := range []string{"file-size", "main.go:3:2", "oversized source", "linked.go", "example directive", "12 bytes", "10 byte limit"} {
|
|
if !strings.Contains(diagnostic.Error(), part) {
|
|
t.Errorf("withCause=%t: %q omits %q", withCause, diagnostic.Error(), part)
|
|
}
|
|
}
|
|
if withCause && (!errors.Is(diagnostic, cause) || !strings.Contains(diagnostic.Error(), cause.Error())) {
|
|
t.Fatalf("diagnostic lost its cause: %v", diagnostic)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCandidateOperationsPreserveCallbackValidationDiagnostics(t *testing.T) {
|
|
repo := newPublishFixture(t)
|
|
cause := errors.New("example execution failed")
|
|
diagnostic := &releasekit.ValidationError{
|
|
Check: "examples",
|
|
Findings: []releasekit.ValidationFinding{{Path: "examples/run/main.go", Message: "unexpected output"}},
|
|
Cause: cause,
|
|
}
|
|
request := releasekit.ReleaseRequest{
|
|
RepositoryDir: repo.work, Product: "Sample App", Version: "v1.2.3",
|
|
AdditionalChecks: func(context.Context, releasekit.Candidate) error { return diagnostic },
|
|
}
|
|
candidate, checkError := releasekit.Check(t.Context(), request)
|
|
result, publishError := releasekit.Publish(t.Context(), request)
|
|
for _, err := range []error{checkError, publishError} {
|
|
var got *releasekit.ValidationError
|
|
if !errors.As(err, &got) || got != diagnostic || !errors.Is(err, cause) {
|
|
t.Fatalf("callback diagnostic identity or cause lost: %v", err)
|
|
}
|
|
for _, part := range []string{"validate candidate", "examples/run/main.go", "unexpected output", cause.Error()} {
|
|
if !strings.Contains(err.Error(), part) {
|
|
t.Errorf("callback error %q omits %q", err, part)
|
|
}
|
|
}
|
|
}
|
|
if candidate.ModuleDir != repo.work || candidate.ModulePath != "example.com/releasekit-publish-fixture" || candidate.Tag != "v1.2.3" || candidate.Commit != repo.commit {
|
|
t.Fatalf("Check() callback failure candidate = %#v, want captured identity", candidate)
|
|
}
|
|
if result.State != releasekit.PublicationNotAttempted || result.TagObject != "" {
|
|
t.Fatalf("callback failure publication result = %#v", result)
|
|
}
|
|
if result.RepositoryDir != repo.work || result.ModuleDir != repo.work || result.ModulePath != candidate.ModulePath || result.Tag != candidate.Tag || result.CandidateCommit != candidate.Commit {
|
|
t.Fatalf("Publish() callback failure result = %#v, want captured identity", result)
|
|
}
|
|
}
|