internal/pkg: pass context to file cure
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (push) Successful in 2m52s
Test / ShareFS (push) Successful in 4m42s
Test / Sandbox (race detector) (push) Successful in 5m17s
Test / Hpkg (push) Successful in 5m24s
Test / Hakurei (push) Successful in 5m31s
Test / Hakurei (race detector) (push) Successful in 7m54s
Test / Flake checks (push) Successful in 1m47s

This removes the left over embedded contexts.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-09 05:31:38 +09:00
parent f712466714
commit 34cb4ebd3b
7 changed files with 17 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
package pkg
import (
"context"
"crypto/sha512"
)
@@ -31,4 +32,4 @@ func (a fileArtifact) Checksum() Checksum {
}
// Cure returns the caller-supplied data.
func (a fileArtifact) Cure() ([]byte, error) { return a, nil }
func (a fileArtifact) Cure(context.Context) ([]byte, error) { return a, nil }

View File

@@ -12,8 +12,6 @@ import (
// hardcoded as [http.MethodGet]. Request body is not allowed because it cannot
// be deterministically represented by Params.
type httpArtifact struct {
// Caller-supplied context.
ctx context.Context
// Caller-supplied url string.
url string
@@ -36,18 +34,14 @@ var _ KnownChecksum = new(httpArtifact)
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
func NewHTTPGet(
ctx context.Context,
c *http.Client,
url string,
checksum Checksum,
) File {
if ctx == nil {
ctx = context.Background()
}
if c == nil {
c = http.DefaultClient
}
return &httpArtifact{ctx: ctx, url: url, checksum: checksum, doFunc: c.Do}
return &httpArtifact{url: url, checksum: checksum, doFunc: c.Do}
}
// Kind returns the hardcoded [Kind] constant.
@@ -73,9 +67,9 @@ func (e ResponseStatusError) Error() string {
// do sends the caller-supplied request on the caller-supplied [http.Client]
// and reads its response body to EOF and returns the resulting bytes.
func (a *httpArtifact) do() (data []byte, err error) {
func (a *httpArtifact) do(ctx context.Context) (data []byte, err error) {
var req *http.Request
req, err = http.NewRequestWithContext(a.ctx, http.MethodGet, a.url, nil)
req, err = http.NewRequestWithContext(ctx, http.MethodGet, a.url, nil)
if err != nil {
return
}
@@ -101,7 +95,7 @@ func (a *httpArtifact) do() (data []byte, err error) {
// Cure completes the http request and returns the resulting response body read
// to EOF. Data does not interact with the filesystem.
func (a *httpArtifact) Cure() (data []byte, err error) {
func (a *httpArtifact) Cure(ctx context.Context) (data []byte, err error) {
a.mu.Lock()
defer a.mu.Unlock()
@@ -110,7 +104,7 @@ func (a *httpArtifact) Cure() (data []byte, err error) {
return a.data, nil
}
if data, err = a.do(); err != nil {
if data, err = a.do(ctx); err != nil {
return
}

View File

@@ -31,13 +31,12 @@ func TestHTTPGet(t *testing.T) {
checkWithCache(t, []cacheTestCase{
{"direct", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
f := pkg.NewHTTPGet(
t.Context(),
&client,
"file:///testdata",
testdataChecksum,
)
wantIdent := pkg.KindHTTPGet.Ident([]byte("file:///testdata"))
if got, err := f.Cure(); err != nil {
if got, err := f.Cure(t.Context()); err != nil {
t.Fatalf("Cure: error = %v", err)
} else if string(got) != testdata {
t.Fatalf("Cure: %x, want %x", got, testdata)
@@ -47,7 +46,6 @@ func TestHTTPGet(t *testing.T) {
// check direct validation
f = pkg.NewHTTPGet(
t.Context(),
&client,
"file:///testdata",
pkg.Checksum{},
@@ -55,7 +53,7 @@ func TestHTTPGet(t *testing.T) {
wantErrMismatch := &pkg.ChecksumMismatchError{
Got: testdataChecksum,
}
if _, err := f.Cure(); !reflect.DeepEqual(err, wantErrMismatch) {
if _, err := f.Cure(t.Context()); !reflect.DeepEqual(err, wantErrMismatch) {
t.Fatalf("Cure: error = %#v, want %#v", err, wantErrMismatch)
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdent {
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdent))
@@ -63,14 +61,13 @@ func TestHTTPGet(t *testing.T) {
// check direct response error
f = pkg.NewHTTPGet(
t.Context(),
&client,
"file:///nonexistent",
pkg.Checksum{},
)
wantIdentNonexistent := pkg.KindHTTPGet.Ident([]byte("file:///nonexistent"))
wantErrNotFound := pkg.ResponseStatusError(http.StatusNotFound)
if _, err := f.Cure(); !reflect.DeepEqual(err, wantErrNotFound) {
if _, err := f.Cure(t.Context()); !reflect.DeepEqual(err, wantErrNotFound) {
t.Fatalf("Cure: error = %#v, want %#v", err, wantErrNotFound)
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdentNonexistent {
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdentNonexistent))
@@ -79,7 +76,6 @@ func TestHTTPGet(t *testing.T) {
{"cure", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
f := pkg.NewHTTPGet(
t.Context(),
&client,
"file:///testdata",
testdataChecksum,
@@ -97,7 +93,7 @@ func TestHTTPGet(t *testing.T) {
t.Fatalf("Cure: %x, want %x", checksum, testdataChecksum)
}
if got, err := f.Cure(); err != nil {
if got, err := f.Cure(t.Context()); err != nil {
t.Fatalf("Cure: error = %v", err)
} else if string(got) != testdata {
t.Fatalf("Cure: %x, want %x", got, testdata)
@@ -107,12 +103,11 @@ func TestHTTPGet(t *testing.T) {
// check load from cache
f = pkg.NewHTTPGet(
t.Context(),
&client,
"file:///testdata",
testdataChecksum,
)
if got, err := f.Cure(); err != nil {
if got, err := f.Cure(t.Context()); err != nil {
t.Fatalf("Cure: error = %v", err)
} else if string(got) != testdata {
t.Fatalf("Cure: %x, want %x", got, testdata)
@@ -122,7 +117,6 @@ func TestHTTPGet(t *testing.T) {
// check error passthrough
f = pkg.NewHTTPGet(
t.Context(),
&client,
"file:///nonexistent",
pkg.Checksum{},

View File

@@ -265,7 +265,7 @@ type File interface {
// Callers must not modify the returned byte slice.
//
// Result must remain identical across multiple invocations.
Cure() ([]byte, error)
Cure(ctx context.Context) ([]byte, error)
Artifact
}
@@ -726,7 +726,7 @@ func (c *Cache) openFile(f File) (r io.ReadCloser, err error) {
return
}
var data []byte
if data, err = f.Cure(); err != nil {
if data, err = f.Cure(c.ctx); err != nil {
return
}
r = io.NopCloser(bytes.NewReader(data))
@@ -931,7 +931,7 @@ func (c *Cache) Cure(a Artifact) (
}
var data []byte
data, err = f.Cure()
data, err = f.Cure(c.ctx)
if err != nil {
return
}

View File

@@ -109,7 +109,7 @@ type stubFile struct {
stubArtifact
}
func (a stubFile) Cure() ([]byte, error) { return a.data, a.err }
func (a stubFile) Cure(context.Context) ([]byte, error) { return a.data, a.err }
// newStubFile returns an implementation of [pkg.File] with hardcoded behaviour.
func newStubFile(

View File

@@ -4,7 +4,6 @@ import (
"archive/tar"
"compress/bzip2"
"compress/gzip"
"context"
"encoding/binary"
"errors"
"io"
@@ -42,13 +41,12 @@ func NewTar(a Artifact, compression uint64) Artifact {
// NewHTTPGetTar is abbreviation for NewHTTPGet passed to NewTar.
func NewHTTPGetTar(
ctx context.Context,
hc *http.Client,
url string,
checksum Checksum,
compression uint64,
) Artifact {
return NewTar(NewHTTPGet(ctx, hc, url, checksum), compression)
return NewTar(NewHTTPGet(hc, url, checksum), compression)
}
// Kind returns the hardcoded [Kind] constant.

View File

@@ -107,7 +107,6 @@ func checkTarHTTP(
}()
a := pkg.NewHTTPGetTar(
t.Context(),
&client,
"file:///testdata",
testdataChecksum,