feat: enhanced and added testcases to increase unit test coverage

Signed-off-by: Animesh Pathak <kurosakiichigo.songoku@gmail.com>
This commit is contained in:
Animesh Pathak 2025-02-13 16:17:33 +05:30
parent 1bd44986dd
commit 6d3d111bf2
7 changed files with 601 additions and 74 deletions

View file

@ -1,30 +1,37 @@
package output
import (
"fmt"
"io"
"fmt"
"io"
"github.com/yannh/kubeconform/pkg/validator"
"github.com/yannh/kubeconform/pkg/validator"
)
type Output interface {
Write(validator.Result) error
Flush() error
Write(validator.Result) error
Flush() error
}
func New(w io.Writer, outputFormat string, printSummary, isStdin, verbose bool) (Output, error) {
switch {
case outputFormat == "json":
return jsonOutput(w, printSummary, isStdin, verbose), nil
case outputFormat == "junit":
return junitOutput(w, printSummary, isStdin, verbose), nil
case outputFormat == "pretty":
return prettyOutput(w, printSummary, isStdin, verbose), nil
case outputFormat == "tap":
return tapOutput(w, printSummary, isStdin, verbose), nil
case outputFormat == "text":
return textOutput(w, printSummary, isStdin, verbose), nil
default:
return nil, fmt.Errorf("'outputFormat' must be 'json', 'junit', 'pretty', 'tap' or 'text'")
}
switch outputFormat {
case "json":
return jsonOutput(w, printSummary, isStdin, verbose), nil
case "junit":
return junitOutput(w, printSummary, isStdin, verbose), nil
case "pretty":
return prettyOutput(w, printSummary, isStdin, verbose), nil
case "tap":
return tapOutput(w, printSummary, isStdin, verbose), nil
case "text":
return textOutput(w, printSummary, isStdin, verbose), nil
default:
return nil, fmt.Errorf("'outputFormat' must be 'json', 'junit', 'pretty', 'tap' or 'text'")
}
}
// Mock writer for testing purposes
type mockWriter struct{}
func (m *mockWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

84
pkg/output/output_test.go Normal file
View file

@ -0,0 +1,84 @@
package output
import (
"testing"
)
// Test generated using Keploy
func TestNew_JSONOutput(t *testing.T) {
writer := &mockWriter{}
output, err := New(writer, "json", false, false, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if output == nil {
t.Fatalf("Expected a valid Output implementation, got nil")
}
}
// Test generated using Keploy
func TestNew_JUnitOutput(t *testing.T) {
writer := &mockWriter{}
output, err := New(writer, "junit", false, false, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if output == nil {
t.Fatalf("Expected a valid Output implementation, got nil")
}
}
// Test generated using Keploy
func TestNew_PrettyOutput(t *testing.T) {
writer := &mockWriter{}
output, err := New(writer, "pretty", false, false, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if output == nil {
t.Fatalf("Expected a valid Output implementation, got nil")
}
}
// Test generated using Keploy
func TestNew_UnsupportedFormat(t *testing.T) {
writer := &mockWriter{}
_, err := New(writer, "unsupported", false, false, false)
if err == nil {
t.Fatalf("Expected an error, got nil")
}
expectedError := "'outputFormat' must be 'json', 'junit', 'pretty', 'tap' or 'text'"
if err.Error() != expectedError {
t.Errorf("Expected error message '%s', got '%s'", expectedError, err.Error())
}
}
// Test generated using Keploy
func TestNew_TapOutput(t *testing.T) {
writer := &mockWriter{}
output, err := New(writer, "tap", false, false, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if output == nil {
t.Fatalf("Expected a valid Output implementation, got nil")
}
}
// Test generated using Keploy
func TestNew_TextOutput(t *testing.T) {
writer := &mockWriter{}
output, err := New(writer, "text", false, false, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if output == nil {
t.Fatalf("Expected a valid Output implementation, got nil")
}
}