@cryptotaxi247 / kubo / commits / 440de64d9

refactor(fsrepo/test) extract assert

@jbenet will move in upcoming branch/PR

Brian Tiger Chow committed Jan 14, 2015 at 12:39 UTC 440de64d971b96eebb7ae409dff0f808a20d6cf8
2 files changed +58 -54
repo/fsrepo/assert/assert.go new
+21
@@ -0,0 +1,21 @@
1 +package assert
2 +
3 +import "testing"
4 +
5 +func Nil(err error, t *testing.T, msgs ...string) {
6 + if err != nil {
7 + t.Fatal(msgs, "error:", err)
8 + }
9 +}
10 +
11 +func True(v bool, t *testing.T, msgs ...string) {
12 + if !v {
13 + t.Fatal(msgs)
14 + }
15 +}
16 +
17 +func Err(err error, t *testing.T, msgs ...string) {
18 + if err == nil {
19 + t.Fatal(msgs, "error:", err)
20 + }
21 +}
repo/fsrepo/fsrepo_test.go
+37 -54
@@ -7,6 +7,7 @@ import (
7
8 datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 "github.com/jbenet/go-ipfs/repo/config"
10 + "github.com/jbenet/go-ipfs/repo/fsrepo/assert"
11 )
12
13 // swap arg order
@@ -22,35 +23,35 @@ func TestInitIdempotence(t *testing.T) {
23 t.Parallel()
24 path := testRepoPath("", t)
25 for i := 0; i < 10; i++ {
25 - AssertNil(Init(path, &config.Config{}), t, "multiple calls to init should succeed")
26 + assert.Nil(Init(path, &config.Config{}), t, "multiple calls to init should succeed")
27 }
28 }
29
30 func TestRemove(t *testing.T) {
31 t.Parallel()
32 path := testRepoPath("foo", t)
32 - AssertNil(Remove(path), t, "should be able to remove after closed")
33 + assert.Nil(Remove(path), t, "should be able to remove after closed")
34 }
35
36 func TestCannotRemoveIfOpen(t *testing.T) {
37 t.Parallel()
38 path := testRepoPath("TestCannotRemoveIfOpen", t)
38 - AssertNil(Init(path, &config.Config{}), t, "should initialize successfully")
39 + assert.Nil(Init(path, &config.Config{}), t, "should initialize successfully")
40 r := At(path)
40 - AssertNil(r.Open(), t)
41 - AssertErr(Remove(path), t, "should not be able to remove while open")
42 - AssertNil(r.Close(), t)
43 - AssertNil(Remove(path), t, "should be able to remove after closed")
41 + assert.Nil(r.Open(), t)
42 + assert.Err(Remove(path), t, "should not be able to remove while open")
43 + assert.Nil(r.Close(), t)
44 + assert.Nil(Remove(path), t, "should be able to remove after closed")
45 }
46
47 func TestCannotBeReopened(t *testing.T) {
48 t.Parallel()
49 path := testRepoPath("", t)
49 - AssertNil(Init(path, &config.Config{}), t)
50 + assert.Nil(Init(path, &config.Config{}), t)
51 r := At(path)
51 - AssertNil(r.Open(), t)
52 - AssertNil(r.Close(), t)
53 - AssertErr(r.Open(), t, "shouldn't be possible to re-open the repo")
52 + assert.Nil(r.Open(), t)
53 + assert.Nil(r.Close(), t)
54 + assert.Err(r.Open(), t, "shouldn't be possible to re-open the repo")
55
56 // mutable state is the enemy. Take Close() as an opportunity to reduce
57 // entropy. Callers ought to start fresh with a new handle by calling `At`.
@@ -62,83 +63,65 @@ func TestCanManageReposIndependently(t *testing.T) {
63 pathB := testRepoPath("b", t)
64
65 t.Log("initialize two repos")
65 - AssertNil(Init(pathA, &config.Config{}), t, "a", "should initialize successfully")
66 - AssertNil(Init(pathB, &config.Config{}), t, "b", "should initialize successfully")
66 + assert.Nil(Init(pathA, &config.Config{}), t, "a", "should initialize successfully")
67 + assert.Nil(Init(pathB, &config.Config{}), t, "b", "should initialize successfully")
68
69 t.Log("ensure repos initialized")
69 - Assert(IsInitialized(pathA), t, "a should be initialized")
70 - Assert(IsInitialized(pathB), t, "b should be initialized")
70 + assert.True(IsInitialized(pathA), t, "a should be initialized")
71 + assert.True(IsInitialized(pathB), t, "b should be initialized")
72
73 t.Log("open the two repos")
74 repoA := At(pathA)
75 repoB := At(pathB)
75 - AssertNil(repoA.Open(), t, "a")
76 - AssertNil(repoB.Open(), t, "b")
76 + assert.Nil(repoA.Open(), t, "a")
77 + assert.Nil(repoB.Open(), t, "b")
78
79 t.Log("close and remove b while a is open")
79 - AssertNil(repoB.Close(), t, "close b")
80 - AssertNil(Remove(pathB), t, "remove b")
80 + assert.Nil(repoB.Close(), t, "close b")
81 + assert.Nil(Remove(pathB), t, "remove b")
82
83 t.Log("close and remove a")
83 - AssertNil(repoA.Close(), t)
84 - AssertNil(Remove(pathA), t)
84 + assert.Nil(repoA.Close(), t)
85 + assert.Nil(Remove(pathA), t)
86 }
87
88 func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
89 t.Parallel()
90 path := testRepoPath("test", t)
91
91 - Assert(!IsInitialized(path), t, "should NOT be initialized")
92 - AssertNil(Init(path, &config.Config{}), t, "should initialize successfully")
92 + assert.True(!IsInitialized(path), t, "should NOT be initialized")
93 + assert.Nil(Init(path, &config.Config{}), t, "should initialize successfully")
94 r := At(path)
94 - AssertNil(r.Open(), t, "should open successfully")
95 + assert.Nil(r.Open(), t, "should open successfully")
96
97 k := "key"
98 data := []byte(k)
98 - AssertNil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")
99 + assert.Nil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")
100
100 - AssertNil(r.Close(), t)
101 + assert.Nil(r.Close(), t)
102 _, err := r.Datastore().Get(datastore.NewKey(k))
102 - AssertErr(err, t, "after closer, Get should be fail")
103 + assert.Err(err, t, "after closer, Get should be fail")
104 }
105
106 func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
107 t.Parallel()
108 path := testRepoPath("test", t)
109
109 - AssertNil(Init(path, &config.Config{}), t)
110 + assert.Nil(Init(path, &config.Config{}), t)
111 r1 := At(path)
111 - AssertNil(r1.Open(), t)
112 + assert.Nil(r1.Open(), t)
113
114 k := "key"
115 expected := []byte(k)
115 - AssertNil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
116 - AssertNil(r1.Close(), t)
116 + assert.Nil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
117 + assert.Nil(r1.Close(), t)
118
119 r2 := At(path)
119 - AssertNil(r2.Open(), t)
120 + assert.Nil(r2.Open(), t)
121 v, err := r2.Datastore().Get(datastore.NewKey(k))
121 - AssertNil(err, t, "using second repo, Get should be successful")
122 + assert.Nil(err, t, "using second repo, Get should be successful")
123 actual, ok := v.([]byte)
123 - Assert(ok, t, "value should be the []byte from r1's Put")
124 - AssertNil(r2.Close(), t)
125 - Assert(bytes.Compare(expected, actual) == 0, t, "data should match")
126 -}
127 -
128 -func AssertNil(err error, t *testing.T, msgs ...string) {
129 - if err != nil {
130 - t.Fatal(msgs, "error:", err)
131 - }
132 -}
133 -
134 -func Assert(v bool, t *testing.T, msgs ...string) {
135 - if !v {
136 - t.Fatal(msgs)
137 - }
138 -}
139 -
140 -func AssertErr(err error, t *testing.T, msgs ...string) {
141 - if err == nil {
142 - t.Fatal(msgs, "error:", err)
143 - }
124 + assert.True(ok, t, "value should be the []byte from r1's Put")
125 + assert.Nil(r2.Close(), t)
126 + assert.True(bytes.Compare(expected, actual) == 0, t, "data should match")
127 }