2222package main
2323
2424import (
25+ "fmt"
2526 "io/ioutil"
2627 "os"
28+ "path/filepath"
2729
30+ "github.com/codegangsta/cli"
2831 "github.com/ethereum/go-ethereum/logger/glog"
2932 "github.com/ethereum/go-ethereum/tests"
3033)
3134
35+ var (
36+ continueOnError = false
37+ testExtension = ".json"
38+ defaultTest = "all"
39+ defaultDir = "."
40+ allTests = []string {"BlockTests" , "StateTests" , "TransactionTests" , "VMTests" }
41+
42+ TestFlag = cli.StringFlag {
43+ Name : "test" ,
44+ Usage : "Test type (string): VMTests, TransactionTests, StateTests, BlockTests" ,
45+ Value : defaultTest ,
46+ }
47+ FileFlag = cli.StringFlag {
48+ Name : "file" ,
49+ Usage : "Test file or directory. Directories are searched for .json files 1 level deep" ,
50+ Value : defaultDir ,
51+ EnvVar : "ETHEREUM_TEST_PATH" ,
52+ }
53+ ContinueOnErrorFlag = cli.BoolFlag {
54+ Name : "continue" ,
55+ Usage : "Continue running tests on error (true) or exit immediately (false)" ,
56+ }
57+ )
58+
59+ func runTest (test , file string ) error {
60+ // glog.Infoln("runTest", test, file)
61+ var err error
62+ switch test {
63+ case "bc" , "BlockTest" , "BlockTests" , "BlockChainTest" :
64+ err = tests .RunBlockTest (file )
65+ case "st" , "state" , "StateTest" , "StateTests" :
66+ err = tests .RunStateTest (file )
67+ case "tx" , "TransactionTest" , "TransactionTests" :
68+ err = tests .RunTransactionTests (file )
69+ case "vm" , "VMTest" , "VMTests" :
70+ err = tests .RunVmTest (file )
71+ default :
72+ err = fmt .Errorf ("Invalid test type specified:" , test )
73+ }
74+ return err
75+ }
76+
3277func getFiles (path string ) ([]string , error ) {
78+ // glog.Infoln("getFiles ", path)
3379 var files []string
3480 f , err := os .Open (path )
3581 if err != nil {
@@ -48,8 +94,9 @@ func getFiles(path string) ([]string, error) {
4894 files = make ([]string , len (fi ))
4995 for i , v := range fi {
5096 // only go 1 depth and leave directory entires blank
51- if ! v .IsDir () {
52- files [i ] = path + v .Name ()
97+ if ! v .IsDir () && v .Name ()[len (v .Name ())- len (testExtension ):len (v .Name ())] == testExtension {
98+ files [i ] = filepath .Join (path , v .Name ())
99+ // glog.Infoln(files[i])
53100 }
54101 }
55102 case mode .IsRegular ():
@@ -60,54 +107,75 @@ func getFiles(path string) ([]string, error) {
60107 return files , nil
61108}
62109
63- func main ( ) {
64- glog . SetToStderr ( true )
65- var continueOnError bool = false
66- // vm.Debug = true
110+ func runSuite ( c * cli. Context ) {
111+ flagTest := c . GlobalString ( TestFlag . Name )
112+ flagFile := c . GlobalString ( FileFlag . Name )
113+ continueOnError = c . GlobalBool ( ContinueOnErrorFlag . Name )
67114
68- if len (os .Args ) < 2 {
69- glog .Exit ("Must specify test type" )
70- }
115+ var tests []string
71116
72- testtype := os . Args [ 1 ]
73- var pattern string
74- if len ( os . Args ) > 2 {
75- pattern = os . Args [ 2 ]
117+ if flagTest == defaultTest {
118+ tests = allTests
119+ } else {
120+ tests = [] string { flagTest }
76121 }
77122
78- files , err := getFiles (pattern )
79- if err != nil {
80- glog .Fatal (err )
81- }
123+ for _ , curTest := range tests {
124+ // glog.Infoln("runSuite", curTest, flagFile)
125+ var err error
126+ var files []string
127+ if flagTest == defaultTest {
128+ files , err = getFiles (filepath .Join (flagFile , curTest ))
82129
83- for _ , testfile := range files {
84- // Skip blank entries
85- if len (testfile ) == 0 {
86- continue
130+ } else {
131+ files , err = getFiles (flagFile )
87132 }
88- // TODO allow io.Reader to be passed so Stdin can be piped
89- // RunVmTest(strings.NewReader(os.Args[2]))
90- // RunVmTest(os.Stdin)
91- var err error
92- switch testtype {
93- case "vm" , "VMTests" :
94- err = tests .RunVmTest (testfile )
95- case "state" , "StateTest" :
96- err = tests .RunStateTest (testfile )
97- case "tx" , "TransactionTests" :
98- err = tests .RunTransactionTests (testfile )
99- case "bc" , "BlockChainTest" :
100- err = tests .RunBlockTest (testfile )
101- default :
102- glog .Fatalln ("Invalid test type specified" )
133+ if err != nil {
134+ glog .Fatalln (err )
103135 }
104136
105- if err != nil {
106- if continueOnError {
107- glog .Errorln (err )
108- } else {
109- glog .Fatalln (err )
137+ if len (files ) == 0 {
138+ glog .Warningln ("No files matched path" )
139+ }
140+ for _ , testfile := range files {
141+ // Skip blank entries
142+ if len (testfile ) == 0 {
143+ continue
144+ }
145+
146+ // TODO allow io.Reader to be passed so Stdin can be piped
147+ // RunVmTest(strings.NewReader(os.Args[2]))
148+ // RunVmTest(os.Stdin)
149+ err := runTest (curTest , testfile )
150+ if err != nil {
151+ if continueOnError {
152+ glog .Errorln (err )
153+ } else {
154+ glog .Fatalln (err )
155+ }
110156 }
157+
111158 }
112159 }
113160}
161+
162+ func main () {
163+ glog .SetToStderr (true )
164+
165+ // vm.Debug = true
166+
167+ app := cli .NewApp ()
168+ app .Name = "ethtest"
169+ app .Usage = "go-ethereum test interface"
170+ app .Action = runSuite
171+ app .Flags = []cli.Flag {
172+ TestFlag ,
173+ FileFlag ,
174+ ContinueOnErrorFlag ,
175+ }
176+
177+ if err := app .Run (os .Args ); err != nil {
178+ glog .Fatalln (err )
179+ }
180+
181+ }
0 commit comments