refactor(seedspec): move seed spec parsing out of the campaign command

the campaign tool and the sweep tools that drive it have to read a seed specification the same way, or a sweep records an intent that differs from what ran. parseSeeds becomes seedspec.Parse with no behaviour change.
This commit is contained in:
pj committed 2026-08-16 17:44:57 +05:30
1 parent 7823f53453
commit a28178337c
3 files changed
+17 -11

No files matched your search

+3 -1
View File
@@ -15,6 +15,8 @@ import (
"strings" "strings"
"syscall" "syscall"
"time" "time"
"github.com/priyanshujain/sanderling/internal/seedspec"
) )
type config struct { type config struct {
@@ -118,7 +120,7 @@ func parseArguments(arguments []string, stderr io.Writer) (config, error) {
return config{}, fmt.Errorf("--run-timeout %s must exceed --duration %s, or every run is killed before it finishes", return config{}, fmt.Errorf("--run-timeout %s must exceed --duration %s, or every run is killed before it finishes",
configuration.runTimeout, configuration.duration) configuration.runTimeout, configuration.duration)
} }
seeds, err := parseSeeds(seedSpecification) seeds, err := seedspec.Parse(seedSpecification)
if err != nil { if err != nil {
return config{}, fmt.Errorf("--seeds: %w", err) return config{}, fmt.Errorf("--seeds: %w", err)
} }
@@ -1,4 +1,8 @@
package main // Package seedspec expands the seed specification a campaign is given into the
// explicit list of seeds it intends to run. The campaign tool and the tools
// that drive it have to read a specification the same way, or a sweep records
// an intent that differs from what ran.
package seedspec
import ( import (
"fmt" "fmt"
@@ -6,9 +10,9 @@ import (
"strings" "strings"
) )
// parseSeeds expands a seed specification such as "1-10,20,30-32" into the // Parse expands a seed specification such as "1-10,20,30-32" into the
// explicit seed list a campaign intends to run. // explicit seed list a campaign intends to run.
func parseSeeds(specification string) ([]int64, error) { func Parse(specification string) ([]int64, error) {
trimmed := strings.TrimSpace(specification) trimmed := strings.TrimSpace(specification)
if trimmed == "" { if trimmed == "" {
return nil, fmt.Errorf("empty seed spec") return nil, fmt.Errorf("empty seed spec")
@@ -1,4 +1,4 @@
package main package seedspec
import ( import (
"slices" "slices"
@@ -6,7 +6,7 @@ import (
"testing" "testing"
) )
func TestParseSeeds_RangesAndLists(t *testing.T) { func TestParse_RangesAndLists(t *testing.T) {
cases := []struct { cases := []struct {
specification string specification string
want []int64 want []int64
@@ -18,7 +18,7 @@ func TestParseSeeds_RangesAndLists(t *testing.T) {
{"4-4", []int64{4}}, {"4-4", []int64{4}},
} }
for _, testCase := range cases { for _, testCase := range cases {
got, err := parseSeeds(testCase.specification) got, err := Parse(testCase.specification)
if err != nil { if err != nil {
t.Fatalf("%q: %v", testCase.specification, err) t.Fatalf("%q: %v", testCase.specification, err)
} }
@@ -28,9 +28,9 @@ func TestParseSeeds_RangesAndLists(t *testing.T) {
} }
} }
func TestParseSeeds_RejectsSeedZero(t *testing.T) { func TestParse_RejectsSeedZero(t *testing.T) {
for _, specification := range []string{"0", "1,0,2", "0-3"} { for _, specification := range []string{"0", "1,0,2", "0-3"} {
_, err := parseSeeds(specification) _, err := Parse(specification)
if err == nil { if err == nil {
t.Fatalf("%q: expected rejection of seed 0", specification) t.Fatalf("%q: expected rejection of seed 0", specification)
} }
@@ -40,9 +40,9 @@ func TestParseSeeds_RejectsSeedZero(t *testing.T) {
} }
} }
func TestParseSeeds_RejectsMalformed(t *testing.T) { func TestParse_RejectsMalformed(t *testing.T) {
for _, specification := range []string{"", " ", "abc", "1,,2", "5-1", "1-", "-5", "1-2-3", "1.5", "2,2"} { for _, specification := range []string{"", " ", "abc", "1,,2", "5-1", "1-", "-5", "1-2-3", "1.5", "2,2"} {
if seeds, err := parseSeeds(specification); err == nil { if seeds, err := Parse(specification); err == nil {
t.Errorf("%q: expected error, got %v", specification, seeds) t.Errorf("%q: expected error, got %v", specification, seeds)
} }
} }