github.com/golang/dep@v0.5.4/gps/pkgtree/ignored_ruleset_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pkgtree
     6  
     7  import "testing"
     8  
     9  func TestIgnoredRuleset(t *testing.T) {
    10  	type tfixm []struct {
    11  		path string
    12  		wild bool
    13  	}
    14  	cases := []struct {
    15  		name            string
    16  		inputs          []string
    17  		wantInTree      tfixm
    18  		wantEmptyTree   bool
    19  		shouldIgnore    []string
    20  		shouldNotIgnore []string
    21  	}{
    22  		{
    23  			name:          "only skip global ignore",
    24  			inputs:        []string{"*"},
    25  			wantEmptyTree: true,
    26  		},
    27  		{
    28  			name: "ignores without ignore suffix",
    29  			inputs: []string{
    30  				"x/y/z",
    31  				"*a/b/c",
    32  				"gophers",
    33  			},
    34  			wantInTree: tfixm{
    35  				{path: "x/y/z", wild: false},
    36  				{path: "*a/b/c", wild: false},
    37  				{path: "gophers", wild: false},
    38  			},
    39  			shouldIgnore: []string{
    40  				"x/y/z",
    41  				"gophers",
    42  			},
    43  			shouldNotIgnore: []string{
    44  				"x/y/z/q",
    45  				"x/y",
    46  				"gopher",
    47  				"gopherss",
    48  			},
    49  		},
    50  		{
    51  			name: "ignores with ignore suffix",
    52  			inputs: []string{
    53  				"x/y/z*",
    54  				"a/b/c",
    55  				"gophers",
    56  			},
    57  			wantInTree: tfixm{
    58  				{path: "x/y/z", wild: true},
    59  				{path: "a/b/c", wild: false},
    60  				{path: "gophers", wild: false},
    61  			},
    62  			shouldIgnore: []string{
    63  				"x/y/z",
    64  				"x/y/zz",
    65  				"x/y/z/",
    66  				"x/y/z/q",
    67  			},
    68  			shouldNotIgnore: []string{
    69  				"x/y",
    70  				"gopher",
    71  			},
    72  		},
    73  		{
    74  			name: "global ignore with other strings",
    75  			inputs: []string{
    76  				"*",
    77  				"gophers*",
    78  				"x/y/z*",
    79  				"a/b/c",
    80  			},
    81  			wantInTree: tfixm{
    82  				{path: "x/y/z", wild: true},
    83  				{path: "a/b/c", wild: false},
    84  				{path: "gophers", wild: true},
    85  			},
    86  			shouldIgnore: []string{
    87  				"x/y/z",
    88  				"x/y/z/",
    89  				"x/y/z/q",
    90  				"gophers",
    91  				"gopherss",
    92  				"gophers/foo",
    93  			},
    94  			shouldNotIgnore: []string{
    95  				"x/y",
    96  				"gopher",
    97  			},
    98  		},
    99  		{
   100  			name: "ineffectual ignore with wildcard",
   101  			inputs: []string{
   102  				"a/b*",
   103  				"a/b/c*",
   104  				"a/b/x/y",
   105  				"a/c*",
   106  			},
   107  			wantInTree: tfixm{
   108  				{path: "a/c", wild: true},
   109  				{path: "a/b", wild: true},
   110  			},
   111  			shouldIgnore: []string{
   112  				"a/cb",
   113  			},
   114  			shouldNotIgnore: []string{
   115  				"a/",
   116  				"a/d",
   117  			},
   118  		},
   119  		{
   120  			name: "same path with and without wildcard",
   121  			inputs: []string{
   122  				"a/b*",
   123  				"a/b",
   124  			},
   125  			wantInTree: tfixm{
   126  				{path: "a/b", wild: true},
   127  			},
   128  			shouldIgnore: []string{
   129  				"a/b",
   130  				"a/bb",
   131  			},
   132  			shouldNotIgnore: []string{
   133  				"a/",
   134  				"a/d",
   135  			},
   136  		},
   137  		{
   138  			name: "empty paths",
   139  			inputs: []string{
   140  				"",
   141  				"a/b*",
   142  			},
   143  			wantInTree: tfixm{
   144  				{path: "a/b", wild: true},
   145  			},
   146  			shouldNotIgnore: []string{
   147  				"",
   148  			},
   149  		},
   150  		{
   151  			name: "single wildcard",
   152  			inputs: []string{
   153  				"a/b*",
   154  			},
   155  			wantInTree: tfixm{
   156  				{path: "a/b", wild: true},
   157  			},
   158  			shouldIgnore: []string{
   159  				"a/b/c",
   160  			},
   161  		},
   162  	}
   163  
   164  	for _, c := range cases {
   165  		igm := NewIgnoredRuleset(c.inputs)
   166  		f := func(t *testing.T) {
   167  
   168  			if c.wantEmptyTree {
   169  				if igm.Len() != 0 {
   170  					t.Fatalf("wanted empty tree, but had %v elements", igm.Len())
   171  				}
   172  			}
   173  
   174  			// Check if the wildcard suffix ignores are in the tree.
   175  			for _, p := range c.wantInTree {
   176  				wildi, has := igm.t.Get(p.path)
   177  				if !has {
   178  					t.Fatalf("expected %q to be in the tree", p.path)
   179  				} else if wildi.(bool) != p.wild {
   180  					if p.wild {
   181  						t.Fatalf("expected %q to be a wildcard matcher, but it was not", p.path)
   182  					} else {
   183  						t.Fatalf("expected %q not to be a wildcard matcher, but it was", p.path)
   184  					}
   185  				}
   186  			}
   187  
   188  			for _, p := range c.shouldIgnore {
   189  				if !igm.IsIgnored(p) {
   190  					t.Fatalf("%q should be ignored, but it was not", p)
   191  				}
   192  			}
   193  			for _, p := range c.shouldNotIgnore {
   194  				if igm.IsIgnored(p) {
   195  					t.Fatalf("%q should not be ignored, but it was", p)
   196  				}
   197  			}
   198  		}
   199  		t.Run(c.name, f)
   200  
   201  		igm = NewIgnoredRuleset(igm.ToSlice())
   202  		t.Run(c.name+"/inandout", f)
   203  	}
   204  }