diff --git a/internal/pkg/file.go b/internal/pkg/file.go index fdff735..98b8eee 100644 --- a/internal/pkg/file.go +++ b/internal/pkg/file.go @@ -3,6 +3,7 @@ package pkg import ( "context" "crypto/sha512" + "fmt" ) // A fileArtifact is an [Artifact] that cures into data known ahead of time. @@ -10,10 +11,28 @@ type fileArtifact []byte var _ KnownChecksum = fileArtifact{} +// fileArtifactNamed embeds fileArtifact alongside a caller-supplied name. +type fileArtifactNamed struct { + fileArtifact + // Caller-supplied user-facing reporting name. + name string +} + +var _ fmt.Stringer = fileArtifactNamed{} + +// String returns the caller-supplied reporting name. +func (a fileArtifactNamed) String() string { return a.name } + // NewFile returns a [File] that cures into a caller-supplied byte slice. // -// Caller must not modify data after NewBin returns. -func NewFile(data []byte) File { return fileArtifact(data) } +// Caller must not modify data after NewFile returns. +func NewFile(name string, data []byte) File { + f := fileArtifact(data) + if name != "" { + return fileArtifactNamed{f, name} + } + return f +} // Kind returns the hardcoded [Kind] constant. func (a fileArtifact) Kind() Kind { return KindFile } diff --git a/internal/pkg/file_test.go b/internal/pkg/file_test.go index 48e5562..07dac04 100644 --- a/internal/pkg/file_test.go +++ b/internal/pkg/file_test.go @@ -15,7 +15,7 @@ func TestFile(t *testing.T) { c.SetStrict(true) cureMany(t, c, []cureStep{ - {"short", pkg.NewFile([]byte{0}), base.Append( + {"short", pkg.NewFile("null", []byte{0}), base.Append( "identifier", "lIx_W4M7tVOcQ8jh08EJOfXf4brRmkEEjvUa7c17vVUzlmtUxlhhrgqmc9aZhjbn", ), pkg.MustDecode( diff --git a/internal/pkg/pkg_test.go b/internal/pkg/pkg_test.go index 2c66438..f5e6dc5 100644 --- a/internal/pkg/pkg_test.go +++ b/internal/pkg/pkg_test.go @@ -496,7 +496,7 @@ func TestCache(t *testing.T) { if _, _, err = c.Cure(stubArtifactF{ kind: pkg.KindExec, params: []byte("unreachable artifact cured after cancel"), - deps: []pkg.Artifact{pkg.NewFile([]byte("unreachable dependency"))}, + deps: []pkg.Artifact{pkg.NewFile("", []byte("unreachable dependency"))}, }); !reflect.DeepEqual(err, context.Canceled) { t.Fatalf("(closed) Cure: error = %v", err) }