mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-16 06:05:07 +08:00
feat: add option to prune old test run directories, keeping a configurable maximum number.
This commit is contained in:
@@ -31,6 +31,7 @@ type Options struct {
|
||||
Timeout time.Duration
|
||||
Retries int
|
||||
NoPreflight bool
|
||||
MaxKeepRuns int
|
||||
}
|
||||
|
||||
type runSummary struct {
|
||||
@@ -161,6 +162,7 @@ func DefaultOptions() Options {
|
||||
Timeout: 120 * time.Second,
|
||||
Retries: 2,
|
||||
NoPreflight: false,
|
||||
MaxKeepRuns: 5,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,6 +214,11 @@ func Run(ctx context.Context, opts Options) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Prune old test runs, keeping only the most recent N.
|
||||
if err := r.pruneOldRuns(); err != nil {
|
||||
r.warnings = append(r.warnings, "prune old runs: "+err.Error())
|
||||
}
|
||||
|
||||
failed := 0
|
||||
for _, cs := range r.results {
|
||||
if !cs.Passed {
|
||||
@@ -269,6 +276,52 @@ func (r *Runner) prepareRunDir() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// pruneOldRuns removes old test run directories, keeping the most recent MaxKeepRuns.
|
||||
// Run IDs use the format "20060102T150405Z", so alphabetical order == chronological order.
|
||||
func (r *Runner) pruneOldRuns() error {
|
||||
keep := r.opts.MaxKeepRuns
|
||||
if keep <= 0 {
|
||||
return nil // 0 or negative means no pruning
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(r.opts.OutputDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Collect only directories (each run is a directory).
|
||||
var runDirs []string
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() {
|
||||
continue
|
||||
}
|
||||
runDirs = append(runDirs, e.Name())
|
||||
}
|
||||
|
||||
sort.Strings(runDirs)
|
||||
|
||||
if len(runDirs) <= keep {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove oldest runs (those at the beginning of the sorted list).
|
||||
toRemove := runDirs[:len(runDirs)-keep]
|
||||
var errs []string
|
||||
for _, name := range toRemove {
|
||||
dirPath := filepath.Join(r.opts.OutputDir, name)
|
||||
if err := os.RemoveAll(dirPath); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("remove %s: %v", name, err))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stdout, "pruned old test run: %s\n", name)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return errors.New(strings.Join(errs, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runner) runPreflight(ctx context.Context) error {
|
||||
steps := [][]string{
|
||||
{"go", "test", "./...", "-count=1"},
|
||||
|
||||
Reference in New Issue
Block a user