From c3a595560230886d34e35e63ce258eb79c815ccc Mon Sep 17 00:00:00 2001 From: "Masih H. Derkani" Date: Tue, 20 Jul 2021 16:47:59 +0100 Subject: [PATCH] Avoid writing to files in testdata Avoid writing to files in `testdata` through examples. This is because when tests that use those file are run in parallel will fail if files are modified. Instead write to a temporary file in examples, and whenever opening testdata files in `RW` mode, make a temporary copy. Thanks to @mvdan for pointing this out. Fixes #175 --- v2/blockstore/example_test.go | 13 ++++++++++--- v2/blockstore/readwrite_test.go | 20 +++++++++++++++++++- v2/example_test.go | 8 +++++++- v2/index/example_test.go | 14 ++++++++++---- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/v2/blockstore/example_test.go b/v2/blockstore/example_test.go index 00a81dc..3091e47 100644 --- a/v2/blockstore/example_test.go +++ b/v2/blockstore/example_test.go @@ -3,6 +3,9 @@ package blockstore_test import ( "context" "fmt" + "io/ioutil" + "os" + "path/filepath" blocks "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" @@ -79,10 +82,14 @@ func ExampleOpenReadWrite() { thatBlock := merkledag.NewRawNode([]byte("lobster")).Block andTheOtherBlock := merkledag.NewRawNode([]byte("barreleye")).Block - dest := "../testdata/sample-rw-bs-v2.car" + tdir, err := ioutil.TempDir(os.TempDir(), "example-*") + if err != nil { + panic(err) + } + dst := filepath.Join(tdir, "sample-rw-bs-v2.car") roots := []cid.Cid{thisBlock.Cid(), thatBlock.Cid(), andTheOtherBlock.Cid()} - rwbs, err := blockstore.OpenReadWrite(dest, roots, carv2.UseDataPadding(1413), carv2.UseIndexPadding(42)) + rwbs, err := blockstore.OpenReadWrite(dst, roots, carv2.UseDataPadding(1413), carv2.UseIndexPadding(42)) if err != nil { panic(err) } @@ -110,7 +117,7 @@ func ExampleOpenReadWrite() { // Resume from the same file to add more blocks. // Note the UseDataPadding and roots must match the values passed to the blockstore instance // that created the original file. Otherwise, we cannot resume from the same file. - resumedRwbos, err := blockstore.OpenReadWrite(dest, roots, carv2.UseDataPadding(1413)) + resumedRwbos, err := blockstore.OpenReadWrite(dst, roots, carv2.UseDataPadding(1413)) if err != nil { panic(err) } diff --git a/v2/blockstore/readwrite_test.go b/v2/blockstore/readwrite_test.go index 2494da6..d53379f 100644 --- a/v2/blockstore/readwrite_test.go +++ b/v2/blockstore/readwrite_test.go @@ -570,11 +570,29 @@ func TestReadWriteWithPaddingWorksAsExpected(t *testing.T) { } func TestReadWriteResumptionFromNonV2FileIsError(t *testing.T) { - subject, err := blockstore.OpenReadWrite("../testdata/sample-rootless-v42.car", []cid.Cid{}) + tmpPath := requireTmpCopy(t, "../testdata/sample-rootless-v42.car") + subject, err := blockstore.OpenReadWrite(tmpPath, []cid.Cid{}) require.EqualError(t, err, "cannot resume on CAR file with version 42") require.Nil(t, subject) } +func requireTmpCopy(t *testing.T, src string) string { + srcF, err := os.Open(src) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, srcF.Close()) }) + stats, err := srcF.Stat() + require.NoError(t, err) + + dst := filepath.Join(t.TempDir(), stats.Name()) + dstF, err := os.Create(dst) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, dstF.Close()) }) + + _, err = io.Copy(dstF, srcF) + require.NoError(t, err) + return dst +} + func TestReadWriteResumptionFromFileWithDifferentCarV1PaddingIsError(t *testing.T) { oneTestBlockCid := oneTestBlockWithCidV1.Cid() WantRoots := []cid.Cid{oneTestBlockCid} diff --git a/v2/example_test.go b/v2/example_test.go index dd3e002..37bcc22 100644 --- a/v2/example_test.go +++ b/v2/example_test.go @@ -4,6 +4,8 @@ import ( "bytes" "fmt" "io/ioutil" + "os" + "path/filepath" carv2 "github.com/ipld/go-car/v2" "github.com/ipld/go-car/v2/blockstore" @@ -15,7 +17,11 @@ func ExampleWrapV1File() { // Writing the result to testdata allows reusing that file in other tests, // and also helps ensure that the result is deterministic. src := "testdata/sample-v1.car" - dst := "testdata/sample-wrapped-v2.car" + tdir, err := ioutil.TempDir(os.TempDir(), "example-*") + if err != nil { + panic(err) + } + dst := filepath.Join(tdir, "wrapped-v2.car") if err := carv2.WrapV1File(src, dst); err != nil { panic(err) } diff --git a/v2/index/example_test.go b/v2/index/example_test.go index 47347d8..5070462 100644 --- a/v2/index/example_test.go +++ b/v2/index/example_test.go @@ -3,7 +3,9 @@ package index_test import ( "fmt" "io" + "io/ioutil" "os" + "path/filepath" "reflect" carv2 "github.com/ipld/go-car/v2" @@ -67,8 +69,12 @@ func ExampleWriteTo() { } // Store the index alone onto destination file. - dest := "../testdata/sample-index.carindex" - f, err := os.Create(dest) + tdir, err := ioutil.TempDir(os.TempDir(), "example-*") + if err != nil { + panic(err) + } + dst := filepath.Join(tdir, "index.carindex") + f, err := os.Create(dst) if err != nil { panic(err) } @@ -96,11 +102,11 @@ func ExampleWriteTo() { // Expect indices to be equal. if reflect.DeepEqual(idx, reReadIdx) { - fmt.Printf("Saved index file at %v matches the index embedded in CARv2 at %v.\n", dest, src) + fmt.Printf("Saved index file matches the index embedded in CARv2 at %v.\n", src) } else { panic("expected to get the same index as the CARv2 file") } // Output: - // Saved index file at ../testdata/sample-index.carindex matches the index embedded in CARv2 at ../testdata/sample-wrapped-v2.car. + // Saved index file matches the index embedded in CARv2 at ../testdata/sample-wrapped-v2.car. } -- GitLab