github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/hack/integration-cli-on-swarm/host/enumerate.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "regexp" 8 ) 9 10 var testFuncRegexp *regexp.Regexp 11 12 func init() { 13 testFuncRegexp = regexp.MustCompile(`(?m)^\s*func\s+\(\w*\s*\*(\w+Suite)\)\s+(Test\w+)`) 14 } 15 16 func enumerateTestsForBytes(b []byte) ([]string, error) { 17 var tests []string 18 submatches := testFuncRegexp.FindAllSubmatch(b, -1) 19 for _, submatch := range submatches { 20 if len(submatch) == 3 { 21 tests = append(tests, fmt.Sprintf("%s.%s$", submatch[1], submatch[2])) 22 } 23 } 24 return tests, nil 25 } 26 27 // enumerateTests enumerates valid `-check.f` strings for all the test functions. 28 // Note that we use regexp rather than parsing Go files for performance reason. 29 // (Try `TESTFLAGS=-check.list make test-integration` to see the slowness of parsing) 30 // The files needs to be `gofmt`-ed 31 // 32 // The result will be as follows, but unsorted ('$' is appended because they are regexp for `-check.f`): 33 // "DockerAuthzSuite.TestAuthZPluginAPIDenyResponse$" 34 // "DockerAuthzSuite.TestAuthZPluginAllowEventStream$" 35 // ... 36 // "DockerTrustedSwarmSuite.TestTrustedServiceUpdate$" 37 func enumerateTests(wd string) ([]string, error) { 38 testGoFiles, err := filepath.Glob(filepath.Join(wd, "integration-cli", "*_test.go")) 39 if err != nil { 40 return nil, err 41 } 42 var allTests []string 43 for _, testGoFile := range testGoFiles { 44 b, err := ioutil.ReadFile(testGoFile) 45 if err != nil { 46 return nil, err 47 } 48 tests, err := enumerateTestsForBytes(b) 49 if err != nil { 50 return nil, err 51 } 52 allTests = append(allTests, tests...) 53 } 54 return allTests, nil 55 }