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