Refresh reports when delivery failures accumulate
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -361,6 +362,71 @@ func TestFailedRenderingKeepsInitializedEvidenceAfterStreamError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) {
|
||||
primary := errors.New("processing failed")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
primary error
|
||||
failFiles []string
|
||||
checkFiles []string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "later writable report", primary: primary,
|
||||
failFiles: []string{ErrorsFile, RecoveriesFile}, checkFiles: []string{MarkdownFile},
|
||||
want: "processing failed,errors.yml,recoveries.yml",
|
||||
},
|
||||
{
|
||||
name: "previously written reports", primary: primary,
|
||||
failFiles: []string{MarkdownFile}, checkFiles: []string{ErrorsFile, RecoveriesFile},
|
||||
want: "processing failed,report.md",
|
||||
},
|
||||
{
|
||||
name: "completion switches to failed status",
|
||||
failFiles: []string{RecoveriesFile, MarkdownFile}, checkFiles: []string{ErrorsFile},
|
||||
want: "recoveries.yml,report.md",
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual := session.replace
|
||||
attempts := make(map[string]int)
|
||||
session.replace = func(ctx context.Context, path string, payload []byte) error {
|
||||
name := filepath.Base(path)
|
||||
attempts[name]++
|
||||
if slices.Contains(tc.failFiles, name) && attempts[name] == 1 {
|
||||
return os.ErrPermission
|
||||
}
|
||||
return actual(ctx, path, payload)
|
||||
}
|
||||
failures := session.finalize(context.Background(), tc.primary, nil, func(cause error, failures []FileFailure) (ReportBundle, error) {
|
||||
var facts []string
|
||||
if cause != nil {
|
||||
facts = append(facts, cause.Error())
|
||||
}
|
||||
for _, failure := range failures {
|
||||
facts = append(facts, filepath.Base(failure.Path))
|
||||
}
|
||||
payload := []byte(strings.Join(facts, ","))
|
||||
return ReportBundle{Errors: payload, Recoveries: payload, Markdown: payload}, nil
|
||||
})
|
||||
if len(failures) != len(tc.failFiles) {
|
||||
t.Fatalf("file failures = %v", failures)
|
||||
}
|
||||
for _, name := range tc.checkFiles {
|
||||
got, err := os.ReadFile(filepath.Join(dir, name))
|
||||
if err != nil || string(got) != tc.want {
|
||||
t.Errorf("%s = %q, %v, want %q", name, got, err, tc.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type writerFunc func([]byte) (int, error)
|
||||
|
||||
func (write writerFunc) Write(payload []byte) (int, error) { return write(payload) }
|
||||
|
||||
Reference in New Issue
Block a user