Skip to content

Commit

Permalink
generate the data file instead
Browse files Browse the repository at this point in the history
  • Loading branch information
alshabib committed Mar 20, 2024
1 parent 4dbbf14 commit 0cf3a8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
37 changes: 33 additions & 4 deletions file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package file_test
import (
"context"
"crypto/sha256"
"os"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -28,6 +29,10 @@ import (
"google.golang.org/protobuf/testing/protocmp"
)

const (
data = "some really important data"
)

type fakeFileClient struct {
fpb.FileClient
PutFn func(ctx context.Context, opts ...grpc.CallOption) (fpb.File_PutClient, error)
Expand Down Expand Up @@ -63,7 +68,31 @@ func (*fakePutClient) CloseSend() error {
return nil
}

func generateFile(t *testing.T) string {
// Create a temporary file
file, err := os.CreateTemp("/tmp", "data-*.txt")
if err != nil {
t.Fatalf("unable to create temp file: %v", err)
}

// Write some text to the file
_, err = file.WriteString(data)
if err != nil {
t.Fatalf("unable to write temp file contents: %v", err)
}

// Close the file
err = file.Close()
if err != nil {
t.Fatalf("unable to close temp file: %v", err)
}

return file.Name()
}

func TestPut(t *testing.T) {
fileName := generateFile(t)
defer os.Remove(fileName)
hash := sha256.New()
_, err := hash.Write([]byte(`some really important data`))
if err != nil {
Expand All @@ -82,7 +111,7 @@ func TestPut(t *testing.T) {
},
{
desc: "put-with-file",
op: file.NewPutOperation().SourceFile("testdata/data.txt"),
op: file.NewPutOperation().SourceFile(fileName),
wantReq: []*fpb.PutRequest{
{
Request: &fpb.PutRequest_Open{
Expand All @@ -91,7 +120,7 @@ func TestPut(t *testing.T) {
},
{
Request: &fpb.PutRequest_Contents{
Contents: []byte(`some really important data`),
Contents: []byte(data),
},
},
{
Expand All @@ -106,7 +135,7 @@ func TestPut(t *testing.T) {
},
{
desc: "put-with-all-details",
op: file.NewPutOperation().SourceFile("testdata/data.txt").RemoteFile("/tmp/here").Perms(644),
op: file.NewPutOperation().SourceFile(fileName).RemoteFile("/tmp/here").Perms(644),
wantReq: []*fpb.PutRequest{
{
Request: &fpb.PutRequest_Open{
Expand All @@ -118,7 +147,7 @@ func TestPut(t *testing.T) {
},
{
Request: &fpb.PutRequest_Contents{
Contents: []byte(`some really important data`),
Contents: []byte(data),
},
},
{
Expand Down
1 change: 0 additions & 1 deletion file/testdata/data.txt

This file was deleted.

0 comments on commit 0cf3a8a

Please sign in to comment.