enumerate.go 1.5 KB

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