...

Source file src/github.com/cilium/ebpf/internal/testutils/glob.go

Documentation: github.com/cilium/ebpf/internal/testutils

     1  package testutils
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  )
     7  
     8  // Files calls fn for each given file.
     9  //
    10  // The function errors out if the pattern matches no files.
    11  func Files(t *testing.T, files []string, fn func(*testing.T, string)) {
    12  	t.Helper()
    13  
    14  	if len(files) == 0 {
    15  		t.Fatalf("No files given")
    16  	}
    17  
    18  	for _, f := range files {
    19  		file := f // force copy
    20  		name := filepath.Base(file)
    21  		t.Run(name, func(t *testing.T) {
    22  			fn(t, file)
    23  		})
    24  	}
    25  }
    26  
    27  // Glob finds files matching a pattern.
    28  //
    29  // The pattern should may include full path. Excludes use the same syntax as
    30  // pattern, but are only applied to the basename instead of the full path.
    31  func Glob(tb testing.TB, pattern string, excludes ...string) []string {
    32  	tb.Helper()
    33  
    34  	files, err := filepath.Glob(pattern)
    35  	if err != nil {
    36  		tb.Fatal("Can't glob files:", err)
    37  	}
    38  
    39  	if len(excludes) == 0 {
    40  		return files
    41  	}
    42  
    43  	var filtered []string
    44  nextFile:
    45  	for _, file := range files {
    46  		base := filepath.Base(file)
    47  		for _, exclude := range excludes {
    48  			if matched, err := filepath.Match(exclude, base); err != nil {
    49  				tb.Fatal(err)
    50  			} else if matched {
    51  				continue nextFile
    52  			}
    53  		}
    54  		filtered = append(filtered, file)
    55  	}
    56  
    57  	return filtered
    58  }
    59  

View as plain text