From 3c0ed84f4df4f0961af05e802be38d9bc256b5be Mon Sep 17 00:00:00 2001 From: Ali Al-Shabibi Date: Wed, 20 Mar 2024 21:11:45 +0100 Subject: [PATCH] generate the data file instead (#28) --- file/file_test.go | 24 ++++++++++++++++++++---- file/testdata/data.txt | 1 - 2 files changed, 20 insertions(+), 5 deletions(-) delete mode 100644 file/testdata/data.txt diff --git a/file/file_test.go b/file/file_test.go index a34180e..240a958 100644 --- a/file/file_test.go +++ b/file/file_test.go @@ -17,6 +17,8 @@ package file_test import ( "context" "crypto/sha256" + "os" + "path" "testing" "github.com/google/go-cmp/cmp" @@ -63,7 +65,21 @@ func (*fakePutClient) CloseSend() error { return nil } +func generateFile(t *testing.T, data string) string { + // Create a temporary file + fileName := path.Join(t.TempDir(), "data") + + // Write some text to the file + if err := os.WriteFile(fileName, []byte(data), 0644); err != nil { + t.Fatalf("unable to write temp file contents: %v", err) + } + + return fileName +} + func TestPut(t *testing.T) { + const data = "some really important data" + fileName := generateFile(t, data) hash := sha256.New() _, err := hash.Write([]byte(`some really important data`)) if err != nil { @@ -82,7 +98,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{ @@ -91,7 +107,7 @@ func TestPut(t *testing.T) { }, { Request: &fpb.PutRequest_Contents{ - Contents: []byte(`some really important data`), + Contents: []byte(data), }, }, { @@ -106,7 +122,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{ @@ -118,7 +134,7 @@ func TestPut(t *testing.T) { }, { Request: &fpb.PutRequest_Contents{ - Contents: []byte(`some really important data`), + Contents: []byte(data), }, }, { diff --git a/file/testdata/data.txt b/file/testdata/data.txt deleted file mode 100644 index 3e499df..0000000 --- a/file/testdata/data.txt +++ /dev/null @@ -1 +0,0 @@ -some really important data \ No newline at end of file