2018-10-23 11:05:44 -04:00
|
|
|
package registry
|
2017-08-15 06:58:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-08-15 06:58:49 -04:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-08-15 06:58:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSearchContext(t *testing.T) {
|
|
|
|
var ctx searchContext
|
|
|
|
cases := []struct {
|
|
|
|
searchCtx searchContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
2023-08-08 11:31:03 -04:00
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Name: "nginx"},
|
|
|
|
},
|
|
|
|
expValue: "nginx",
|
|
|
|
call: ctx.Name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{StarCount: 5000},
|
|
|
|
},
|
|
|
|
expValue: "5000",
|
|
|
|
call: ctx.StarCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{IsOfficial: true},
|
|
|
|
},
|
|
|
|
expValue: "[OK]",
|
|
|
|
call: ctx.IsOfficial,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{IsOfficial: false},
|
|
|
|
},
|
|
|
|
call: ctx.IsOfficial,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
golangci-lint: enable more linters
fix some nolintlint false positives
For some reason, nolintlint doesn't consider these used, but they seem to be
legitimate cases where deprecated fields are used.
templates/templates.go:27:29: directive `//nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope` is unused for linter "staticcheck" (nolintlint)
"title": strings.Title, //nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
^
cli/command/formatter/image_test.go:75:31: directive `//nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.` is unused for linter "staticcheck" (nolintlint)
call: ctx.VirtualSize, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
^
cli/command/registry/formatter_search.go:100:39: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
return c.formatBool(c.s.IsAutomated) //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:50:55: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:53:31: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:59:27: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:202:84: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-02 15:50:18 -04:00
|
|
|
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:nolintlint,staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
2023-08-08 11:31:03 -04:00
|
|
|
},
|
|
|
|
expValue: "[OK]",
|
golangci-lint: enable more linters
fix some nolintlint false positives
For some reason, nolintlint doesn't consider these used, but they seem to be
legitimate cases where deprecated fields are used.
templates/templates.go:27:29: directive `//nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope` is unused for linter "staticcheck" (nolintlint)
"title": strings.Title, //nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
^
cli/command/formatter/image_test.go:75:31: directive `//nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.` is unused for linter "staticcheck" (nolintlint)
call: ctx.VirtualSize, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
^
cli/command/registry/formatter_search.go:100:39: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
return c.formatBool(c.s.IsAutomated) //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:50:55: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:53:31: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:59:27: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:202:84: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-02 15:50:18 -04:00
|
|
|
call: ctx.IsAutomated, //nolint:nolintlint,staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
2023-08-08 11:31:03 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{},
|
|
|
|
},
|
golangci-lint: enable more linters
fix some nolintlint false positives
For some reason, nolintlint doesn't consider these used, but they seem to be
legitimate cases where deprecated fields are used.
templates/templates.go:27:29: directive `//nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope` is unused for linter "staticcheck" (nolintlint)
"title": strings.Title, //nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
^
cli/command/formatter/image_test.go:75:31: directive `//nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.` is unused for linter "staticcheck" (nolintlint)
call: ctx.VirtualSize, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
^
cli/command/registry/formatter_search.go:100:39: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
return c.formatBool(c.s.IsAutomated) //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:50:55: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:53:31: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:59:27: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:202:84: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-02 15:50:18 -04:00
|
|
|
call: ctx.IsAutomated, //nolint:nolintlint,staticcheck // ignore SA1019 (IsAutomated is deprecated).
|
2023-08-08 11:31:03 -04:00
|
|
|
},
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.searchCtx
|
|
|
|
v := c.call()
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, is.Equal(v, c.expValue))
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchContextDescription(t *testing.T) {
|
2023-08-08 11:31:03 -04:00
|
|
|
const (
|
|
|
|
shortDescription = "Official build of Nginx."
|
|
|
|
longDescription = "Automated Nginx reverse proxy for docker containers"
|
|
|
|
descriptionWReturns = "Automated\nNginx reverse\rproxy\rfor docker\ncontainers"
|
|
|
|
)
|
2017-08-15 06:58:49 -04:00
|
|
|
|
|
|
|
var ctx searchContext
|
|
|
|
cases := []struct {
|
|
|
|
searchCtx searchContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
2023-08-08 11:31:03 -04:00
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: shortDescription},
|
|
|
|
trunc: true,
|
|
|
|
},
|
|
|
|
expValue: shortDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: shortDescription},
|
|
|
|
trunc: false,
|
|
|
|
},
|
|
|
|
expValue: shortDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: longDescription},
|
|
|
|
trunc: false,
|
|
|
|
},
|
|
|
|
expValue: longDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: longDescription},
|
|
|
|
trunc: true,
|
|
|
|
},
|
|
|
|
expValue: formatter.Ellipsis(longDescription, 45),
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: descriptionWReturns},
|
|
|
|
trunc: false,
|
|
|
|
},
|
|
|
|
expValue: longDescription,
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
searchCtx: searchContext{
|
|
|
|
s: registrytypes.SearchResult{Description: descriptionWReturns},
|
|
|
|
trunc: true,
|
|
|
|
},
|
|
|
|
expValue: formatter.Ellipsis(longDescription, 45),
|
|
|
|
call: ctx.Description,
|
|
|
|
},
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.searchCtx
|
|
|
|
v := c.call()
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, is.Equal(v, c.expValue))
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchContextWrite(t *testing.T) {
|
|
|
|
cases := []struct {
|
2023-08-08 11:31:03 -04:00
|
|
|
doc string
|
|
|
|
format formatter.Format
|
|
|
|
expected string
|
|
|
|
expectedErr string
|
2017-08-15 06:58:49 -04:00
|
|
|
}{
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Errors",
|
|
|
|
format: "{{InvalidFunction}}",
|
|
|
|
expectedErr: `template parsing error: template: :1: function "InvalidFunction" not defined`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "Nil format",
|
|
|
|
format: "{{nil}}",
|
|
|
|
expectedErr: `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "JSON format",
|
|
|
|
format: "{{json .}}",
|
|
|
|
expected: `{"Description":"Official build","IsAutomated":"false","IsOfficial":"true","Name":"result1","StarCount":"5000"}
|
|
|
|
{"Description":"Not official","IsAutomated":"true","IsOfficial":"false","Name":"result2","StarCount":"5"}
|
|
|
|
`,
|
2017-08-15 06:58:49 -04:00
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "JSON format, single field",
|
|
|
|
format: "{{json .Name}}",
|
|
|
|
expected: `"result1"
|
|
|
|
"result2"
|
|
|
|
`,
|
2017-08-15 06:58:49 -04:00
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Table format",
|
|
|
|
format: NewSearchFormat("table"),
|
|
|
|
expected: string(golden.Get(t, "search-context-write-table.golden")),
|
2017-08-15 06:58:49 -04:00
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Table format, single column",
|
|
|
|
format: NewSearchFormat("table {{.Name}}"),
|
|
|
|
expected: `NAME
|
2017-08-15 06:58:49 -04:00
|
|
|
result1
|
|
|
|
result2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Custom format, single field",
|
|
|
|
format: NewSearchFormat("{{.Name}}"),
|
|
|
|
expected: `result1
|
2017-08-15 06:58:49 -04:00
|
|
|
result2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2023-08-08 11:31:03 -04:00
|
|
|
doc: "Custom Format, two columns",
|
|
|
|
format: NewSearchFormat("{{.Name}} {{.StarCount}}"),
|
|
|
|
expected: `result1 5000
|
2017-08-15 06:58:49 -04:00
|
|
|
result2 5
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
results := []registrytypes.SearchResult{
|
2023-08-07 14:21:39 -04:00
|
|
|
{Name: "result1", Description: "Official build", StarCount: 5000, IsOfficial: true},
|
golangci-lint: enable more linters
fix some nolintlint false positives
For some reason, nolintlint doesn't consider these used, but they seem to be
legitimate cases where deprecated fields are used.
templates/templates.go:27:29: directive `//nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope` is unused for linter "staticcheck" (nolintlint)
"title": strings.Title, //nolint:staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
^
cli/command/formatter/image_test.go:75:31: directive `//nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.` is unused for linter "staticcheck" (nolintlint)
call: ctx.VirtualSize, //nolint:staticcheck // ignore SA1019: field is deprecated, but still set on API < v1.44.
^
cli/command/registry/formatter_search.go:100:39: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
return c.formatBool(c.s.IsAutomated) //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:50:55: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
s: registrytypes.SearchResult{IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:53:31: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:59:27: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
call: ctx.IsAutomated, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
cli/command/registry/formatter_search_test.go:202:84: directive `//nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).` is unused for linter "staticcheck" (nolintlint)
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true}, //nolint:staticcheck // ignore SA1019 (IsAutomated is deprecated).
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-02 15:50:18 -04:00
|
|
|
{Name: "result2", Description: "Not official", StarCount: 5, IsAutomated: true},
|
2020-08-28 17:00:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
2023-08-08 11:31:03 -04:00
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
2020-08-28 17:00:21 -04:00
|
|
|
var out bytes.Buffer
|
2023-08-08 11:31:03 -04:00
|
|
|
err := SearchWrite(formatter.Context{Format: tc.format, Output: &out}, results)
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
assert.Check(t, is.Error(err, tc.expectedErr))
|
2020-08-28 17:00:21 -04:00
|
|
|
} else {
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, err)
|
2020-08-28 17:00:21 -04:00
|
|
|
}
|
2023-08-08 11:31:03 -04:00
|
|
|
assert.Check(t, is.Equal(out.String(), tc.expected))
|
2020-08-28 17:00:21 -04:00
|
|
|
})
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
}
|