CoreAPI: Don't panic when testing incomplete implementions
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@d7a89ddb0258cbc54631924688e6f23bc02709d5 This commit was moved from ipfs/boxo@9845df2662de30cb8d0030bd280e2b58247a0ef0
Łukasz Magiera committed
Jan 7, 2019 at 16:19 UTC
dfd6253ba8498af122a35929870657e5a8b29744
11 files changed
+116
-9
core/coreiface/tests/api.go
+16
@@ -2,12 +2,15 @@ package tests
2
3
import (
4
"context"
5
+ "errors"
6
"testing"
7
"time"
8
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
)
11
12
+var apiNotImplemented = errors.New("api not implemented")
13
+
14
func (tp *provider) makeAPI(ctx context.Context) (coreiface.CoreAPI, error) {
15
api, err := tp.MakeAPISwarm(ctx, false, 1)
16
if err != nil {
@@ -76,3 +79,16 @@ func TestApi(p Provider) func(t *testing.T) {
79
})
80
}
81
}
82
+
83
+func (tp *provider) hasApi(t *testing.T, tf func(coreiface.CoreAPI) error) {
84
+ ctx, cancel := context.WithCancel(context.Background())
85
+ defer cancel()
86
+ api, err := tp.makeAPI(ctx)
87
+ if err != nil {
88
+ t.Fatal(err)
89
+ }
90
+
91
+ if err := tf(api); err != nil {
92
+ t.Fatal(api)
93
+ }
94
+}
core/coreiface/tests/block.go
+7
@@ -13,6 +13,13 @@ import (
13
)
14
15
func (tp *provider) TestBlock(t *testing.T) {
16
+ tp.hasApi(t, func(api coreiface.CoreAPI) error {
17
+ if api.Block() == nil {
18
+ return apiNotImplemented
19
+ }
20
+ return nil
21
+ })
22
+
23
t.Run("TestBlockPut", tp.TestBlockPut)
24
t.Run("TestBlockPutFormat", tp.TestBlockPutFormat)
25
t.Run("TestBlockPutHash", tp.TestBlockPutHash)
core/coreiface/tests/dag.go
+7
@@ -13,6 +13,13 @@ import (
13
)
14
15
func (tp *provider) TestDag(t *testing.T) {
16
+ tp.hasApi(t, func(api coreiface.CoreAPI) error {
17
+ if api.Dag() == nil {
18
+ return apiNotImplemented
19
+ }
20
+ return nil
21
+ })
22
+
23
t.Run("TestPut", tp.TestPut)
24
t.Run("TestPutWithHash", tp.TestPutWithHash)
25
t.Run("TestPath", tp.TestDagPath)
core/coreiface/tests/dht.go
+8
@@ -5,10 +5,18 @@ import (
5
"io"
6
"testing"
7
8
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
9
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
)
11
12
func (tp *provider) TestDht(t *testing.T) {
13
+ tp.hasApi(t, func(api iface.CoreAPI) error {
14
+ if api.Dht() == nil {
15
+ return apiNotImplemented
16
+ }
17
+ return nil
18
+ })
19
+
20
t.Run("TestDhtFindPeer", tp.TestDhtFindPeer)
21
t.Run("TestDhtFindProviders", tp.TestDhtFindProviders)
22
t.Run("TestDhtProvide", tp.TestDhtProvide)
core/coreiface/tests/key.go
+8
@@ -5,10 +5,18 @@ import (
5
"strings"
6
"testing"
7
8
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
9
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
)
11
12
func (tp *provider) TestKey(t *testing.T) {
13
+ tp.hasApi(t, func(api iface.CoreAPI) error {
14
+ if api.Key() == nil {
15
+ return apiNotImplemented
16
+ }
17
+ return nil
18
+ })
19
+
20
t.Run("TestListSelf", tp.TestListSelf)
21
t.Run("TestRenameSelf", tp.TestRenameSelf)
22
t.Run("TestRemoveSelf", tp.TestRemoveSelf)
core/coreiface/tests/name.go
+7
@@ -16,6 +16,13 @@ import (
16
)
17
18
func (tp *provider) TestName(t *testing.T) {
19
+ tp.hasApi(t, func(api coreiface.CoreAPI) error {
20
+ if api.Name() == nil {
21
+ return apiNotImplemented
22
+ }
23
+ return nil
24
+ })
25
+
26
t.Run("TestPublishResolve", tp.TestPublishResolve)
27
t.Run("TestBasicPublishResolveKey", tp.TestBasicPublishResolveKey)
28
t.Run("TestBasicPublishResolveTimeout", tp.TestBasicPublishResolveTimeout)
core/coreiface/tests/object.go
+7
@@ -13,6 +13,13 @@ import (
13
)
14
15
func (tp *provider) TestObject(t *testing.T) {
16
+ tp.hasApi(t, func(api iface.CoreAPI) error {
17
+ if api.Object() == nil {
18
+ return apiNotImplemented
19
+ }
20
+ return nil
21
+ })
22
+
23
t.Run("TestNew", tp.TestNew)
24
t.Run("TestObjectPut", tp.TestObjectPut)
25
t.Run("TestObjectGet", tp.TestObjectGet)
core/coreiface/tests/path.go
+33
-9
@@ -26,23 +26,27 @@ func (tp *provider) TestMutablePath(t *testing.T) {
26
t.Fatal(err)
27
}
28
29
- // get self /ipns path
30
- keys, err := api.Key().List(ctx)
29
+ blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
30
if err != nil {
31
t.Fatal(err)
32
}
33
35
- if !keys[0].Path().Mutable() {
36
- t.Error("expected self /ipns path to be mutable")
34
+ if blk.Path().Mutable() {
35
+ t.Error("expected /ipld path to be immutable")
36
}
37
39
- blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
38
+ // get self /ipns path
39
+ if api.Key() == nil {
40
+ t.Fatal(".Key not implemented")
41
+ }
42
+
43
+ keys, err := api.Key().List(ctx)
44
if err != nil {
41
- t.Error(err)
45
+ t.Fatal(err)
46
}
47
44
- if blk.Path().Mutable() {
45
- t.Error("expected /ipld path to be immutable")
48
+ if !keys[0].Path().Mutable() {
49
+ t.Error("expected self /ipns path to be mutable")
50
}
51
}
52
@@ -54,6 +58,10 @@ func (tp *provider) TestPathRemainder(t *testing.T) {
58
t.Fatal(err)
59
}
60
61
+ if api.Dag() == nil {
62
+ t.Fatal(".Dag not implemented")
63
+ }
64
+
65
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
66
if err != nil {
67
t.Fatal(err)
@@ -82,6 +90,10 @@ func (tp *provider) TestEmptyPathRemainder(t *testing.T) {
90
t.Fatal(err)
91
}
92
93
+ if api.Dag() == nil {
94
+ t.Fatal(".Dag not implemented")
95
+ }
96
+
97
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
98
if err != nil {
99
t.Fatal(err)
@@ -114,6 +126,10 @@ func (tp *provider) TestInvalidPathRemainder(t *testing.T) {
126
t.Fatal(err)
127
}
128
129
+ if api.Dag() == nil {
130
+ t.Fatal(".Dag not implemented")
131
+ }
132
+
133
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
134
if err != nil {
135
t.Fatal(err)
@@ -138,9 +154,17 @@ func (tp *provider) TestPathRoot(t *testing.T) {
154
t.Fatal(err)
155
}
156
157
+ if api.Block() == nil {
158
+ t.Fatal(".Block not implemented")
159
+ }
160
+
161
blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw"))
162
if err != nil {
143
- t.Error(err)
163
+ t.Fatal(err)
164
+ }
165
+
166
+ if api.Dag() == nil {
167
+ t.Fatal(".Dag not implemented")
168
}
169
170
obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`))
core/coreiface/tests/pin.go
+8
@@ -2,6 +2,7 @@ package tests
2
3
import (
4
"context"
5
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
6
"strings"
7
"testing"
8
@@ -9,6 +10,13 @@ import (
10
)
11
12
func (tp *provider) TestPin(t *testing.T) {
13
+ tp.hasApi(t, func(api iface.CoreAPI) error {
14
+ if api.Pin() == nil {
15
+ return apiNotImplemented
16
+ }
17
+ return nil
18
+ })
19
+
20
t.Run("TestPinAdd", tp.TestPinAdd)
21
t.Run("TestPinSimple", tp.TestPinSimple)
22
t.Run("TestPinRecursive", tp.TestPinRecursive)
core/coreiface/tests/pubsub.go
+8
@@ -2,12 +2,20 @@ package tests
2
3
import (
4
"context"
5
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
6
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
7
"testing"
8
"time"
9
)
10
11
func (tp *provider) TestPubSub(t *testing.T) {
12
+ tp.hasApi(t, func(api iface.CoreAPI) error {
13
+ if api.PubSub() == nil {
14
+ return apiNotImplemented
15
+ }
16
+ return nil
17
+ })
18
+
19
t.Run("TestBasicPubSub", tp.TestBasicPubSub)
20
}
21
core/coreiface/tests/unixfs.go
+7
@@ -24,6 +24,13 @@ import (
24
)
25
26
func (tp *provider) TestUnixfs(t *testing.T) {
27
+ tp.hasApi(t, func(api coreiface.CoreAPI) error {
28
+ if api.Unixfs() == nil {
29
+ return apiNotImplemented
30
+ }
31
+ return nil
32
+ })
33
+
34
t.Run("TestAdd", tp.TestAdd)
35
t.Run("TestAddPinned", tp.TestAddPinned)
36
t.Run("TestAddHashOnly", tp.TestAddHashOnly)