Commit c3a59556 authored by Masih H. Derkani's avatar Masih H. Derkani Committed by Masih H. Derkani

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
parent c2e497e2
...@@ -3,6 +3,9 @@ package blockstore_test ...@@ -3,6 +3,9 @@ package blockstore_test
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os"
"path/filepath"
blocks "github.com/ipfs/go-block-format" blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
...@@ -79,10 +82,14 @@ func ExampleOpenReadWrite() { ...@@ -79,10 +82,14 @@ func ExampleOpenReadWrite() {
thatBlock := merkledag.NewRawNode([]byte("lobster")).Block thatBlock := merkledag.NewRawNode([]byte("lobster")).Block
andTheOtherBlock := merkledag.NewRawNode([]byte("barreleye")).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()} 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 { if err != nil {
panic(err) panic(err)
} }
...@@ -110,7 +117,7 @@ func ExampleOpenReadWrite() { ...@@ -110,7 +117,7 @@ func ExampleOpenReadWrite() {
// Resume from the same file to add more blocks. // Resume from the same file to add more blocks.
// Note the UseDataPadding and roots must match the values passed to the blockstore instance // 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. // 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 { if err != nil {
panic(err) panic(err)
} }
......
...@@ -570,11 +570,29 @@ func TestReadWriteWithPaddingWorksAsExpected(t *testing.T) { ...@@ -570,11 +570,29 @@ func TestReadWriteWithPaddingWorksAsExpected(t *testing.T) {
} }
func TestReadWriteResumptionFromNonV2FileIsError(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.EqualError(t, err, "cannot resume on CAR file with version 42")
require.Nil(t, subject) 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) { func TestReadWriteResumptionFromFileWithDifferentCarV1PaddingIsError(t *testing.T) {
oneTestBlockCid := oneTestBlockWithCidV1.Cid() oneTestBlockCid := oneTestBlockWithCidV1.Cid()
WantRoots := []cid.Cid{oneTestBlockCid} WantRoots := []cid.Cid{oneTestBlockCid}
......
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath"
carv2 "github.com/ipld/go-car/v2" carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore" "github.com/ipld/go-car/v2/blockstore"
...@@ -15,7 +17,11 @@ func ExampleWrapV1File() { ...@@ -15,7 +17,11 @@ func ExampleWrapV1File() {
// Writing the result to testdata allows reusing that file in other tests, // Writing the result to testdata allows reusing that file in other tests,
// and also helps ensure that the result is deterministic. // and also helps ensure that the result is deterministic.
src := "testdata/sample-v1.car" 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 { if err := carv2.WrapV1File(src, dst); err != nil {
panic(err) panic(err)
} }
......
...@@ -3,7 +3,9 @@ package index_test ...@@ -3,7 +3,9 @@ package index_test
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath"
"reflect" "reflect"
carv2 "github.com/ipld/go-car/v2" carv2 "github.com/ipld/go-car/v2"
...@@ -67,8 +69,12 @@ func ExampleWriteTo() { ...@@ -67,8 +69,12 @@ func ExampleWriteTo() {
} }
// Store the index alone onto destination file. // Store the index alone onto destination file.
dest := "../testdata/sample-index.carindex" tdir, err := ioutil.TempDir(os.TempDir(), "example-*")
f, err := os.Create(dest) if err != nil {
panic(err)
}
dst := filepath.Join(tdir, "index.carindex")
f, err := os.Create(dst)
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -96,11 +102,11 @@ func ExampleWriteTo() { ...@@ -96,11 +102,11 @@ func ExampleWriteTo() {
// Expect indices to be equal. // Expect indices to be equal.
if reflect.DeepEqual(idx, reReadIdx) { 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 { } else {
panic("expected to get the same index as the CARv2 file") panic("expected to get the same index as the CARv2 file")
} }
// Output: // 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.
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment