internal/pkg: optionally named static file
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (race detector) (push) Successful in 8m9s
Test / ShareFS (push) Successful in 8m30s
Test / Hakurei (race detector) (push) Successful in 10m35s
Test / Hakurei (push) Successful in 3m58s
Test / Sandbox (push) Successful in 1m42s
Test / Hpkg (push) Successful in 3m57s
Test / Flake checks (push) Successful in 1m46s
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (race detector) (push) Successful in 8m9s
Test / ShareFS (push) Successful in 8m30s
Test / Hakurei (race detector) (push) Successful in 10m35s
Test / Hakurei (push) Successful in 3m58s
Test / Sandbox (push) Successful in 1m42s
Test / Hpkg (push) Successful in 3m57s
Test / Flake checks (push) Successful in 1m46s
These are generally for generating configuration files or build scripts, naming them is quite useful. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -3,6 +3,7 @@ package pkg
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A fileArtifact is an [Artifact] that cures into data known ahead of time.
|
// A fileArtifact is an [Artifact] that cures into data known ahead of time.
|
||||||
@@ -10,10 +11,28 @@ type fileArtifact []byte
|
|||||||
|
|
||||||
var _ KnownChecksum = fileArtifact{}
|
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.
|
// NewFile returns a [File] that cures into a caller-supplied byte slice.
|
||||||
//
|
//
|
||||||
// Caller must not modify data after NewBin returns.
|
// Caller must not modify data after NewFile returns.
|
||||||
func NewFile(data []byte) File { return fileArtifact(data) }
|
func NewFile(name string, data []byte) File {
|
||||||
|
f := fileArtifact(data)
|
||||||
|
if name != "" {
|
||||||
|
return fileArtifactNamed{f, name}
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
// Kind returns the hardcoded [Kind] constant.
|
// Kind returns the hardcoded [Kind] constant.
|
||||||
func (a fileArtifact) Kind() Kind { return KindFile }
|
func (a fileArtifact) Kind() Kind { return KindFile }
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func TestFile(t *testing.T) {
|
|||||||
c.SetStrict(true)
|
c.SetStrict(true)
|
||||||
|
|
||||||
cureMany(t, c, []cureStep{
|
cureMany(t, c, []cureStep{
|
||||||
{"short", pkg.NewFile([]byte{0}), base.Append(
|
{"short", pkg.NewFile("null", []byte{0}), base.Append(
|
||||||
"identifier",
|
"identifier",
|
||||||
"lIx_W4M7tVOcQ8jh08EJOfXf4brRmkEEjvUa7c17vVUzlmtUxlhhrgqmc9aZhjbn",
|
"lIx_W4M7tVOcQ8jh08EJOfXf4brRmkEEjvUa7c17vVUzlmtUxlhhrgqmc9aZhjbn",
|
||||||
), pkg.MustDecode(
|
), pkg.MustDecode(
|
||||||
|
|||||||
@@ -496,7 +496,7 @@ func TestCache(t *testing.T) {
|
|||||||
if _, _, err = c.Cure(stubArtifactF{
|
if _, _, err = c.Cure(stubArtifactF{
|
||||||
kind: pkg.KindExec,
|
kind: pkg.KindExec,
|
||||||
params: []byte("unreachable artifact cured after cancel"),
|
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) {
|
}); !reflect.DeepEqual(err, context.Canceled) {
|
||||||
t.Fatalf("(closed) Cure: error = %v", err)
|
t.Fatalf("(closed) Cure: error = %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user