github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/engine/updates_test.go (about) 1 package engine 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "strings" 7 "testing" 8 9 "github.com/docker/cli/cli/command/formatter" 10 clitypes "github.com/docker/cli/types" 11 "gotest.tools/assert" 12 is "gotest.tools/assert/cmp" 13 ) 14 15 func TestUpdateContextWrite(t *testing.T) { 16 cases := []struct { 17 context formatter.Context 18 expected string 19 }{ 20 // Errors 21 { 22 formatter.Context{Format: "{{InvalidFunction}}"}, 23 `Template parsing error: template: :1: function "InvalidFunction" not defined 24 `, 25 }, 26 { 27 formatter.Context{Format: "{{nil}}"}, 28 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 29 `, 30 }, 31 // Table format 32 { 33 formatter.Context{Format: NewUpdatesFormat("table", false)}, 34 `TYPE VERSION NOTES 35 updateType1 version1 description 1 36 updateType2 version2 description 2 37 `, 38 }, 39 { 40 formatter.Context{Format: NewUpdatesFormat("table", true)}, 41 `version1 42 version2 43 `, 44 }, 45 { 46 formatter.Context{Format: NewUpdatesFormat("table {{.Version}}", false)}, 47 `VERSION 48 version1 49 version2 50 `, 51 }, 52 { 53 formatter.Context{Format: NewUpdatesFormat("table {{.Version}}", true)}, 54 `VERSION 55 version1 56 version2 57 `, 58 }, 59 // Raw Format 60 { 61 formatter.Context{Format: NewUpdatesFormat("raw", false)}, 62 `update_version: version1 63 type: updateType1 64 notes: description 1 65 66 update_version: version2 67 type: updateType2 68 notes: description 2 69 70 `, 71 }, 72 { 73 formatter.Context{Format: NewUpdatesFormat("raw", true)}, 74 `update_version: version1 75 update_version: version2 76 `, 77 }, 78 // Custom Format 79 { 80 formatter.Context{Format: NewUpdatesFormat("{{.Version}}", false)}, 81 `version1 82 version2 83 `, 84 }, 85 } 86 87 for _, testcase := range cases { 88 updates := []clitypes.Update{ 89 {Type: "updateType1", Version: "version1", Notes: "description 1"}, 90 {Type: "updateType2", Version: "version2", Notes: "description 2"}, 91 } 92 out := &bytes.Buffer{} 93 testcase.context.Output = out 94 err := UpdatesWrite(testcase.context, updates) 95 if err != nil { 96 assert.Error(t, err, testcase.expected) 97 } else { 98 assert.Check(t, is.Equal(testcase.expected, out.String())) 99 } 100 } 101 } 102 103 func TestUpdateContextWriteJSON(t *testing.T) { 104 updates := []clitypes.Update{ 105 {Type: "updateType1", Version: "version1", Notes: "note1"}, 106 {Type: "updateType2", Version: "version2", Notes: "note2"}, 107 } 108 expectedJSONs := []map[string]interface{}{ 109 {"Version": "version1", "Notes": "note1", "Type": "updateType1"}, 110 {"Version": "version2", "Notes": "note2", "Type": "updateType2"}, 111 } 112 113 out := &bytes.Buffer{} 114 err := UpdatesWrite(formatter.Context{Format: "{{json .}}", Output: out}, updates) 115 if err != nil { 116 t.Fatal(err) 117 } 118 for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") { 119 var m map[string]interface{} 120 if err := json.Unmarshal([]byte(line), &m); err != nil { 121 t.Fatal(err) 122 } 123 assert.Check(t, is.DeepEqual(expectedJSONs[i], m)) 124 } 125 } 126 127 func TestUpdateContextWriteJSONField(t *testing.T) { 128 updates := []clitypes.Update{ 129 {Type: "updateType1", Version: "version1"}, 130 {Type: "updateType2", Version: "version2"}, 131 } 132 out := &bytes.Buffer{} 133 err := UpdatesWrite(formatter.Context{Format: "{{json .Type}}", Output: out}, updates) 134 if err != nil { 135 t.Fatal(err) 136 } 137 for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") { 138 var s string 139 if err := json.Unmarshal([]byte(line), &s); err != nil { 140 t.Fatal(err) 141 } 142 assert.Check(t, is.Equal(updates[i].Type, s)) 143 } 144 }