k8s.io/kubernetes@v1.29.3/test/list/main_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "errors" 21 "path/filepath" 22 "reflect" 23 "testing" 24 ) 25 26 var collectCases = []struct { 27 filename string 28 code string 29 output []Test 30 }{ 31 // Empty: no tests 32 {"e2e/util_test.go", "", []Test{}}, 33 // Go unit test 34 {"test/list/main_test.go", ` 35 var num = 3 36 func Helper(x int) { return x / 0 } 37 func TestStuff(t *Testing.T) { 38 }`, []Test{{"test/list/main_test.go:5:1", "k8s.io/kubernetes/test/list", "TestStuff"}}, 39 }, 40 // Describe + It 41 {"e2e/foo.go", ` 42 var _ = Describe("Feature", func() { 43 It("should work properly", func() {}) 44 })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "should work properly"}}, 45 }, 46 // SIGDescribe + It 47 {"e2e/foo.go", ` 48 var _ = SIGDescribe("Feature", func() { 49 It("should work properly", func() {}) 50 })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "should work properly"}}, 51 }, 52 // SIGDescribe + Context + It 53 {"e2e/foo.go", ` 54 var _ = SIGDescribe("Feature", func() { 55 Context("when offline", func() { 56 It("should work", func() {}) 57 }) 58 })`, []Test{{"e2e/foo.go:5:3", "[k8s.io] Feature when offline", "should work"}}, 59 }, 60 // SIGDescribe + It(Sprintf) 61 {"e2e/foo.go", ` 62 var _ = SIGDescribe("Feature", func() { 63 It(fmt.Sprintf("handles %d nodes", num), func() {}) 64 })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "handles * nodes"}}, 65 }, 66 // SIGDescribe + Sprintf + It(var) 67 {"e2e/foo.go", ` 68 var _ = SIGDescribe("Feature", func() { 69 arg := fmt.Sprintf("does %s and %v at %d", task, desc, num) 70 It(arg, func() {}) 71 })`, []Test{{"e2e/foo.go:5:2", "[k8s.io] Feature", "does * and * at *"}}, 72 }, 73 // SIGDescribe + string + It(var) 74 {"e2e/foo.go", ` 75 var _ = SIGDescribe("Feature", func() { 76 arg := "does stuff" 77 It(arg, func() {}) 78 })`, []Test{{"e2e/foo.go:5:2", "[k8s.io] Feature", "does stuff"}}, 79 }, 80 // SIGDescribe + It(unknown) 81 {"e2e/foo.go", ` 82 var _ = SIGDescribe("Feature", func() { 83 It(mysteryFunc(), func() {}) 84 })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "*"}}, 85 }, 86 } 87 88 func TestCollect(t *testing.T) { 89 for _, test := range collectCases { 90 code := "package test\n" + test.code 91 tests := collect(test.filename, code) 92 if !reflect.DeepEqual(tests, test.output) { 93 t.Errorf("code:\n%s\ngot %v\nwant %v", 94 code, tests, test.output) 95 } 96 } 97 } 98 99 func TestHandlePath(t *testing.T) { 100 tl := testList{} 101 e := errors.New("ex") 102 if tl.handlePath("foo", nil, e) != e { 103 t.Error("handlePath not returning errors") 104 } 105 if tl.handlePath("foo.txt", nil, nil) != nil { 106 t.Error("should skip random files") 107 } 108 if tl.handlePath("third_party/a_test.go", nil, nil) != filepath.SkipDir { 109 t.Error("should skip third_party") 110 } 111 }