Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hammond authored and Nathan Hammond committed May 17, 2023
1 parent d333680 commit 528d6fd
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions cli/internal/cache/cache_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"bytes"
"errors"
"net/http"
"os"
"testing"

"github.com/DataDog/zstd"

"github.com/vercel/turbo/cli/internal/cacheitem"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
Expand All @@ -17,9 +18,21 @@ import (

type errorResp struct {
err error
t *testing.T
}

func (sr *errorResp) PutArtifact(hash string, body []byte, duration int, tag string) error {
sr.t.Helper()
outdir := turbopath.AbsoluteSystemPathFromUpstream(sr.t.TempDir())
cache := cacheitem.FromReader(bytes.NewReader(body), true)
restored, err := cache.Restore(outdir)

sr.t.Log(restored)
assert.Equal(sr.t, restored[0].ToString(), "one")
assert.Equal(sr.t, restored[1].ToString(), "two")
assert.Equal(sr.t, len(restored), 2)
assert.NilError(sr.t, err, "Restoration was successful.")

return sr.err
}

Expand Down Expand Up @@ -240,6 +253,34 @@ func TestRestoreInvalidTar(t *testing.T) {
assert.Equal(t, string(contents), string(expectedContents), "expected to not overwrite file")
}

// Note that testing Put will require mocking the filesystem and is not currently the most
// interesting test. The current implementation directly returns the error from PutArtifact.
// We should still add the test once feasible to avoid future breakage.
func Test_httpCache_Put(t *testing.T) {
root := fs.AbsoluteSystemPathFromUpstream(t.TempDir())
_ = root.Join("one").WriteFile(nil, 0644)
_ = root.Join("two").WriteFile(nil, 0644)

clientErr := errors.New("PutArtifact")
client := &errorResp{err: clientErr, t: t}

cache := newHTTPCache(Opts{}, client, nil, root)

assert.ErrorIs(
t,
cache.Put(root, "000", 10, []turbopath.AnchoredSystemPath{"one", "two"}),
clientErr,
"Succeeds at writing, cache item is successfully passed through.",
)

assert.ErrorIs(
t,
cache.Put(root, "000", 10, []turbopath.AnchoredSystemPath{"one", "two", "missing"}),
os.ErrNotExist,
"Errors with missing file.",
)

assert.ErrorIs(
t,
cache.Put(root, "000", 10, []turbopath.AnchoredSystemPath{"missing", "one", "two"}),
os.ErrNotExist,
"Errors with missing file at first load.",
)
}

0 comments on commit 528d6fd

Please sign in to comment.