github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/diff.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 cov 18 19 import ( 20 "fmt" 21 "golang.org/x/tools/cover" 22 ) 23 24 // DiffProfiles returns the difference between two sets of coverage profiles. 25 // The profiles are expected to be from a single execution of the same binary 26 // (or multiple binaries, if using a merged coverage profile) 27 func DiffProfiles(before []*cover.Profile, after []*cover.Profile) ([]*cover.Profile, error) { 28 var diff []*cover.Profile 29 if len(before) != len(after) { 30 return nil, fmt.Errorf("before and after have different numbers of profiles (%d vs. %d)", len(before), len(after)) 31 } 32 for i, beforeProfile := range before { 33 afterProfile := after[i] 34 if err := ensureProfilesMatch(beforeProfile, afterProfile); err != nil { 35 return nil, fmt.Errorf("error on profile #%d: %v", i, err) 36 } 37 diffProfile := cover.Profile{FileName: beforeProfile.FileName, Mode: beforeProfile.Mode} 38 for j, beforeBlock := range beforeProfile.Blocks { 39 afterBlock := afterProfile.Blocks[j] 40 diffBlock := cover.ProfileBlock{ 41 StartLine: beforeBlock.StartLine, 42 StartCol: beforeBlock.StartCol, 43 EndLine: beforeBlock.EndLine, 44 EndCol: beforeBlock.EndCol, 45 NumStmt: beforeBlock.NumStmt, 46 Count: afterBlock.Count - beforeBlock.Count, 47 } 48 diffProfile.Blocks = append(diffProfile.Blocks, diffBlock) 49 } 50 diff = append(diff, &diffProfile) 51 } 52 return diff, nil 53 }