go.mod and test hygiene · Entire
go.mod and test hygiene
6b45fb8→main·
Soph·1mo ago·4 files·+28 added/-32 removed
Four small hygiene fixes flagged in review:
- go.mod pinned
go 1.26.2in the go directive, forcing that exact patch toolchain on everyone. Usego 1.26for the language requirement plus atoolchain go1.26.2line. - ExampleClient_Sync ran with no timeout (the review flagged real network I/O during go test). The repo's golangci-lint config enforces testableexamples, so the example must keep its
// Output:directive and remain runnable; instead of dropping it, bound the call with a 30s context deadline so it can't hang. The example hosts are RFC 2606 reserved names that don't resolve, so it fails fast and prints nothing. - The smart-HTTP test server's handlers called s.tb.Fatalf from the server's goroutines, where Fatalf's runtime.Goexit doesn't stop the test correctly. Use s.tb.Errorf (safe from any goroutine); each call is the handler's final statement, so the handler still returns.
- SyncRequest.Validate and PlanRequest.Validate were byte-for-byte duplicates; extract validateSyncFields and have both delegate.
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
Sessions
3456387d7a8aView transcript
Changes
4
Mclient.go+13/-25
Mclient_test.go+4/-4
Mexample_test.go+8/-2
Mgo.mod+3/-1
func (r SyncRequest) Validate() error {
if r.Source.URL == "" {
return errors.New("source URL is required")
}
if r.Target.URL == "" {
return errors.New("target URL is required")
}
if err := validateOperationMode(r.Policy.Mode); err != nil {
return err
}
if err := r.Policy.Validate(); err != nil {
return err
}
if _, err := validation.NormalizeProtocolMode(string(r.Policy.Protocol)); err != nil {
return fmt.Errorf("normalize protocol: %w", err)
}
if _, err := validation.ValidateMappings(validationMappings(r.Scope.Mappings), r.Scope.AllRefs); err != nil {
return fmt.Errorf("validate mappings: %w", err)
}
return nil
}
func (r PlanRequest) Validate() error {
if r.Source.URL == "" {
return validateSyncFields(r.Source, r.Target, r.Scope, r.Policy)
}
}
// validateSyncFields validates the fields shared by SyncRequest and
// PlanRequest, whose Validate methods are otherwise identical.
func validateSyncFields(source, target Endpoint, scope RefScope, policy SyncPolicy) error {
if source.URL == "" {
return errors.New("source URL is required")
}
if target.URL == "" {
return errors.New("target URL is required")
}
if err := validateOperationMode(policy.Mode); err != nil {
return err
}
if err := policy.Validate(); err != nil {
return err
}
if _, err := validation.NormalizeProtocolMode(string(policy.Protocol)); err != nil {
return fmt.Errorf("normalize protocol: %w", err)
}
if _, err := validation.ValidateMappings(validationMappings(scope.Mappings), scope.AllRefs); err != nil {
return fmt.Errorf("validate mappings: %w", err)
}
return nil
}
Mclient.go+13/-25
w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", service))
if _, err := w.Write(buf.Bytes()); err != nil {
s.tb.Fatalf("write advertised refs: %v", err)
s.tb.Errorf("write advertised refs: %v", err)
}
}
Mclient_test.go+4/-4
import (
"context"
"net/http"
"time"
"entire.io/entire/git-sync"
)
if _, err := client.Sync(context.Background(), gitsync.SyncRequest{
// Bound the call with a context deadline so it can't hang on network I/O
// when go test runs this example. The hosts below are RFC 2606 reserved
// names that don't resolve, so the call fails fast and prints nothing.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if _, err := client.Sync(ctx, gitsync.SyncRequest{
Source: gitsync.Endpoint{URL: "https://github.example/source/repo.git"},
Target: gitsync.Endpoint{URL: "https://git.example/target/repo.git"},
Scope: gitsync.RefScope{Branches: []string{"main"}},
}); err != nil {
return // network error expected in example environment
}
// Output:
}
Mexample_test.go+8/-2
module entire.io/entire/git-sync
go 1.26.2
go 1.26
toolchain go1.26.2
require (
github.com/go-git/go-billy/v6 v6.0.0-alpha.1
)
Mgo.mod+3/-1