github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/junit/calculation/coveragelist.go (about) 1 /* 2 Copyright 2018 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 calculation 18 19 import ( 20 "path" 21 "strings" 22 ) 23 24 // CoverageList is a collection and summary over multiple file Coverage objects 25 type CoverageList struct { 26 *Coverage 27 Group []Coverage 28 } 29 30 // CovList constructs new (file) Group Coverage 31 func newCoverageList(name string) *CoverageList { 32 return &CoverageList{ 33 Coverage: &Coverage{Name: name}, 34 Group: []Coverage{}, 35 } 36 } 37 38 // Ratio summarizes the list of coverages and returns the summarized ratio 39 func (covList *CoverageList) Ratio() float32 { 40 covList.summarize() 41 return covList.Coverage.Ratio() 42 } 43 44 // summarize summarizes all items in the Group and stores the result 45 func (covList *CoverageList) summarize() { 46 covList.NumCoveredStmts = 0 47 covList.NumAllStmts = 0 48 for _, item := range covList.Group { 49 covList.NumCoveredStmts += item.NumCoveredStmts 50 covList.NumAllStmts += item.NumAllStmts 51 } 52 } 53 54 // Subset returns the subset obtained through applying filter 55 func (covList *CoverageList) Subset(prefix string) *CoverageList { 56 s := newCoverageList("Filtered Summary") 57 for _, c := range covList.Group { 58 if strings.HasPrefix(c.Name, prefix) { 59 covList.Group = append(covList.Group, c) 60 } 61 } 62 return s 63 } 64 65 // ListDirectories gets a list a sub-directories that contains source code. 66 func (covList CoverageList) ListDirectories() []string { 67 dirSet := map[string]bool{} 68 for _, cov := range covList.Group { 69 dirSet[path.Dir(cov.Name)] = true 70 } 71 var result []string 72 for key := range dirSet { 73 result = append(result, key) 74 } 75 return result 76 }