docker-cli/cli/command/formatter/formatter_test.go
Sebastiaan van Stijn 1d768f8983
update go:build tags to go1.23 to align with vendor.mod
Go maintainers started to unconditionally update the minimum go version
for golang.org/x/ dependencies to go1.23, which means that we'll no longer
be able to support any version below that when updating those dependencies;

> all: upgrade go directive to at least 1.23.0 [generated]
>
> By now Go 1.24.0 has been released, and Go 1.22 is no longer supported
> per the Go Release Policy (https://go.dev/doc/devel/release#policy).
>
> For golang/go#69095.

This updates our minimum version to go1.23, as we won't be able to maintain
compatibility with older versions because of the above.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-17 10:43:47 +02:00

72 lines
1.4 KiB
Go

// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.23
package formatter
import (
"bytes"
"testing"
"gotest.tools/v3/assert"
)
func TestFormat(t *testing.T) {
f := Format("json")
assert.Assert(t, f.IsJSON())
assert.Assert(t, !f.IsTable())
f = Format("table")
assert.Assert(t, !f.IsJSON())
assert.Assert(t, f.IsTable())
f = Format("other")
assert.Assert(t, !f.IsJSON())
assert.Assert(t, !f.IsTable())
}
type fakeSubContext struct {
Name string
}
func (fakeSubContext) FullHeader() any {
return map[string]string{"Name": "NAME"}
}
func TestContext(t *testing.T) {
testCases := []struct {
name string
format string
expected string
}{
{
name: "json format",
format: JSONFormatKey,
expected: `{"Name":"test"}
`,
},
{
name: "table format",
format: `table {{.Name}}`,
expected: `NAME
test
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
ctx := Context{
Format: Format(tc.format),
Output: buf,
}
subContext := fakeSubContext{Name: "test"}
subFormat := func(f func(sub SubContext) error) error {
return f(subContext)
}
err := ctx.Write(&subContext, subFormat)
assert.NilError(t, err)
assert.Equal(t, buf.String(), tc.expected)
})
}
}