2018-10-23 11:05:44 -04:00
|
|
|
package engine
|
2018-03-19 18:56:51 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2018-09-11 08:46:30 -04:00
|
|
|
clitypes "github.com/docker/cli/types"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-03-19 18:56:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpdateContextWrite(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
context formatter.Context
|
2018-03-19 18:56:51 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: "{{InvalidFunction}}"},
|
2018-03-19 18:56:51 -04:00
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: "{{nil}}"},
|
2018-03-19 18:56:51 -04:00
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Table format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("table", false)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`TYPE VERSION NOTES
|
|
|
|
updateType1 version1 description 1
|
|
|
|
updateType2 version2 description 2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("table", true)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`version1
|
|
|
|
version2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("table {{.Version}}", false)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`VERSION
|
|
|
|
version1
|
|
|
|
version2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("table {{.Version}}", true)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`VERSION
|
|
|
|
version1
|
|
|
|
version2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Raw Format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("raw", false)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`update_version: version1
|
|
|
|
type: updateType1
|
|
|
|
notes: description 1
|
|
|
|
|
|
|
|
update_version: version2
|
|
|
|
type: updateType2
|
|
|
|
notes: description 2
|
|
|
|
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("raw", true)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`update_version: version1
|
|
|
|
update_version: version2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Custom Format
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewUpdatesFormat("{{.Version}}", false)},
|
2018-03-19 18:56:51 -04:00
|
|
|
`version1
|
|
|
|
version2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range cases {
|
2018-09-11 08:46:30 -04:00
|
|
|
updates := []clitypes.Update{
|
2018-03-19 18:56:51 -04:00
|
|
|
{Type: "updateType1", Version: "version1", Notes: "description 1"},
|
|
|
|
{Type: "updateType2", Version: "version2", Notes: "description 2"},
|
|
|
|
}
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
testcase.context.Output = out
|
|
|
|
err := UpdatesWrite(testcase.context, updates)
|
|
|
|
if err != nil {
|
|
|
|
assert.Error(t, err, testcase.expected)
|
|
|
|
} else {
|
|
|
|
assert.Check(t, is.Equal(testcase.expected, out.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateContextWriteJSON(t *testing.T) {
|
2018-09-11 08:46:30 -04:00
|
|
|
updates := []clitypes.Update{
|
2018-03-19 18:56:51 -04:00
|
|
|
{Type: "updateType1", Version: "version1", Notes: "note1"},
|
|
|
|
{Type: "updateType2", Version: "version2", Notes: "note2"},
|
|
|
|
}
|
|
|
|
expectedJSONs := []map[string]interface{}{
|
|
|
|
{"Version": "version1", "Notes": "note1", "Type": "updateType1"},
|
|
|
|
{"Version": "version2", "Notes": "note2", "Type": "updateType2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := &bytes.Buffer{}
|
2018-10-23 11:05:44 -04:00
|
|
|
err := UpdatesWrite(formatter.Context{Format: "{{json .}}", Output: out}, updates)
|
2018-03-19 18:56:51 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(line), &m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Check(t, is.DeepEqual(expectedJSONs[i], m))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateContextWriteJSONField(t *testing.T) {
|
2018-09-11 08:46:30 -04:00
|
|
|
updates := []clitypes.Update{
|
2018-03-19 18:56:51 -04:00
|
|
|
{Type: "updateType1", Version: "version1"},
|
|
|
|
{Type: "updateType2", Version: "version2"},
|
|
|
|
}
|
|
|
|
out := &bytes.Buffer{}
|
2018-10-23 11:05:44 -04:00
|
|
|
err := UpdatesWrite(formatter.Context{Format: "{{json .Type}}", Output: out}, updates)
|
2018-03-19 18:56:51 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal([]byte(line), &s); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Check(t, is.Equal(updates[i].Type, s))
|
|
|
|
}
|
|
|
|
}
|