add unit for formatters.go
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
		
							parent
							
								
									137afa3f35
								
							
						
					
					
						commit
						7f70138b5f
					
				|  | @ -0,0 +1,80 @@ | ||||||
|  | package app | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"os" | ||||||
|  | 	"testing" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // TestFormatAsTable tests the FormatAsTable function.
 | ||||||
|  | func TestFormatAsTable(t *testing.T) { | ||||||
|  | 
 | ||||||
|  | 	h := []*HelmRelease{ | ||||||
|  | 		{ | ||||||
|  | 			Name:      "test", | ||||||
|  | 			Namespace: "test", | ||||||
|  | 			Enabled:   true, | ||||||
|  | 			Installed: true, | ||||||
|  | 			Labels:    "test", | ||||||
|  | 			Chart:     "test", | ||||||
|  | 			Version:   "test", | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			Name:      "test1", | ||||||
|  | 			Namespace: "test2", | ||||||
|  | 			Enabled:   false, | ||||||
|  | 			Installed: false, | ||||||
|  | 			Labels:    "test1", | ||||||
|  | 			Chart:     "test1", | ||||||
|  | 			Version:   "test1", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	tableoutput := "testdata/formatters/tableoutput" | ||||||
|  | 	expectd, err := os.ReadFile(tableoutput) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Errorf("error reading %s: %v", tableoutput, err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	result := captureStdout(func() { | ||||||
|  | 		FormatAsTable(h) | ||||||
|  | 	}) | ||||||
|  | 	if result != string(expectd) { | ||||||
|  | 		t.Errorf("FormatAsTable() = %v, want %v", result, string(expectd)) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestFormatAsJson(t *testing.T) { | ||||||
|  | 	h := []*HelmRelease{ | ||||||
|  | 		{ | ||||||
|  | 			Name:      "test", | ||||||
|  | 			Namespace: "test", | ||||||
|  | 			Enabled:   true, | ||||||
|  | 			Installed: true, | ||||||
|  | 			Labels:    "test", | ||||||
|  | 			Chart:     "test", | ||||||
|  | 			Version:   "test", | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			Name:      "test1", | ||||||
|  | 			Namespace: "test2", | ||||||
|  | 			Enabled:   false, | ||||||
|  | 			Installed: false, | ||||||
|  | 			Labels:    "test1", | ||||||
|  | 			Chart:     "test1", | ||||||
|  | 			Version:   "test1", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	output := "testdata/formatters/jsonoutput" | ||||||
|  | 	expectd, err := os.ReadFile(output) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Errorf("error reading %s: %v", output, err) | ||||||
|  | 	} | ||||||
|  | 	result := captureStdout(func() { | ||||||
|  | 		FormatAsJson(h) | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	if result != string(expectd) { | ||||||
|  | 		t.Errorf("FormatAsJson() = %v, want %v", result, string(expectd)) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | [{"name":"test","namespace":"test","enabled":true,"installed":true,"labels":"test","chart":"test","version":"test"},{"name":"test1","namespace":"test2","enabled":false,"installed":false,"labels":"test1","chart":"test1","version":"test1"}] | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | NAME 	NAMESPACE	ENABLED	INSTALLED	LABELS	CHART	VERSION | ||||||
|  | test 	test     	true   	true     	test  	test 	test    | ||||||
|  | test1	test2    	false  	false    	test1 	test1	test1   | ||||||
		Loading…
	
		Reference in New Issue