enumerate_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. "testing"
  9. )
  10. func getRepoTopDir(t *testing.T) string {
  11. wd, err := os.Getwd()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. wd = filepath.Clean(wd)
  16. suffix := "hack/integration-cli-on-swarm/host"
  17. if !strings.HasSuffix(wd, suffix) {
  18. t.Skipf("cwd seems strange (needs to have suffix %s): %v", suffix, wd)
  19. }
  20. return filepath.Clean(filepath.Join(wd, "../../.."))
  21. }
  22. func TestEnumerateTests(t *testing.T) {
  23. if testing.Short() {
  24. t.Skip("skipping in short mode")
  25. }
  26. tests, err := enumerateTests(getRepoTopDir(t))
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. sort.Strings(tests)
  31. t.Logf("enumerated %d test filter strings:", len(tests))
  32. for _, s := range tests {
  33. t.Logf("- %q", s)
  34. }
  35. }
  36. func TestEnumerateTestsForBytes(t *testing.T) {
  37. b := []byte(`package main
  38. import (
  39. "github.com/go-check/check"
  40. )
  41. func (s *FooSuite) TestA(c *check.C) {
  42. }
  43. func (s *FooSuite) TestAAA(c *check.C) {
  44. }
  45. func (s *BarSuite) TestBar(c *check.C) {
  46. }
  47. func (x *FooSuite) TestC(c *check.C) {
  48. }
  49. func (*FooSuite) TestD(c *check.C) {
  50. }
  51. // should not be counted
  52. func (s *FooSuite) testE(c *check.C) {
  53. }
  54. // counted, although we don't support ungofmt file
  55. func (s *FooSuite) TestF (c *check.C){}
  56. `)
  57. expected := []string{
  58. "FooSuite.TestA$",
  59. "FooSuite.TestAAA$",
  60. "BarSuite.TestBar$",
  61. "FooSuite.TestC$",
  62. "FooSuite.TestD$",
  63. "FooSuite.TestF$",
  64. }
  65. actual, err := enumerateTestsForBytes(b)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if !reflect.DeepEqual(expected, actual) {
  70. t.Fatalf("expected %q, got %q", expected, actual)
  71. }
  72. }