...

Source file src/edge-infra.dev/third_party/gopherage/pkg/cov/filter.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  	"golang.org/x/tools/cover"
    21  	"regexp"
    22  	"strings"
    23  )
    24  
    25  // FilterProfilePaths produces a new profile that removes either everything matching or everything
    26  // not matching the provided paths, depending on the value of include.
    27  // Paths are interpreted as regular expressions.
    28  // If include is true, paths is treated as an allowlist; otherwise it is treated as a denylist.
    29  func FilterProfilePaths(profile []*cover.Profile, paths []string, include bool) ([]*cover.Profile, error) {
    30  	parenPaths := make([]string, len(paths))
    31  	for i, path := range paths {
    32  		parenPaths[i] = "(" + path + ")"
    33  	}
    34  	joined := strings.Join(parenPaths, "|")
    35  	re, err := regexp.Compile(joined)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	result := make([]*cover.Profile, 0, len(profile))
    40  	for _, p := range profile {
    41  		if re.MatchString(p.FileName) == include {
    42  			result = append(result, p)
    43  		}
    44  	}
    45  	return result, nil
    46  }
    47  

View as plain text