github.com/felipejfc/helm@v2.1.2+incompatible/pkg/ignore/rules_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 103 // Directory tests 104 {`cargo/`, "cargo", true}, 105 {`cargo/`, "cargo/", true}, 106 {`cargo/`, "mast/", false}, 107 {`helm.txt/`, "helm.txt", false}, 108 109 // Negation tests 110 {`!helm.txt`, "helm.txt", false}, 111 {`!helm.txt`, "tiller.txt", true}, 112 {`!*.txt`, "cargo", true}, 113 {`!cargo/`, "mast/", true}, 114 115 // Absolute path tests 116 {`/a.txt`, "a.txt", true}, 117 {`/a.txt`, "cargo/a.txt", false}, 118 {`/cargo/a.txt`, "cargo/a.txt", true}, 119 } 120 121 for _, test := range tests { 122 r, err := parseString(test.pattern) 123 if err != nil { 124 t.Fatalf("Failed to parse: %s", err) 125 } 126 fi, err := os.Stat(filepath.Join(testdata, test.name)) 127 if err != nil { 128 t.Fatalf("Fixture missing: %s", err) 129 } 130 131 if r.Ignore(test.name, fi) != test.expect { 132 t.Errorf("Expected %q to be %v for pattern %q", test.name, test.expect, test.pattern) 133 } 134 } 135 } 136 137 func TestAddDefaults(t *testing.T) { 138 r := Rules{} 139 r.AddDefaults() 140 141 if len(r.patterns) != 1 { 142 t.Errorf("Expected 1 default patterns, got %d", len(r.patterns)) 143 } 144 } 145 146 func parseString(str string) (*Rules, error) { 147 b := bytes.NewBuffer([]byte(str)) 148 return Parse(b) 149 }