github.com/keleustes/helm@v3.0.0-beta.3+incompatible/internal/ignore/rules_test.go (about) 1 /* 2 Copyright The Helm 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 ignore 18 19 import ( 20 "bytes" 21 "os" 22 "path/filepath" 23 "testing" 24 ) 25 26 var testdata = "./testdata" 27 28 func TestParse(t *testing.T) { 29 rules := `#ignore 30 31 #ignore 32 foo 33 bar/* 34 baz/bar/foo.txt 35 36 one/more 37 ` 38 r, err := parseString(rules) 39 if err != nil { 40 t.Fatalf("Error parsing rules: %s", err) 41 } 42 43 if len(r.patterns) != 4 { 44 t.Errorf("Expected 4 rules, got %d", len(r.patterns)) 45 } 46 47 expects := []string{"foo", "bar/*", "baz/bar/foo.txt", "one/more"} 48 for i, p := range r.patterns { 49 if p.raw != expects[i] { 50 t.Errorf("Expected %q, got %q", expects[i], p.raw) 51 } 52 if p.match == nil { 53 t.Errorf("Expected %s to have a matcher function.", p.raw) 54 } 55 } 56 } 57 58 func TestParseFail(t *testing.T) { 59 shouldFail := []string{"foo/**/bar", "[z-"} 60 for _, fail := range shouldFail { 61 _, err := parseString(fail) 62 if err == nil { 63 t.Errorf("Rule %q should have failed", fail) 64 } 65 } 66 } 67 68 func TestParseFile(t *testing.T) { 69 f := filepath.Join(testdata, HelmIgnore) 70 if _, err := os.Stat(f); err != nil { 71 t.Fatalf("Fixture %s missing: %s", f, err) 72 } 73 74 r, err := ParseFile(f) 75 if err != nil { 76 t.Fatalf("Failed to parse rules file: %s", err) 77 } 78 79 if len(r.patterns) != 3 { 80 t.Errorf("Expected 3 patterns, got %d", len(r.patterns)) 81 } 82 } 83 84 func TestIgnore(t *testing.T) { 85 // Test table: Given pattern and name, Ignore should return expect. 86 tests := []struct { 87 pattern string 88 name string 89 expect bool 90 }{ 91 // Glob tests 92 {`helm.txt`, "helm.txt", true}, 93 {`helm.*`, "helm.txt", true}, 94 {`helm.*`, "rudder.txt", false}, 95 {`*.txt`, "tiller.txt", true}, 96 {`*.txt`, "cargo/a.txt", true}, 97 {`cargo/*.txt`, "cargo/a.txt", true}, 98 {`cargo/*.*`, "cargo/a.txt", true}, 99 {`cargo/*.txt`, "mast/a.txt", false}, 100 {`ru[c-e]?er.txt`, "rudder.txt", true}, 101 {`templates/.?*`, "templates/.dotfile", true}, 102 // "." should never get ignored. https://github.com/helm/helm/issues/1776 103 {`.*`, ".", false}, 104 {`.*`, "./", false}, 105 {`.*`, ".joonix", true}, 106 {`.*`, "helm.txt", false}, 107 {`.*`, "", false}, 108 109 // Directory tests 110 {`cargo/`, "cargo", true}, 111 {`cargo/`, "cargo/", true}, 112 {`cargo/`, "mast/", false}, 113 {`helm.txt/`, "helm.txt", false}, 114 115 // Negation tests 116 {`!helm.txt`, "helm.txt", false}, 117 {`!helm.txt`, "tiller.txt", true}, 118 {`!*.txt`, "cargo", true}, 119 {`!cargo/`, "mast/", true}, 120 121 // Absolute path tests 122 {`/a.txt`, "a.txt", true}, 123 {`/a.txt`, "cargo/a.txt", false}, 124 {`/cargo/a.txt`, "cargo/a.txt", true}, 125 } 126 127 for _, test := range tests { 128 r, err := parseString(test.pattern) 129 if err != nil { 130 t.Fatalf("Failed to parse: %s", err) 131 } 132 fi, err := os.Stat(filepath.Join(testdata, test.name)) 133 if err != nil { 134 t.Fatalf("Fixture missing: %s", err) 135 } 136 137 if r.Ignore(test.name, fi) != test.expect { 138 t.Errorf("Expected %q to be %v for pattern %q", test.name, test.expect, test.pattern) 139 } 140 } 141 } 142 143 func TestAddDefaults(t *testing.T) { 144 r := Rules{} 145 r.AddDefaults() 146 147 if len(r.patterns) != 1 { 148 t.Errorf("Expected 1 default patterns, got %d", len(r.patterns)) 149 } 150 } 151 152 func parseString(str string) (*Rules, error) { 153 b := bytes.NewBuffer([]byte(str)) 154 return Parse(b) 155 }