50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package validation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunPreservesCanceledGoSetup(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
err := Run(ctx, Request{RepositoryDir: t.TempDir()})
|
|
var failure *Failure
|
|
if !errors.As(err, &failure) {
|
|
t.Fatalf("Run() error = %v, want a validation failure", err)
|
|
}
|
|
if failure.Check != GoTest {
|
|
t.Fatalf("failure check = %q, want %q", failure.Check, GoTest)
|
|
}
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("Run() error = %v, want context cancellation preserved", err)
|
|
}
|
|
}
|
|
|
|
func TestFailureFormatsDiagnosticsAndUnwrapsCause(t *testing.T) {
|
|
wantCause := errors.New("underlying check error")
|
|
failure := &Failure{
|
|
Check: GoTest,
|
|
Findings: []Finding{{
|
|
Path: "README.md",
|
|
Line: 4,
|
|
Column: 2,
|
|
Message: "broken destination",
|
|
}},
|
|
Measurement: &FileSizeMeasurement{Bytes: 12, LimitBytes: 10},
|
|
Cause: wantCause,
|
|
}
|
|
|
|
if !errors.Is(failure, wantCause) {
|
|
t.Fatalf("Failure does not unwrap its cause")
|
|
}
|
|
for _, part := range []string{"go-test", "README.md:4:2", "broken destination", "12 bytes", "10 byte limit", wantCause.Error()} {
|
|
if !strings.Contains(failure.Error(), part) {
|
|
t.Errorf("Failure.Error() = %q; want it to include %q", failure.Error(), part)
|
|
}
|
|
}
|
|
}
|