gitee.com/mirrors/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/unittest/unittest.go (about) 1 // Copyright 2017 The GoReporter Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package unittest 15 16 import ( 17 "bytes" 18 "os/exec" 19 "path/filepath" 20 "strings" 21 22 "github.com/golang/glog" 23 ) 24 25 func UnitTest(packagePath string) (packageUnitTestResults []string, packageTestRaceResults []string) { 26 packageUnitTestResults = make([]string, 0) 27 packageTestRaceResults = make([]string, 0) 28 29 packageName := PackageAbsPath(packagePath) 30 if "" == packageName { 31 packageName = packagePath 32 } 33 34 out, err := GoTestWithCoverAndRace(packagePath) 35 if err != nil { 36 if !strings.Contains(out, "==================") { 37 glog.Infoln("[UnitTest] package->:", packageName, " ... ", err) 38 } else { 39 glog.Infoln("[UnitTest] package->:", packageName, " ... pass") 40 } 41 } else { 42 glog.Infoln("[UnitTest] package->:", packageName, " ... pass") 43 } 44 45 if out == "" || !strings.Contains(out, "ok") { 46 return packageUnitTestResults, packageTestRaceResults 47 } else if err != nil { 48 lindex := strings.LastIndex(out, "coverage:") 49 res := strings.Split(out[lindex:], "\n") 50 info := strings.Fields(res[2]) 51 cov := strings.Fields(res[0]) 52 53 if len(info) >= 3 && len(cov) >= 2 { 54 rest := info[0] + " " + info[1] + " " + info[2] + " " + cov[0] + " " + cov[1] 55 packageUnitTestResults = strings.Fields(rest) 56 57 for in, val := range strings.Split(out, "==================") { 58 if (in+1)%2 == 0 { 59 packageTestRaceResults = append(packageTestRaceResults, val) 60 } 61 } 62 } 63 } else { 64 test := strings.Fields(out) 65 packageUnitTestResults = test 66 } 67 68 return packageUnitTestResults, packageTestRaceResults 69 } 70 71 // run go test -cover 72 func GoTestWithCoverAndRace(packagePath string) (packageUnitResult string, err error) { 73 cmd := exec.Command("go", "test", packagePath, "-cover", "-race") 74 var out bytes.Buffer 75 cmd.Stdout = &out 76 // cmd.Stderr = os.Stderr 77 err = cmd.Run() 78 if err != nil { 79 if !strings.Contains(out.String(), "==================") { 80 return "", err 81 } 82 } 83 84 return out.String(), err 85 } 86 87 // run go list -cover 88 func GoListWithImportPackages(packagePath string) (importPackages []string) { 89 importPackages = make([]string, 0) 90 cmd := exec.Command("go", "list", "-f", `'{{ join .Imports " " }}'`, packagePath) 91 var out bytes.Buffer 92 cmd.Stdout = &out 93 // cmd.Stderr = os.Stderr 94 err := cmd.Run() 95 if err != nil { 96 glog.Warningln(err) 97 return importPackages 98 } 99 packagesString := out.String() 100 packagesString = strings.Replace(packagesString, `'`, "", -1) 101 packages := strings.Fields(packagesString) 102 103 var out2 bytes.Buffer 104 cmd = exec.Command("go", "list", "std") 105 cmd.Stdout = &out2 106 // cmd.Stderr = os.Stderr 107 err = cmd.Run() 108 if err != nil { 109 glog.Warningln(err) 110 return importPackages 111 } 112 stdPackages := strings.Split(out2.String(), "\n") 113 mapStdPackages := make(map[string]string, 0) 114 for i := 0; i < len(stdPackages); i++ { 115 mapStdPackages[stdPackages[i]] = stdPackages[i] 116 } 117 // remove std package 118 for i := 0; i < len(packages); i++ { 119 if strings.Contains(packages[i], string(filepath.Separator)) && !strings.Contains(packages[i], "vendor") { 120 if _, ok := mapStdPackages[packages[i]]; !ok { 121 importPackages = append(importPackages, strings.Replace(packages[i], "'", "", -1)) 122 } 123 } 124 } 125 126 return importPackages 127 } 128 129 func PackageAbsPath(path string) (packagePath string) { 130 absPath, err := filepath.Abs(path) 131 if err != nil { 132 glog.Errorln(err) 133 } 134 packagePathIndex := strings.Index(absPath, "src") 135 if -1 != packagePathIndex { 136 packagePath = absPath[(packagePathIndex + 4):] 137 } 138 139 return packagePath 140 }