Report temporary metadata cleanup failures
This commit is contained in:
@@ -49,6 +49,10 @@ type BuildReceipt struct {
|
||||
}
|
||||
|
||||
func WriteBuildReceipt(outputDir string, receipt BuildReceipt) (string, error) {
|
||||
return writeBuildReceipt(outputDir, receipt, os.Remove)
|
||||
}
|
||||
|
||||
func writeBuildReceipt(outputDir string, receipt BuildReceipt, remove func(string) error) (resultPath string, resultErr error) {
|
||||
resolvedDir, err := filepath.Abs(outputDir)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve build receipt directory: %w", err)
|
||||
@@ -67,7 +71,11 @@ func WriteBuildReceipt(outputDir string, receipt BuildReceipt) (string, error) {
|
||||
return "", fmt.Errorf("create temporary build receipt: %w", err)
|
||||
}
|
||||
temporaryPath := temporary.Name()
|
||||
defer os.Remove(temporaryPath)
|
||||
defer func() {
|
||||
if err := remove(temporaryPath); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
resultErr = errors.Join(resultErr, fmt.Errorf("remove temporary build receipt %q: %w", temporaryPath, err))
|
||||
}
|
||||
}()
|
||||
if _, err := temporary.Write(contents); err != nil {
|
||||
_ = temporary.Close()
|
||||
return "", fmt.Errorf("write build receipt: %w", err)
|
||||
|
||||
@@ -4,12 +4,68 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteBuildReceiptReportsTemporaryCleanup(t *testing.T) {
|
||||
for _, mode := range []string{"renamed", "cleanup failure", "install and cleanup failure"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
repository := t.TempDir()
|
||||
output := t.TempDir()
|
||||
name := "app-v1.2.3-linux-amd64"
|
||||
receipt := BuildReceipt{
|
||||
SchemaVersion: BuildReceiptSchemaVersion, RepositoryDir: repository, ModuleDir: repository,
|
||||
ModulePath: "example.com/receipt", Tag: "v1.2.3", CandidateCommit: strings.Repeat("a", 40),
|
||||
Version: "v1.2.3", MainPackage: ".", BinaryName: "app", Compiler: "go fixture",
|
||||
Targets: []ReceiptTarget{{OS: "linux", Arch: "amd64"}},
|
||||
Artifacts: []ReceiptArtifact{{Path: filepath.Join(output, name), Filename: name, Target: ReceiptTarget{OS: "linux", Arch: "amd64"}, SHA256: strings.Repeat("a", 64)}},
|
||||
}
|
||||
if mode == "install and cleanup failure" {
|
||||
blocked := filepath.Join(output, BuildReceiptName)
|
||||
if err := os.Mkdir(blocked, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(blocked, "keep"), nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
cleanupErr := errors.New("cleanup failed")
|
||||
var temporaryPath string
|
||||
remove := func(path string) error {
|
||||
temporaryPath = path
|
||||
err := os.Remove(path)
|
||||
if mode == "renamed" {
|
||||
return err
|
||||
}
|
||||
return cleanupErr
|
||||
}
|
||||
path, err := writeBuildReceipt(output, receipt, remove)
|
||||
if mode == "renamed" {
|
||||
if err != nil || path != filepath.Join(output, BuildReceiptName) {
|
||||
t.Fatalf("renamed receipt = %q, %v", path, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !errors.Is(err, cleanupErr) || temporaryPath == "" || !strings.Contains(err.Error(), temporaryPath) {
|
||||
t.Fatalf("cleanup path = %q, error = %v", temporaryPath, err)
|
||||
}
|
||||
if mode == "cleanup failure" && path != filepath.Join(output, BuildReceiptName) {
|
||||
t.Fatalf("installed receipt path = %q", path)
|
||||
}
|
||||
if mode == "install and cleanup failure" {
|
||||
var pathErr *os.PathError
|
||||
if !errors.As(err, &pathErr) {
|
||||
t.Fatalf("install cause missing: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildReceiptRoundTripBindsRootAndNestedModuleIdentity(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user