Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/gateway/car-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is an example that shows how to build a Gateway backed by the contents of
a CAR file. A [CAR file](https://ipld.io/specs/transport/car/) is a Content
Addressable aRchive that contains blocks.
Addressable archive that contains blocks.

The `main.go` sets up a `blockService` backed by a static CAR file,
and then uses it to initialize `gateway.NewBlocksBackend(blockService)`.
Expand All @@ -16,7 +16,7 @@ and then uses it to initialize `gateway.NewBlocksBackend(blockService)`.
## Usage

First of all, you will need some content stored as a CAR file. You can easily
export your favorite website, or content, using:
export your favorite website or content, using:

```
ipfs dag export <CID> > data.car
Expand Down
12 changes: 3 additions & 9 deletions files/filewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ func TestWriteTo(t *testing.T) {
"a": NewBytesFile([]byte("foobar")),
}),
})
tmppath, err := os.MkdirTemp("", "files-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmppath)
tmppath := t.TempDir()

path := filepath.Join(tmppath, "output")

err = WriteTo(sf, path)
err := WriteTo(sf, path)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -78,9 +74,7 @@ func TestWriteTo(t *testing.T) {
}

func TestDontAllowOverwrite(t *testing.T) {
tmppath, err := os.MkdirTemp("", "files-test")
assert.NoError(t, err)
defer os.RemoveAll(tmppath)
tmppath := t.TempDir()

path := filepath.Join(tmppath, "output")

Expand Down
4 changes: 1 addition & 3 deletions files/filewriter_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
)

func TestWriteToInvalidPaths(t *testing.T) {
tmppath, err := os.MkdirTemp("", "files-test")
assert.NoError(t, err)
defer os.RemoveAll(tmppath)
tmppath := t.TempDir()

path := filepath.Join(tmppath, "output")

Expand Down
4 changes: 1 addition & 3 deletions files/filewriter_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
)

func TestWriteToInvalidPaths(t *testing.T) {
tmppath, err := os.MkdirTemp("", "files-test")
assert.NoError(t, err)
defer os.RemoveAll(tmppath)
tmppath := t.TempDir()

path := filepath.Join(tmppath, "output")

Expand Down
5 changes: 1 addition & 4 deletions files/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func TestFileFilter(t *testing.T) {
if err == nil {
t.Errorf("creating a filter without an invalid ignore file path should have failed")
}
tmppath, err := os.MkdirTemp("", "filter-test")
if err != nil {
t.Fatal(err)
}
tmppath := t.TempDir()
ignoreFilePath := filepath.Join(tmppath, "ignoreFile")
ignoreFileContents := []byte("a.txt")
if err := os.WriteFile(ignoreFilePath, ignoreFileContents, 0o666); err != nil {
Expand Down
6 changes: 1 addition & 5 deletions files/serialfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ func TestSerialFile(t *testing.T) {
}

func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
tmppath, err := os.MkdirTemp("", "files-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmppath)
tmppath := t.TempDir()

testInputs := map[string]string{
"1": "Some text!\n",
Expand Down
27 changes: 11 additions & 16 deletions filestore/filestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ var bg = context.Background()
func newTestFilestore(t *testing.T, option ...Option) (string, *Filestore) {
mds := ds.NewMapDatastore()

testdir, err := os.MkdirTemp("", "filestore-test")
if err != nil {
t.Fatal(err)
}
testdir := t.TempDir()
fm := NewFileManager(mds, testdir, option...)
fm.AllowFiles = true

Expand All @@ -32,18 +29,22 @@ func newTestFilestore(t *testing.T, option ...Option) (string, *Filestore) {
return testdir, fstore
}

func makeFile(dir string, data []byte) (string, error) {
func makeFile(t *testing.T, dir string, data []byte) string {
t.Helper()
f, err := os.CreateTemp(dir, "file")
if err != nil {
return "", err
t.Fatal(err)
}
t.Cleanup(func() {
f.Close()
})

_, err = f.Write(data)
if err != nil {
return "", err
t.Fatal(err)
}

return f.Name(), nil
return f.Name()
}

func TestBasicFilestore(t *testing.T) {
Expand All @@ -62,10 +63,7 @@ func TestBasicFilestore(t *testing.T) {
buf := make([]byte, 1000)
rand.Read(buf)

fname, err := makeFile(dir, buf)
if err != nil {
t.Fatal(err)
}
fname := makeFile(t, dir, buf)

var cids []cid.Cid
for i := 0; i < 100; i++ {
Expand Down Expand Up @@ -122,10 +120,7 @@ func randomFileAdd(t *testing.T, fs *Filestore, dir string, size int) (string, [
buf := make([]byte, size)
rand.Read(buf)

fname, err := makeFile(dir, buf)
if err != nil {
t.Fatal(err)
}
fname := makeFile(t, dir, buf)

var out []cid.Cid
for i := 0; i < size/10; i++ {
Expand Down
1 change: 1 addition & 0 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (ks *FSKeystore) List() ([]string, error) {
if err != nil {
return nil, err
}
defer dir.Close()

dirs, err := dir.Readdirnames(0)
if err != nil {
Expand Down
17 changes: 3 additions & 14 deletions keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func privKeyOrFatal(t *testing.T) ci.PrivKey {
}

func TestKeystoreBasics(t *testing.T) {
tdir, err := os.MkdirTemp("", "keystore-test")
if err != nil {
t.Fatal(err)
}
tdir := t.TempDir()

ks, err := NewFSKeystore(tdir)
if err != nil {
Expand Down Expand Up @@ -140,12 +137,7 @@ func TestKeystoreBasics(t *testing.T) {
}

func TestInvalidKeyFiles(t *testing.T) {
tdir, err := os.MkdirTemp("", "keystore-test")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(tdir)
tdir := t.TempDir()

ks, err := NewFSKeystore(tdir)
if err != nil {
Expand Down Expand Up @@ -198,10 +190,7 @@ func TestInvalidKeyFiles(t *testing.T) {
}

func TestNonExistingKey(t *testing.T) {
tdir, err := os.MkdirTemp("", "keystore-test")
if err != nil {
t.Fatal(err)
}
tdir := t.TempDir()

ks, err := NewFSKeystore(tdir)
if err != nil {
Expand Down
48 changes: 36 additions & 12 deletions tar/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
fp "path/filepath"
"runtime"
Expand Down Expand Up @@ -56,10 +57,12 @@ func TestSingleFile(t *testing.T) {
func(t *testing.T, extractDir string) {
f, err := os.Open(fp.Join(extractDir, fileName))
assert.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, f.Close())
})
data, err := io.ReadAll(f)
assert.NoError(t, err)
assert.Equal(t, fileData, string(data))
assert.NoError(t, f.Close())
},
nil,
)
Expand All @@ -79,10 +82,12 @@ func TestSingleFileWithMeta(t *testing.T) {
testMeta(t, path, mode, mtime)
f, err := os.Open(path)
assert.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, f.Close())
})
data, err := io.ReadAll(f)
assert.NoError(t, err)
assert.Equal(t, fileData, string(data))
assert.NoError(t, f.Close())
},
nil,
)
Expand All @@ -96,9 +101,10 @@ func TestSingleDirectory(t *testing.T) {
},
func(t *testing.T, extractDir string) {
f, err := os.Open(extractDir)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
t.Cleanup(func() {
f.Close()
})
objs, err := f.Readdir(1)
if err == io.EOF && len(objs) == 0 {
return
Expand All @@ -123,6 +129,9 @@ func TestSingleDirectoryWithMeta(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
f.Close()
})
objs, err := f.Readdir(1)
if err == io.EOF && len(objs) == 0 {
return
Expand Down Expand Up @@ -491,6 +500,9 @@ func TestLastElementOverwrite(t *testing.T) {
// This file will reside outside of the extraction directory root.
f, err := os.Create(fp.Join(rootDir, "outside-ref"))
assert.NoError(t, err)
t.Cleanup(func() {
f.Close()
})
n, err := f.WriteString(originalData)
assert.NoError(t, err)
assert.Equal(t, len(originalData), n)
Expand All @@ -516,15 +528,15 @@ func TestLastElementOverwrite(t *testing.T) {
const tarOutRoot = "tar-out-root"

func testTarExtraction(t *testing.T, setup func(t *testing.T, rootDir string), tarEntries []tarEntry, check func(t *testing.T, extractDir string), extractError error) {
var err error

// Directory structure.
// FIXME: We can't easily work on a MemFS since we would need to replace
// all the `os` calls in the extractor so using a temporary dir.
rootDir, err := os.MkdirTemp("", "tar-extraction-test")
assert.NoError(t, err)
rootDir := t.TempDir()
t.Cleanup(func() {
chmodRecursive(t, rootDir)
})
extractDir := fp.Join(rootDir, tarOutRoot)
err = os.MkdirAll(extractDir, 0o755)
err := os.MkdirAll(extractDir, 0o755)
assert.NoError(t, err)

if setup != nil {
Expand All @@ -542,11 +554,23 @@ func testTarExtraction(t *testing.T, setup func(t *testing.T, rootDir string), t
}
}

func testExtract(t *testing.T, tarFile string, extractDir string, expectedError error) {
var err error
func chmodRecursive(t *testing.T, path string) {
t.Helper()
err := fp.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
return os.Chmod(path, fs.FileMode(0700))
})
if err != nil {
t.Log("ERROR:", err)
}
}

func testExtract(t *testing.T, tarFile string, extractDir string, expectedError error) {
tarReader, err := os.Open(tarFile)
assert.NoError(t, err)
defer tarReader.Close()

extractor := &Extractor{Path: extractDir}
err = extractor.Extract(tarReader)
Expand Down
Loading