github.com/rigado/snapd@v2.42.5-go-mod+incompatible/sanity/check_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package sanity_test 21 22 import ( 23 "go/ast" 24 "go/parser" 25 "go/token" 26 "path/filepath" 27 "reflect" 28 "runtime" 29 "sort" 30 "strings" 31 "testing" 32 33 . "gopkg.in/check.v1" 34 35 "github.com/snapcore/snapd/sanity" 36 ) 37 38 // Hook up check.v1 into the "go test" runner 39 func Test(t *testing.T) { TestingT(t) } 40 41 type sanitySuite struct{} 42 43 var _ = Suite(&sanitySuite{}) 44 45 func (s *sanitySuite) TestRunHappy(c *C) { 46 var happyChecks []func() error 47 var happyCheckRan int 48 49 happyChecks = append(happyChecks, func() error { 50 happyCheckRan += 1 51 return nil 52 }) 53 54 restore := sanity.MockChecks(happyChecks) 55 defer restore() 56 57 err := sanity.Check() 58 c.Check(err, IsNil) 59 c.Check(happyCheckRan, Equals, 1) 60 } 61 62 func (s *sanitySuite) TestRunNotHappy(c *C) { 63 var unhappyChecks []func() error 64 var unhappyCheckRan int 65 66 unhappyChecks = append(unhappyChecks, func() error { 67 unhappyCheckRan += 1 68 return nil 69 }) 70 71 restore := sanity.MockChecks(unhappyChecks) 72 defer restore() 73 74 err := sanity.Check() 75 c.Check(err, IsNil) 76 c.Check(unhappyCheckRan, Equals, 1) 77 } 78 79 func (s *sanitySuite) TestUnexportedChecks(c *C) { 80 // collect what funcs we run in sanity.Check 81 var runCheckers []string 82 v := reflect.ValueOf(sanity.Checks()) 83 for i := 0; i < v.Len(); i++ { 84 v := v.Index(i) 85 fname := runtime.FuncForPC(v.Pointer()).Name() 86 pos := strings.LastIndexByte(fname, '.') 87 checker := fname[pos+1:] 88 if !strings.HasPrefix(checker, "check") { 89 c.Fatalf(`%q in sanity.Checks does not have "check" prefix`, checker) 90 } 91 runCheckers = append(runCheckers, checker) 92 } 93 94 // collect all "check*" functions 95 goFiles, err := filepath.Glob("*.go") 96 c.Assert(err, IsNil) 97 fset := token.NewFileSet() 98 99 var checkers []string 100 for _, fn := range goFiles { 101 f, err := parser.ParseFile(fset, fn, nil, 0) 102 c.Assert(err, IsNil) 103 ast.Inspect(f, func(n ast.Node) bool { 104 switch x := n.(type) { 105 case *ast.File: 106 return true 107 case *ast.FuncDecl: 108 name := x.Name.Name 109 if strings.HasPrefix(name, "check") { 110 checkers = append(checkers, name) 111 } 112 return false 113 default: 114 return false 115 } 116 }) 117 } 118 119 sort.Strings(checkers) 120 sort.Strings(runCheckers) 121 122 c.Check(checkers, DeepEquals, runCheckers) 123 }