Skip to content

Commit c5d6fcb

Browse files
committed
Return error up stack instead of passing testing var down
1 parent 2455462 commit c5d6fcb

7 files changed

Lines changed: 159 additions & 84 deletions

File tree

‎tests/block_test.go‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,38 @@ var blockTestDir = filepath.Join(baseDir, "BlockTests")
1010

1111
// TODO: refactor test setup & execution to better align with vm and tx tests
1212
func TestBcValidBlockTests(t *testing.T) {
13-
// SimpleTx3 genesis block does not validate against calculated state root
14-
// as of 2015-06-09. unskip once working /Gustav
15-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcValidBlockTest.json"), []string{"SimpleTx3"}, t)
13+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcValidBlockTest.json"), []string{"SimpleTx3"})
1614
}
1715

1816
func TestBcUncleTests(t *testing.T) {
19-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcUncleTest.json"), []string{}, t)
20-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcBruncleTest.json"), []string{}, t)
17+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcUncleTest.json"), []string{})
18+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcBruncleTest.json"), []string{})
2119
}
2220

2321
func TestBcUncleHeaderValidityTests(t *testing.T) {
24-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcUncleHeaderValiditiy.json"), []string{}, t)
22+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcUncleHeaderValiditiy.json"), []string{})
2523
}
2624

2725
func TestBcInvalidHeaderTests(t *testing.T) {
28-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcInvalidHeaderTest.json"), []string{}, t)
26+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcInvalidHeaderTest.json"), []string{})
2927
}
3028

3129
func TestBcInvalidRLPTests(t *testing.T) {
32-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcInvalidRLPTest.json"), []string{}, t)
30+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcInvalidRLPTest.json"), []string{})
3331
}
3432

3533
func TestBcRPCAPITests(t *testing.T) {
36-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcRPC_API_Test.json"), []string{}, t)
34+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcRPC_API_Test.json"), []string{})
3735
}
3836

3937
func TestBcForkBlockTests(t *testing.T) {
40-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcForkBlockTest.json"), []string{}, t)
38+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcForkBlockTest.json"), []string{})
4139
}
4240

4341
func TestBcTotalDifficulty(t *testing.T) {
44-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcTotalDifficultyTest.json"), []string{}, t)
42+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcTotalDifficultyTest.json"), []string{})
4543
}
4644

4745
func TestBcWallet(t *testing.T) {
48-
runBlockTestsInFile(filepath.Join(blockTestDir, "bcWalletTest.json"), []string{}, t)
46+
runBlockTestsInFile(filepath.Join(blockTestDir, "bcWalletTest.json"), []string{})
4947
}

‎tests/block_test_util.go‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"runtime"
1212
"strconv"
1313
"strings"
14-
"testing"
1514
"time"
1615

1716
"github.com/ethereum/go-ethereum/accounts"
@@ -87,10 +86,10 @@ type btTransaction struct {
8786
Value string
8887
}
8988

