github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/formatter/disk_usage_test.go (about) 1 package formatter 2 3 import ( 4 "bytes" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 "gotest.tools/v3/golden" 9 ) 10 11 func TestDiskUsageContextFormatWrite(t *testing.T) { 12 cases := []struct { 13 context DiskUsageContext 14 expected string 15 }{ 16 // Check default output format (verbose and non-verbose mode) for table headers 17 { 18 DiskUsageContext{ 19 Context: Context{ 20 Format: NewDiskUsageFormat("table", false), 21 }, 22 Verbose: false, 23 }, 24 `TYPE TOTAL ACTIVE SIZE RECLAIMABLE 25 Images 0 0 0B 0B 26 Containers 0 0 0B 0B 27 Local Volumes 0 0 0B 0B 28 Build Cache 0 0 0B 0B 29 `, 30 }, 31 { 32 DiskUsageContext{Verbose: true, Context: Context{Format: NewDiskUsageFormat("table", true)}}, 33 `Images space usage: 34 35 REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS 36 37 Containers space usage: 38 39 CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES 40 41 Local Volumes space usage: 42 43 VOLUME NAME LINKS SIZE 44 45 Build cache usage: 0B 46 47 CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED 48 `, 49 }, 50 { 51 DiskUsageContext{Verbose: true, Context: Context{Format: NewDiskUsageFormat("raw", true)}}, 52 ``, 53 }, 54 { 55 DiskUsageContext{Verbose: true, Context: Context{Format: NewDiskUsageFormat("{{json .}}", true)}}, 56 `{"Images":[],"Containers":[],"Volumes":[],"BuildCache":[]}`, 57 }, 58 // Errors 59 { 60 DiskUsageContext{ 61 Context: Context{ 62 Format: "{{InvalidFunction}}", 63 }, 64 }, 65 `template parsing error: template: :1: function "InvalidFunction" not defined`, 66 }, 67 { 68 DiskUsageContext{ 69 Context: Context{ 70 Format: "{{nil}}", 71 }, 72 }, 73 `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`, 74 }, 75 // Table Format 76 { 77 DiskUsageContext{ 78 Context: Context{ 79 Format: NewDiskUsageFormat("table", false), 80 }, 81 }, 82 `TYPE TOTAL ACTIVE SIZE RECLAIMABLE 83 Images 0 0 0B 0B 84 Containers 0 0 0B 0B 85 Local Volumes 0 0 0B 0B 86 Build Cache 0 0 0B 0B 87 `, 88 }, 89 { 90 DiskUsageContext{ 91 Context: Context{ 92 Format: NewDiskUsageFormat("table {{.Type}}\t{{.Active}}", false), 93 }, 94 }, 95 string(golden.Get(t, "disk-usage-context-write-custom.golden")), 96 }, 97 // Raw Format 98 { 99 DiskUsageContext{ 100 Context: Context{ 101 Format: NewDiskUsageFormat("raw", false), 102 }, 103 }, 104 string(golden.Get(t, "disk-usage-raw-format.golden")), 105 }, 106 } 107 108 for _, tc := range cases { 109 tc := tc 110 t.Run(string(tc.context.Format), func(t *testing.T) { 111 var out bytes.Buffer 112 tc.context.Output = &out 113 if err := tc.context.Write(); err != nil { 114 assert.Error(t, err, tc.expected) 115 } else { 116 assert.Equal(t, out.String(), tc.expected) 117 } 118 }) 119 } 120 }