From 9e6e892bd03fa51f4b4152aab52bf69ad1f0fd7a Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Sun, 30 Aug 2026 18:38:16 +0300 Subject: [PATCH] caddyhttp: call StaticIPRange.Provision directly in tests TestStaticIPRangeProvision was re-implementing Provision's loop instead of calling it. The real Provision only touches the Ranges field, so a zero-value caddy.Context is enough to exercise it. Signed-off-by: Mohammed Al Sahaf --- modules/caddyhttp/ip_range_test.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/modules/caddyhttp/ip_range_test.go b/modules/caddyhttp/ip_range_test.go index 959b10ac8..95d7a51e7 100644 --- a/modules/caddyhttp/ip_range_test.go +++ b/modules/caddyhttp/ip_range_test.go @@ -3,6 +3,8 @@ package caddyhttp import ( "net/netip" "testing" + + "github.com/caddyserver/caddy/v2" ) func TestCIDRExpressionToPrefix(t *testing.T) { @@ -135,25 +137,13 @@ func TestStaticIPRangeProvision(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &StaticIPRange{Ranges: tt.ranges} - // We can't easily create a caddy.Context here without full module setup, - // but Provision only uses the ranges field, so we test the logic directly. - // The Provision method calls CIDRExpressionToPrefix which we test separately. - var parsedCount int - var gotErr bool - for _, r := range s.Ranges { - _, err := CIDRExpressionToPrefix(r) - if err != nil { - gotErr = true - break - } - parsedCount++ - } + err := s.Provision(caddy.Context{}) - if gotErr != tt.wantErr { - t.Errorf("provision error = %v, wantErr %v", gotErr, tt.wantErr) + if (err != nil) != tt.wantErr { + t.Errorf("Provision() error = %v, wantErr %v", err, tt.wantErr) } - if !tt.wantErr && parsedCount != tt.wantLen { - t.Errorf("parsed %d ranges, want %d", parsedCount, tt.wantLen) + if !tt.wantErr && len(s.ranges) != tt.wantLen { + t.Errorf("parsed %d ranges, want %d", len(s.ranges), tt.wantLen) } }) }