github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fstest/test_all/run_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os/exec" 6 "regexp" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestTestsToRegexp(t *testing.T) { 14 for _, test := range []struct { 15 in []string 16 want string 17 }{ 18 { 19 in: []string{}, 20 want: "", 21 }, 22 { 23 in: []string{"TestOne"}, 24 want: "^TestOne$", 25 }, 26 { 27 in: []string{"TestOne", "TestTwo"}, 28 want: "^(TestOne|TestTwo)$", 29 }, 30 { 31 in: []string{"TestOne", "TestTwo", "TestThree"}, 32 want: "^(TestOne|TestThree|TestTwo)$", 33 }, 34 { 35 in: []string{"TestOne/Sub1"}, 36 want: "^TestOne$/^Sub1$", 37 }, 38 { 39 in: []string{ 40 "TestOne/Sub1", 41 "TestTwo", 42 }, 43 want: "^(TestOne|TestTwo)$/^Sub1$", 44 }, 45 { 46 in: []string{ 47 "TestOne/Sub1", 48 "TestOne/Sub2", 49 "TestTwo", 50 }, 51 want: "^(TestOne|TestTwo)$/^(Sub1|Sub2)$", 52 }, 53 { 54 in: []string{ 55 "TestOne/Sub1", 56 "TestOne/Sub2/SubSub1", 57 "TestTwo", 58 }, 59 want: "^(TestOne|TestTwo)$/^(Sub1|Sub2)$/^SubSub1$", 60 }, 61 { 62 in: []string{ 63 "TestTests/A1", 64 "TestTests/B/B1", 65 "TestTests/C/C3/C31", 66 }, 67 want: "^TestTests$/^(A1|B|C)$/^(B1|C3)$/^C31$", 68 }, 69 } { 70 got := testsToRegexp(test.in) 71 assert.Equal(t, test.want, got, fmt.Sprintf("in=%v want=%q got=%q", test.in, test.want, got)) 72 } 73 } 74 75 var runRe = regexp.MustCompile(`(?m)^\s*=== RUN\s*(Test.*?)\s*$`) 76 77 // Test the regexp work with the -run flag in actually selecting the right tests 78 func TestTestsToRegexpLive(t *testing.T) { 79 for _, test := range []struct { 80 in []string 81 want []string 82 }{ 83 { 84 in: []string{ 85 "TestTests/A1", 86 "TestTests/C/C3", 87 }, 88 want: []string{ 89 "TestTests", 90 "TestTests/A1", 91 "TestTests/C", 92 "TestTests/C/C3", 93 "TestTests/C/C3/C31", 94 "TestTests/C/C3/C32", 95 }, 96 }, 97 { 98 in: []string{ 99 "TestTests", 100 "TestTests/A1", 101 "TestTests/B", 102 "TestTests/B/B1", 103 "TestTests/C", 104 }, 105 want: []string{ 106 "TestTests", 107 "TestTests/A1", 108 "TestTests/B", 109 "TestTests/B/B1", 110 "TestTests/C", 111 // FIXME there doesn't seem to be a way to select a non-terminal test 112 // and all of its subtests if there is a longer path (in this case B/B1) 113 // "TestTests/C/C1", 114 // "TestTests/C/C2", 115 // "TestTests/C/C3", 116 // "TestTests/C/C3/C31", 117 // "TestTests/C/C3/C32", 118 }, 119 }, 120 { 121 in: []string{ 122 "TestTests/A1", 123 "TestTests/B/B1", 124 "TestTests/C/C3/C31", 125 }, 126 want: []string{ 127 "TestTests", 128 "TestTests/A1", 129 "TestTests/B", 130 "TestTests/B/B1", 131 "TestTests/C", 132 "TestTests/C/C3", 133 "TestTests/C/C3/C31", 134 }, 135 }, 136 { 137 in: []string{ 138 "TestTests/B/B1", 139 "TestTests/C/C3/C31", 140 }, 141 want: []string{ 142 "TestTests", 143 "TestTests/B", 144 "TestTests/B/B1", 145 "TestTests/C", 146 "TestTests/C/C3", 147 "TestTests/C/C3/C31", 148 }, 149 }, 150 } { 151 runRegexp := testsToRegexp(test.in) 152 cmd := exec.Command("go", "test", "-v", "-run", runRegexp) 153 out, err := cmd.CombinedOutput() 154 require.NoError(t, err) 155 var got []string 156 for _, match := range runRe.FindAllSubmatch(out, -1) { 157 got = append(got, string(match[1])) 158 } 159 assert.Equal(t, test.want, got, fmt.Sprintf("in=%v want=%v got=%v, runRegexp=%q", test.in, test.want, got, runRegexp)) 160 } 161 } 162 163 var nilTest = func(t *testing.T) {} 164 165 // Nested tests for TestTestsToRegexpLive to run 166 func TestTests(t *testing.T) { 167 t.Run("A1", nilTest) 168 t.Run("A2", nilTest) 169 t.Run("B", func(t *testing.T) { 170 t.Run("B1", nilTest) 171 t.Run("B2", nilTest) 172 }) 173 t.Run("C", func(t *testing.T) { 174 t.Run("C1", nilTest) 175 t.Run("C2", nilTest) 176 t.Run("C3", func(t *testing.T) { 177 t.Run("C31", nilTest) 178 t.Run("C32", nilTest) 179 }) 180 }) 181 }