Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Oct 1, 2024
1 parent 225d8f6 commit 244e1bd
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions pkg/cache/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"context"
"errors"
"path/filepath"
"fmt"
"net/url"
"os"
"testing"

"github.com/grafana/k6build/pkg/cache"
"github.com/grafana/k6build/pkg/util"
)

type object struct {
Expand Down Expand Up @@ -193,42 +195,28 @@ func TestFileCacheRetrieval(t *testing.T) {

testCases := []struct {
title string
object cache.Object
id string
url string
expected []byte
expectErr error
}{
{
title: "download existing object",
object: cache.Object{
ID: "object",
URL: fmt.Sprintf("file://%s/object/data", cacheDir),
},
id: "object",
url: filepath.Join(cacheDir, "/object/data"),
expected: []byte("content"),
expectErr: nil,
},
{
title: "download non existing object",
object: cache.Object{
ID: "object",
URL: fmt.Sprintf("file://%s/another_object/data", cacheDir),
},
expectErr: cache.ErrObjectNotFound,
},
{
title: "download malformed url",
object: cache.Object{
ID: "object",
URL: fmt.Sprintf("file://%s/invalid&path/data", cacheDir),
},
// FIXME: this should be an ErrInvalidURL
id: "object",
url: filepath.Join(cacheDir, "/another_object/data"),
expectErr: cache.ErrObjectNotFound,
},
{
title: "download malicious url",
object: cache.Object{
ID: "object",
URL: fmt.Sprintf("file://%s/../../data", cacheDir),
},
id: "object",
url: filepath.Join(cacheDir, "/../../data"),
expectErr: cache.ErrInvalidURL,
},
}
Expand All @@ -237,7 +225,9 @@ func TestFileCacheRetrieval(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

content, err := fileCache.Download(context.TODO(), tc.object)
objectURL, _ := util.URLFromFilePath(tc.url)
object := cache.Object{ID: tc.id, URL: objectURL.String()}
content, err := fileCache.Download(context.TODO(), object)
if !errors.Is(err, tc.expectErr) {
t.Fatalf("expected %v got %v", tc.expectErr, err)
}
Expand Down

0 comments on commit 244e1bd

Please sign in to comment.