github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/internal/logfilter/filter_test.go (about)

     1  package logfilter
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestFileNameFilter(t *testing.T) {
    11  	type matchPair struct {
    12  		fileName string
    13  		match    bool
    14  	}
    15  	testCases := []struct {
    16  		name  string
    17  		rule  string
    18  		match []matchPair
    19  	}{
    20  		{name: "empty", rule: "",
    21  			match: []matchPair{
    22  				{"gopkg/confr/loader.go", true},
    23  				{"gopkg/easy/json.go", true},
    24  				{"gopkg/easy/ezdbg/debug_test.go", true},
    25  			}},
    26  		{name: "allow all", rule: "allow=all",
    27  			match: []matchPair{
    28  				{"gopkg/confr/loader.go", true},
    29  				{"gopkg/easy/json.go", true},
    30  				{"gopkg/easy/ezdbg/debug_test.go", true},
    31  				{"gopkg/pkg1/a.go", true},
    32  				{"gopkg/pkg1/sub/b.go", true},
    33  				{"gopkg/pkg1/sub/sub/a.go", true},
    34  				{"gopkg/pkg2/a.go", true},
    35  				{"gopkg/pkg2/sub/b.go", true},
    36  			}},
    37  		{name: "allow 1", rule: "allow=pkg1/*,pkg2/*.go",
    38  			match: []matchPair{
    39  				{"gopkg/confr/loader.go", false},
    40  				{"gopkg/easy/json.go", false},
    41  				{"gopkg/easy/ezdbg/debug_test.go", false},
    42  				{"gopkg/pkg1/a.go", true},
    43  				{"gopkg/pkg1/sub/b.go", false},
    44  				{"gopkg/pkg2/a.go", true},
    45  				{"gopkg/pkg2/sub/b.go", false},
    46  			}},
    47  		{name: "allow 2", rule: "allow=pkg1/sub1/abc.go,pkg1/sub2/def.go",
    48  			match: []matchPair{
    49  				{"gopkg/confr/loader.go", false},
    50  				{"gopkg/easy/json.go", false},
    51  				{"gopkg/easy/ezdbg/debug_test.go", false},
    52  				{"gopkg/pkg1/a.go", false},
    53  				{"gopkg/pkg1/sub1/abc.go", true},
    54  				{"gopkg/pkg1/sub2/abc.go", false},
    55  				{"gopkg/pkg1/sub2/def.go", true},
    56  			}},
    57  		{name: "allow 3", rule: "allow=pkg1/**",
    58  			match: []matchPair{
    59  				{"gopkg/confr/loader.go", false},
    60  				{"gopkg/easy/json.go", false},
    61  				{"gopkg/easy/ezdbg/debug_test.go", false},
    62  				{"gopkg/pkg1/a.go", true},
    63  				{"gopkg/pkg1/sub/a.go", true},
    64  				{"gopkg/pkg1/sub/sub/a.go", true},
    65  			}},
    66  		{name: "deny all", rule: "deny=all",
    67  			match: []matchPair{
    68  				{"gopkg/confr/loader.go", false},
    69  				{"gopkg/easy/json.go", false},
    70  				{"gopkg/easy/ezdbg/debug_test.go", false},
    71  				{"gopkg/pkg1/a.go", false},
    72  				{"gopkg/pkg1/sub/b.go", false},
    73  				{"gopkg/pkg1/sub/sub/a.go", false},
    74  				{"gopkg/pkg2/a.go", false},
    75  				{"gopkg/pkg2/sub/b.go", false},
    76  			}},
    77  		{name: "deny 1", rule: "deny=pkg1/**.go,pkg2/**.go",
    78  			match: []matchPair{
    79  				{"gopkg/confr/loader.go", true},
    80  				{"gopkg/easy/json.go", true},
    81  				{"gopkg/easy/ezdbg/debug_test.go", true},
    82  				{"gopkg/pkg1/a.go", false},
    83  				{"gopkg/pkg1/sub/a.go", false},
    84  				{"gopkg/pkg1/sub/sub/a.go", false},
    85  				{"gopkg/pkg2/a.go", false},
    86  				{"gopkg/pkg2/sub/a.go", false},
    87  				{"gopkg/pkg2/sub/sub/a.go", false},
    88  				{"gopkg/pkg3/a.go", true},
    89  				{"gopkg/pkg3/sub/a.go", true},
    90  				{"gopkg/pkg3/sub/sub/a.go", true},
    91  			}},
    92  		{name: "deny 2", rule: "allow=all;deny=pkg/**",
    93  			match: []matchPair{
    94  				{"gopkg/confr/loader.go", false},
    95  				{"gopkg/easy/json.go", false},
    96  				{"gopkg/easy/ezdbg/debug_test.go", false},
    97  				{"gopkg/pkg1/a.go", false},
    98  				{"gopkg/pkg1/sub/a.go", false},
    99  				{"gopkg/pkg1/sub/sub/a.go", false},
   100  				{"gopkg/pkg2/a.go", false},
   101  				{"gopkg/pkg2/sub/a.go", false},
   102  				{"gopkg/pkg2/sub/sub/a.go", false},
   103  				{"gopkg/pkg3/a.go", false},
   104  				{"gopkg/pkg3/sub/a.go", false},
   105  				{"gopkg/pkg3/sub/sub/a.go", false},
   106  				{"mcli/cli.go", true},
   107  				{"mcli/pkg/a.go", false},
   108  				{"mcli/pkg/sub/a.go", false},
   109  				{"mcli/pkg/sub/sub/a.go", false},
   110  			}},
   111  	}
   112  
   113  	for _, tc := range testCases {
   114  		t.Run(tc.name, func(t *testing.T) {
   115  			filter, errs := NewFileNameFilter(tc.rule)
   116  			require.Nil(t, errs)
   117  			for _, pair := range tc.match {
   118  				fileName := "/Users/bytedance/go/src/github.com/jxskiss/" + pair.fileName
   119  				got := filter.Allow(pair.fileName)
   120  				assert.Equalf(t, pair.match, got, "rule: %v, fileName: %v", tc.rule, fileName)
   121  			}
   122  		})
   123  	}
   124  }