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