...

Source file src/edge-infra.dev/third_party/gopherage/pkg/cov/util.go

Documentation: edge-infra.dev/third_party/gopherage/pkg/cov

     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  	"errors"
    21  	"fmt"
    22  	"golang.org/x/tools/cover"
    23  	"io"
    24  )
    25  
    26  // DumpProfile dumps the profiles given to writer in go coverage format.
    27  func DumpProfile(profiles []*cover.Profile, writer io.Writer) error {
    28  	if len(profiles) == 0 {
    29  		return errors.New("can't write an empty profile")
    30  	}
    31  	if _, err := io.WriteString(writer, "mode: "+profiles[0].Mode+"\n"); err != nil {
    32  		return err
    33  	}
    34  	for _, profile := range profiles {
    35  		for _, block := range profile.Blocks {
    36  			if _, err := fmt.Fprintf(writer, "%s:%d.%d,%d.%d %d %d\n", profile.FileName, block.StartLine, block.StartCol, block.EndLine, block.EndCol, block.NumStmt, block.Count); err != nil {
    37  				return err
    38  			}
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  func deepCopyProfile(profile cover.Profile) cover.Profile {
    45  	p := profile
    46  	p.Blocks = make([]cover.ProfileBlock, len(profile.Blocks))
    47  	copy(p.Blocks, profile.Blocks)
    48  	return p
    49  }
    50  
    51  // blocksEqual returns true if the blocks refer to the same code, otherwise false.
    52  // It does not care about Count.
    53  func blocksEqual(a cover.ProfileBlock, b cover.ProfileBlock) bool {
    54  	return a.StartCol == b.StartCol && a.StartLine == b.StartLine &&
    55  		a.EndCol == b.EndCol && a.EndLine == b.EndLine && a.NumStmt == b.NumStmt
    56  }
    57  
    58  func ensureProfilesMatch(a *cover.Profile, b *cover.Profile) error {
    59  	if a.FileName != b.FileName {
    60  		return fmt.Errorf("coverage filename mismatch (%s vs %s)", a.FileName, b.FileName)
    61  	}
    62  	if len(a.Blocks) != len(b.Blocks) {
    63  		return fmt.Errorf("file block count for %s mismatches (%d vs %d)", a.FileName, len(a.Blocks), len(b.Blocks))
    64  	}
    65  	if a.Mode != b.Mode {
    66  		return fmt.Errorf("mode for %s mismatches (%s vs %s)", a.FileName, a.Mode, b.Mode)
    67  	}
    68  	for i, ba := range a.Blocks {
    69  		bb := b.Blocks[i]
    70  		if !blocksEqual(ba, bb) {
    71  			return fmt.Errorf("coverage block mismatch: block #%d for %s (%+v mismatches %+v)", i, a.FileName, ba, bb)
    72  		}
    73  	}
    74  	return nil
    75  }
    76  

View as plain text