90-
func runBlockTestsInFile(filepath string, snafus []string, t *testing.T) {
89+
func runBlockTestsInFile(filepath string, snafus []string) error {
9190
bt, err := LoadBlockTests(filepath)
9291
if err != nil {
93-
t.Fatal(err)
92+
return nil
9493
}
9594

9695
notWorking := make(map[string]bool, 100)
@@ -100,21 +99,24 @@ func runBlockTestsInFile(filepath string, snafus []string, t *testing.T) {
10099

101100
for name, test := range bt {
102101
if !notWorking[name] {
103-
runBlockTest(name, test, t)
102+
if err := runBlockTest(name, test); err != nil {
103+
return err
104+
}
104105
}
105106
}
107+
return nil
106108
}
107109

108-
func runBlockTest(name string, test *BlockTest, t *testing.T) {
110+
func runBlockTest(name string, test *BlockTest) error {
109111
cfg := testEthConfig()
110112
ethereum, err := eth.New(cfg)
111113
if err != nil {
112-
t.Fatalf("%v", err)
114+
return err
113115
}
114116

115117
err = ethereum.Start()
116118
if err != nil {
117-
t.Fatalf("%v", err)
119+
return err
118120
}
119121

120122
// import the genesis block
@@ -123,19 +125,20 @@ func runBlockTest(name string, test *BlockTest, t *testing.T) {
123125
// import pre accounts
124126
statedb, err := test.InsertPreState(ethereum)
125127
if err != nil {
126-
t.Fatalf("InsertPreState: %v", err)
128+
return fmt.Errorf("InsertPreState: %v", err)
127129
}
128130

129131
err = test.TryBlocksInsert(ethereum.ChainManager())
130132
if err != nil {
131-
t.Fatal(err)
133+
return err
132134
}
133135

134136
if err = test.ValidatePostState(statedb); err != nil {
135-
t.Fatal("post state validation failed: %v", err)
137+
return fmt.Errorf("post state validation failed: %v", err)
136138
}
137139
fmt.Println("Block test passed: ", name)
138140
// t.Log("Block test passed: ", name)
141+
return nil
139142
}
140143

141144
func testEthConfig() *eth.Config {

‎tests/init.go‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package tests
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"fmt"
57
"io"
68
"io/ioutil"
79
// "log"
810
"net/http"
911
"os"
10-
"testing"
1112

1213
// logpkg "github.com/ethereum/go-ethereum/logger"
1314
)
@@ -20,34 +21,40 @@ import (
2021
// logpkg.AddLogSystem(Logger)
2122
// }
2223

23-
func readJSON(t *testing.T, reader io.Reader, value interface{}) {
24+
func readJSON(reader io.Reader, value interface{}) error {
2425
data, err := ioutil.ReadAll(reader)
2526
err = json.Unmarshal(data, &value)
2627
if err != nil {
27-
t.Error(err)
28+
return err
2829
}
30+
return nil
2931
}
3032

31-
func CreateHttpTests(t *testing.T, uri string, value interface{}) {
33+
func CreateHttpTests(uri string, value interface{}) error {
3234
resp, err := http.Get(uri)
3335
if err != nil {
34-
t.Error(err)
35-
36-
return
36+
return err
3737
}
3838
defer resp.Body.Close()
3939

40-
readJSON(t, resp.Body, value)
40+
err = readJSON(resp.Body, value)
41+
if err != nil {
42+
return err
43+
}
44+
return nil
4145
}
4246

43-
func CreateFileTests(t *testing.T, fn string, value interface{}) {
47+
func CreateFileTests(fn string, value interface{}) error {
4448
file, err := os.Open(fn)
4549
if err != nil {
46-
t.Error(err)
47-
48-
return
50+
return err
4951
}
5052
defer file.Close()
5153

52-
readJSON(t, file, value)
54+
err = readJSON(file, value)
55+
if err != nil {
56+
return err
57+
}
58+
return nil
59+
}
5360
}

‎tests/state_test.go‎

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,93 +10,127 @@ var stateTestDir = filepath.Join(baseDir, "StateTests")
1010

1111
func TestStateSystemOperations(t *testing.T) {
1212
fn := filepath.Join(stateTestDir, "stSystemOperationsTest.json")
13-
RunStateTest(fn, t)
13+
if err := RunStateTest(fn); err != nil {
14+
t.Error(err)
15+
}
1416
}
1517

1618
func TestStateExample(t *testing.T) {
1719
fn := filepath.Join(stateTestDir, "stExample.json")
18-
RunStateTest(fn, t)
20+
if err := RunStateTest(fn); err != nil {
21+
t.Error(err)
22+
}
1923
}
2024

2125
func TestStatePreCompiledContracts(t *testing.T) {
2226
fn := filepath.Join(stateTestDir, "stPreCompiledContracts.json")
23-
RunStateTest(fn, t)
27+
if err := RunStateTest(fn); err != nil {
28+
t.Error(err)
29+
}
2430
}
2531

2632
func TestStateRecursiveCreate(t *testing.T) {
2733
fn := filepath.Join(stateTestDir, "stRecursiveCreate.json")
28-
RunStateTest(fn, t)
34+
if err := RunStateTest(fn); err != nil {
35+
t.Error(err)
36+
}
2937
}
3038

3139
func TestStateSpecial(t *testing.T) {
3240
fn := filepath.Join(stateTestDir, "stSpecialTest.json")
33-
RunStateTest(fn, t)
41+
if err := RunStateTest(fn); err != nil {
42+
t.Error(err)
43+
}
3444
}
3545

3646
func TestStateRefund(t *testing.T) {
3747
fn := filepath.Join(stateTestDir, "stRefundTest.json")
38-
RunStateTest(fn, t)
48+
if err := RunStateTest(fn); err != nil {
49+
t.Error(err)
50+
}
3951
}
4052

4153
func TestStateBlockHash(t *testing.T) {
4254
fn := filepath.Join(stateTestDir, "stBlockHashTest.json")
43-
RunStateTest(fn, t)
55+
if err := RunStateTest(fn); err != nil {
56+
t.Error(err)
57+
}
4458
}
4559

4660
func TestStateInitCode(t *testing.T) {
4761
fn := filepath.Join(stateTestDir, "stInitCodeTest.json")
48-
RunStateTest(fn, t)
62+
if err := RunStateTest(fn); err != nil {
63+
t.Error(err)
64+
}
4965
}
5066

5167
func TestStateLog(t *testing.T) {
5268
fn := filepath.Join(stateTestDir, "stLogTests.json")
53-
RunStateTest(fn, t)
69+
if err := RunStateTest(fn); err != nil {
70+
t.Error(err)
71+
}
5472
}
5573

5674
func TestStateTransaction(t *testing.T) {
5775
fn := filepath.Join(stateTestDir, "stTransactionTest.json")
58-
RunStateTest(fn, t)
76+
if err := RunStateTest(fn); err != nil {
77+
t.Error(err)
78+
}
5979
}
6080

6181
func TestCallCreateCallCode(t *testing.T) {
6282
fn := filepath.Join(stateTestDir, "stCallCreateCallCodeTest.json")
63-
RunStateTest(fn, t)
83+
if err := RunStateTest(fn); err != nil {
84+
t.Error(err)
85+
}
6486
}
6587

6688
func TestMemory(t *testing.T) {
6789
fn := filepath.Join(stateTestDir, "stMemoryTest.json")
68-
RunStateTest(fn, t)
90+
if err := RunStateTest(fn); err != nil {
91+
t.Error(err)
92+
}
6993
}
7094

7195
func TestMemoryStress(t *testing.T) {
7296
if os.Getenv("TEST_VM_COMPLEX") == "" {
7397
t.Skip()
7498
}
7599
fn := filepath.Join(stateTestDir, "stMemoryStressTest.json")
76-
RunStateTest(fn, t)
100+
if err := RunStateTest(fn); err != nil {
101+
t.Error(err)
102+
}
77103
}
78104

79105
func TestQuadraticComplexity(t *testing.T) {
80106
if os.Getenv("TEST_VM_COMPLEX") == "" {
81107
t.Skip()
82108
}
83109
fn := filepath.Join(stateTestDir, "stQuadraticComplexityTest.json")
84-
RunStateTest(fn, t)
110+
if err := RunStateTest(fn); err != nil {
111+
t.Error(err)
112+
}
85113
}
86114

87115
func TestSolidity(t *testing.T) {
88116
fn := filepath.Join(stateTestDir, "stSolidityTest.json")
89-
RunStateTest(fn, t)
117+
if err := RunStateTest(fn); err != nil {
118+
t.Error(err)
119+
}
90120
}
91121

92122
func TestWallet(t *testing.T) {
93123
fn := filepath.Join(stateTestDir, "stWalletTest.json")
94-
RunStateTest(fn, t)
124+
if err := RunStateTest(fn); err != nil {
125+
t.Error(err)
126+
}
95127
}
96128

97129
func TestStateTestsRandom(t *testing.T) {
98130
fns, _ := filepath.Glob("./files/StateTests/RandomTests/*")
99131
for _, fn := range fns {
100-
RunStateTest(fn, t)
132+
if err := RunStateTest(fn); err != nil {
133+
t.Error(err)
134+
}
101135
}
102136
}

0 commit comments

Comments
 (0)