Skip to content

Replace context.Background() → t.Context() #3532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions pkg/downloader/downloader_test.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
package downloader

import (
"context"
"net/http"
"net/http/httptest"
"os"
@@ -44,26 +43,26 @@
t.Run("without cache", func(t *testing.T) {
t.Run("without digest", func(t *testing.T) {
localPath := filepath.Join(t.TempDir(), t.Name())
r, err := Download(context.Background(), localPath, dummyRemoteFileURL)
r, err := Download(t.Context(), localPath, dummyRemoteFileURL)

Check failure on line 46 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 46 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

// download again, make sure StatusSkippedIsReturned
r, err = Download(context.Background(), localPath, dummyRemoteFileURL)
r, err = Download(t.Context(), localPath, dummyRemoteFileURL)

Check failure on line 51 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 51 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
assert.NilError(t, err)
assert.Equal(t, StatusSkipped, r.Status)
})
t.Run("with digest", func(t *testing.T) {
wrongDigest := digest.Digest("sha256:8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9")
localPath := filepath.Join(t.TempDir(), t.Name())
_, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))
_, err := Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))

Check failure on line 58 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 58 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
assert.ErrorContains(t, err, "expected digest")

r, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
r, err := Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))

Check failure on line 61 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 61 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

r, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
r, err = Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))

Check failure on line 65 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 65 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
assert.NilError(t, err)
assert.Equal(t, StatusSkipped, r.Status)
})
@@ -72,18 +71,18 @@
t.Run("serial", func(t *testing.T) {
cacheDir := filepath.Join(t.TempDir(), "cache")
localPath := filepath.Join(t.TempDir(), t.Name())
r, err := Download(context.Background(), localPath, dummyRemoteFileURL,
r, err := Download(t.Context(), localPath, dummyRemoteFileURL,

Check failure on line 74 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 74 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

r, err = Download(context.Background(), localPath, dummyRemoteFileURL,
r, err = Download(t.Context(), localPath, dummyRemoteFileURL,

Check failure on line 79 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 79 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusSkipped, r.Status)

localPath2 := localPath + "-2"
r, err = Download(context.Background(), localPath2, dummyRemoteFileURL,
r, err = Download(t.Context(), localPath2, dummyRemoteFileURL,

Check failure on line 85 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 85 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)
@@ -95,7 +94,7 @@
go func() {
// Parallel download is supported only for different instances with unique localPath.
localPath := filepath.Join(t.TempDir(), t.Name())
r, err := Download(context.Background(), localPath, dummyRemoteFileURL,
r, err := Download(t.Context(), localPath, dummyRemoteFileURL,

Check failure on line 97 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 97 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
results <- downloadResult{r, err}
}()
@@ -108,22 +107,22 @@
})
t.Run("caching-only mode", func(t *testing.T) {
t.Run("serial", func(t *testing.T) {
_, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
_, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))

Check failure on line 110 in pkg/downloader/downloader_test.go

GitHub Actions / Lints

stdversion: testing.Context requires go1.24 or later (file is go1.23) (govet)

Check failure on line 110 in pkg/downloader/downloader_test.go

GitHub Actions / Unit tests (1.23.x)

t.Context undefined (type *testing.T has no field or method Context, but does have unexported field context)
assert.ErrorContains(t, err, "cache directory to be specified")

cacheDir := filepath.Join(t.TempDir(), "cache")
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

r, err = Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
r, err = Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)

localPath := filepath.Join(t.TempDir(), t.Name())
r, err = Download(context.Background(), localPath, dummyRemoteFileURL,
r, err = Download(t.Context(), localPath, dummyRemoteFileURL,
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)
@@ -133,7 +132,7 @@
results := make(chan downloadResult, parallelDownloads)
for range parallelDownloads {
go func() {
r, err := Download(context.Background(), "", dummyRemoteFileURL,
r, err := Download(t.Context(), "", dummyRemoteFileURL,
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
results <- downloadResult{r, err}
}()
@@ -149,7 +148,7 @@
assert.ErrorContains(t, err, "cache directory to be specified")

cacheDir := filepath.Join(t.TempDir(), "cache")
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

@@ -167,7 +166,7 @@
assert.ErrorContains(t, err, "cache directory to be specified")

cacheDir := filepath.Join(t.TempDir(), "cache")
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)
assert.Equal(t, dummyRemoteFileStat.ModTime().Truncate(time.Second).UTC(), r.LastModified)
@@ -211,23 +210,23 @@
opt := []Opt{cacheOpt}

// Download on the first call
r, err := Download(context.Background(), filepath.Join(downloadDir, "1"), ts.URL+"/digest-less.txt", opt...)
r, err := Download(t.Context(), filepath.Join(downloadDir, "1"), ts.URL+"/digest-less.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

// Next download will use the cached download
r, err = Download(context.Background(), filepath.Join(downloadDir, "2"), ts.URL+"/digest-less.txt", opt...)
r, err = Download(t.Context(), filepath.Join(downloadDir, "2"), ts.URL+"/digest-less.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)

// Modifying remote file will cause redownload
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now()))
r, err = Download(context.Background(), filepath.Join(downloadDir, "3"), ts.URL+"/digest-less.txt", opt...)
r, err = Download(t.Context(), filepath.Join(downloadDir, "3"), ts.URL+"/digest-less.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

// Next download will use the cached download
r, err = Download(context.Background(), filepath.Join(downloadDir, "4"), ts.URL+"/digest-less.txt", opt...)
r, err = Download(t.Context(), filepath.Join(downloadDir, "4"), ts.URL+"/digest-less.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)
})
@@ -243,16 +242,16 @@
assert.NilError(t, err)
opt := []Opt{cacheOpt, WithExpectedDigest(digester.Digest())}

r, err := Download(context.Background(), filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...)
r, err := Download(t.Context(), filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)
r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...)
r, err = Download(t.Context(), filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)

// modifying remote file won't cause redownload because expected digest is provided
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now()))
r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...)
r, err = Download(t.Context(), filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...)
assert.NilError(t, err)
assert.Equal(t, StatusUsedCache, r.Status)
})
@@ -270,7 +269,7 @@
t.Cleanup(func() { _ = f.Close() })
testLocalFileURL := "file://" + localFile

r, err := Download(context.Background(), localPath, testLocalFileURL)
r, err := Download(t.Context(), localPath, testLocalFileURL)
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)
})
@@ -284,10 +283,10 @@
testLocalFileURL := "file://" + localTestFile
wrongDigest := digest.Digest(emptyFileDigest)

_, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
_, err := Download(t.Context(), localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
assert.ErrorContains(t, err, "expected digest")

r, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
r, err := Download(t.Context(), localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)
})
@@ -320,7 +319,7 @@
localFile += ".gz"
testLocalFileURL := "file://" + localFile

r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

@@ -338,7 +337,7 @@
localFile += ".bz2"
testLocalFileURL := "file://" + localFile

r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

@@ -354,7 +353,7 @@
assert.NilError(t, os.WriteFile(localFile, testDownloadCompressedContents, 0o644))
testLocalFileURL := "file://" + localFile

r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true))
assert.NilError(t, err)
assert.Equal(t, StatusDownloaded, r.Status)

Loading
Oops, something went wrong.