github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/lint/support/message_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 support 18 19 import ( 20 "testing" 21 22 "github.com/pkg/errors" 23 ) 24 25 var linter = Linter{} 26 var errLint = errors.New("lint failed") 27 28 func TestRunLinterRule(t *testing.T) { 29 var tests = []struct { 30 Severity int 31 LintError error 32 ExpectedMessages int 33 ExpectedReturn bool 34 ExpectedHighestSeverity int 35 }{ 36 {InfoSev, errLint, 1, false, InfoSev}, 37 {WarningSev, errLint, 2, false, WarningSev}, 38 {ErrorSev, errLint, 3, false, ErrorSev}, 39 // No error so it returns true 40 {ErrorSev, nil, 3, true, ErrorSev}, 41 // Retains highest severity 42 {InfoSev, errLint, 4, false, ErrorSev}, 43 // Invalid severity values 44 {4, errLint, 4, false, ErrorSev}, 45 {22, errLint, 4, false, ErrorSev}, 46 {-1, errLint, 4, false, ErrorSev}, 47 } 48 49 for _, test := range tests { 50 isValid := linter.RunLinterRule(test.Severity, "chart", test.LintError) 51 if len(linter.Messages) != test.ExpectedMessages { 52 t.Errorf("RunLinterRule(%d, \"chart\", %v), linter.Messages should now have %d message, we got %d", test.Severity, test.LintError, test.ExpectedMessages, len(linter.Messages)) 53 } 54 55 if linter.HighestSeverity != test.ExpectedHighestSeverity { 56 t.Errorf("RunLinterRule(%d, \"chart\", %v), linter.HighestSeverity should be %d, we got %d", test.Severity, test.LintError, test.ExpectedHighestSeverity, linter.HighestSeverity) 57 } 58 59 if isValid != test.ExpectedReturn { 60 t.Errorf("RunLinterRule(%d, \"chart\", %v), should have returned %t but returned %t", test.Severity, test.LintError, test.ExpectedReturn, isValid) 61 } 62 } 63 } 64 65 func TestMessage(t *testing.T) { 66 m := Message{ErrorSev, "Chart.yaml", errors.New("Foo")} 67 if m.Error() != "[ERROR] Chart.yaml: Foo" { 68 t.Errorf("Unexpected output: %s", m.Error()) 69 } 70 71 m = Message{WarningSev, "templates/", errors.New("Bar")} 72 if m.Error() != "[WARNING] templates/: Bar" { 73 t.Errorf("Unexpected output: %s", m.Error()) 74 } 75 76 m = Message{InfoSev, "templates/rc.yaml", errors.New("FooBar")} 77 if m.Error() != "[INFO] templates/rc.yaml: FooBar" { 78 t.Errorf("Unexpected output: %s", m.Error()) 79 } 80 }