Skip to content

Commit

Permalink
feat: removing duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
femrtnz committed Sep 30, 2024
1 parent e7e6812 commit bd2bfda
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 142 deletions.
70 changes: 26 additions & 44 deletions pkg/printer/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,55 +41,37 @@ func TestNewCSVPrinter(t *testing.T) {
}

func TestCSVPrinterPrint(t *testing.T) {
tests := []struct {
name string
labels map[string]interface{}
withLabels config.OptionalFlags
}{
{
name: "WithLabels",
labels: map[string]interface{}{"app": "php-apache-app"},
withLabels: config.OptionalFlags{Labels: true},
},
{
name: "NoLabels",
labels: map[string]interface{}{},
withLabels: config.OptionalFlags{Labels: false},
},
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
defer os.Remove(tmpFile.Name())
defer os.Remove(tmpFile.Name())

tp := &csvPrinter{
commonPrinter: &commonPrinter{tmpFile},
}
tp := &csvPrinter{
commonPrinter: &commonPrinter{tmpFile},
}

version, _ := judge.NewVersion("1.2.3")
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: tt.labels,
}}
version, _ := judge.NewVersion("1.2.3")
labels := map[string]interface{}{"key1": "value1"}

if err := tp.Print(results, tt.withLabels); err != nil {
t.Fatalf("unexpected error: %v", err)
}
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: labels,
}}

fi, _ := tmpFile.Stat()
if fi.Size() == 0 {
t.Fatalf("expected non-zero size output file: %v", err)
}
})
if err := tp.Print(results, config.OptionalFlags{Labels: true}); err != nil {
t.Fatalf("unexpected error: %v", err)
}

fi, _ := tmpFile.Stat()
if fi.Size() == 0 {
t.Fatalf("expected non-zero size output file: %v", err)
}
}

Expand Down
87 changes: 33 additions & 54 deletions pkg/printer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,46 @@ func Test_newJSONPrinter(t *testing.T) {
})
}
}

func Test_jsonPrinter_Print(t *testing.T) {
tests := []struct {
name string
labels map[string]interface{}
withLabels config.OptionalFlags
}{
{
name: "WithLabels",
labels: map[string]interface{}{"label1": "value1", "label2": "value2"},
withLabels: config.OptionalFlags{Labels: true},
},
{
name: "NoLabels",
labels: map[string]interface{}{},
withLabels: config.OptionalFlags{Labels: false},
},
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
defer os.Remove(tmpFile.Name())

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
defer os.Remove(tmpFile.Name())

c := &jsonPrinter{
commonPrinter: &commonPrinter{tmpFile},
}
c := &jsonPrinter{
commonPrinter: &commonPrinter{tmpFile},
}

version, _ := judge.NewVersion("1.2.3")
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: tt.labels,
}}
version, _ := judge.NewVersion("1.2.3")
labels := map[string]interface{}{"key1": "value1"}

if err := c.Print(results, tt.withLabels); err != nil {
t.Fatalf("unexpected error: %v", err)
}
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: labels,
}}
if err := c.Print(results, config.OptionalFlags{Labels: true}); err != nil {
t.Fatalf("unexpected error: %v", err)
}

tmpFile.Seek(0, 0)
tmpFile.Seek(0, 0)

var readResults []judge.Result
readBytes, err := ioutil.ReadAll(tmpFile)
if err != nil {
t.Fatalf("unexpected error reading back the file: %v", err)
}
if err := json.Unmarshal(readBytes, &readResults); err != nil {
t.Fatalf("unexpected error unmarshalling the previously written file: %v", err)
}
if !reflect.DeepEqual(readResults, results) {
t.Fatalf("written and read result do not seem to be equal")
}
})
var readResults []judge.Result
readBytes, err := ioutil.ReadAll(tmpFile)
if err != nil {
t.Fatalf("unexpected error reading back the file: %v", err)
}
if err := json.Unmarshal(readBytes, &readResults); err != nil {
t.Fatalf("unexpected error unmarshalling the previously written file: %v", err)
}
if !reflect.DeepEqual(readResults, results) {
t.Fatalf("written and read result do not seem to be equal")
}
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/printer/printer_helper_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
package printer

import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/doitintl/kube-no-trouble/pkg/config"
"github.com/doitintl/kube-no-trouble/pkg/judge"
)

func TestTypePrinterPrint(t *testing.T) {
tests := []struct {
name string
labels map[string]interface{}
withLabels config.OptionalFlags
}{
{
name: "WithLabels",
labels: map[string]interface{}{"app": "version1"},
withLabels: config.OptionalFlags{Labels: true},
},
{
name: "NoLabels",
labels: map[string]interface{}{},
withLabels: config.OptionalFlags{Labels: false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
defer os.Remove(tmpFile.Name())

tp := &csvPrinter{
commonPrinter: &commonPrinter{tmpFile},
}

version, _ := judge.NewVersion("1.2.3")
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: tt.labels,
}}

if err := tp.Print(results, tt.withLabels); err != nil {
t.Fatalf("unexpected error: %v", err)
}

fi, _ := tmpFile.Stat()
if fi.Size() == 0 {
t.Fatalf("expected non-zero size output file: %v", err)
}
})
}
}

func TestMapToCommaSeparatedString(t *testing.T) {
tests := []struct {
input map[string]interface{}
Expand Down
69 changes: 25 additions & 44 deletions pkg/printer/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,56 +41,37 @@ func Test_newTextPrinter(t *testing.T) {
}

func Test_textPrinter_Print(t *testing.T) {
tests := []struct {
name string
labels map[string]interface{}
withLabels config.OptionalFlags
}{
{
name: "WithLabels",
labels: map[string]interface{}{"label1": "value1", "label2": "value2"},
withLabels: config.OptionalFlags{Labels: true},
},
{
name: "NoLabels",
labels: map[string]interface{}{},
withLabels: config.OptionalFlags{Labels: false},
},
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
defer os.Remove(tmpFile.Name())

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), tempFilePrefix)
if err != nil {
t.Fatalf(tempFileCreateFailureMessage, err)
}
defer os.Remove(tmpFile.Name())
tp := &textPrinter{
commonPrinter: &commonPrinter{tmpFile},
}

tp := &textPrinter{
commonPrinter: &commonPrinter{tmpFile},
}
version, _ := judge.NewVersion("1.2.3")
labels := map[string]interface{}{"key1": "value1"}

version, _ := judge.NewVersion("1.2.3")
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: tt.labels,
}}
results := []judge.Result{{
Name: "Name",
Namespace: "Namespace",
Kind: "Kind",
ApiVersion: "1.2.3",
RuleSet: "Test",
ReplaceWith: "4.5.6",
Since: version,
Labels: labels,
}}

if err := tp.Print(results, tt.withLabels); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := tp.Print(results, config.OptionalFlags{Labels: true}); err != nil {
t.Fatalf("unexpected error: %v", err)
}

fi, _ := tmpFile.Stat()
if fi.Size() == 0 {
t.Fatalf("expected non-zero size output file: %v", err)
}
})
fi, _ := tmpFile.Stat()
if fi.Size() == 0 {
t.Fatalf("expected non-zero size output file: %v", err)
}
}

Expand Down

0 comments on commit bd2bfda

Please sign in to comment.