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