Files
docker-cli/cli/command/stack/formatter/formatter_test.go
Sebastiaan van Stijn 581cb2b70a cli/command/stack/swarm: GetStacks: don't use pointers for values
These are very small structs, so using pointers doesn't bring much
advantage and makes it slightly more cumbersome to use.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-28 15:44:09 +02:00

70 lines
1.3 KiB
Go

package formatter
import (
"bytes"
"testing"
"github.com/docker/cli/cli/command/formatter"
"gotest.tools/v3/assert"
)
func TestStackContextWrite(t *testing.T) {
tests := []struct {
name string
format formatter.Format
expected string
}{
{
name: "invalid function",
format: `{{InvalidFunction}}`,
expected: `template parsing error: template: :1: function "InvalidFunction" not defined`,
},
{
name: "invalid placeholder",
format: `{{nil}}`,
expected: `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
},
{
name: "table format",
format: SwarmStackTableFormat,
expected: `NAME SERVICES
baz 2
bar 1
`,
},
{
name: "custom table format",
format: `table {{.Name}}`,
expected: `NAME
baz
bar
`,
},
{
name: "custom format",
format: `{{.Name}}`,
expected: `baz
bar
`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var out bytes.Buffer
fmtCtx := formatter.Context{
Format: tc.format,
Output: &out,
}
if err := StackWrite(fmtCtx, []Stack{
{Name: "baz", Services: 2},
{Name: "bar", Services: 1},
}); err != nil {
assert.Error(t, err, tc.expected)
} else {
assert.Equal(t, out.String(), tc.expected)
}
})
}
}