From e16e9775d48b6cd0cc7b4a66114d1b9680aa2f31 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 13 Sep 2016 14:21:07 -0400 Subject: [PATCH] Fix testcases that expect trailing whitespace and broken integration tests based of nil pointers Signed-off-by: Daniel Nephin --- cli/command/container/ps.go | 4 ++-- cli/command/formatter/container.go | 10 +++++++++- cli/command/formatter/volume.go | 4 ++-- cli/command/formatter/volume_test.go | 12 ++++++------ cli/command/image/images.go | 2 +- cli/command/network/list.go | 2 +- cli/command/volume/list.go | 2 +- utils/templates/templates.go | 9 +++++++++ 8 files changed, 31 insertions(+), 14 deletions(-) diff --git a/cli/command/container/ps.go b/cli/command/container/ps.go index 9d015fd707..b5a3be06e9 100644 --- a/cli/command/container/ps.go +++ b/cli/command/container/ps.go @@ -111,13 +111,13 @@ func runPs(dockerCli *command.DockerCli, opts *psOptions) error { if len(dockerCli.ConfigFile().PsFormat) > 0 && !opts.quiet { format = dockerCli.ConfigFile().PsFormat } else { - format = "table" + format = formatter.TableFormatKey } } containerCtx := formatter.Context{ Output: dockerCli.Out(), - Format: formatter.NewContainerFormat(format, opts.quiet, opts.size), + Format: formatter.NewContainerFormat(format, opts.quiet, listOptions.Size), Trunc: !opts.noTrunc, } return formatter.ContainerWrite(containerCtx, containers) diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index 6f3a162fe3..30a6492476 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -41,7 +41,15 @@ func NewContainerFormat(source string, quiet bool, size bool) Format { if quiet { return `container_id: {{.ID}}` } - format := `container_id: {{.ID}}\nimage: {{.Image}}\ncommand: {{.Command}}\ncreated_at: {{.CreatedAt}}\nstatus: {{.Status}}\nnames: {{.Names}}\nlabels: {{.Labels}}\nports: {{.Ports}}\n` + format := `container_id: {{.ID}} +image: {{.Image}} +command: {{.Command}} +created_at: {{.CreatedAt}} +status: {{- pad .Status 1 0}} +names: {{.Names}} +labels: {{- pad .Labels 1 0}} +ports: {{- pad .Ports 1 0}} +` if size { format += `size: {{.Size}}\n` } diff --git a/cli/command/formatter/volume.go b/cli/command/formatter/volume.go index 2fec59d8fb..e41ee266bf 100644 --- a/cli/command/formatter/volume.go +++ b/cli/command/formatter/volume.go @@ -36,7 +36,7 @@ func NewVolumeFormat(source string, quiet bool) Format { func VolumeWrite(ctx Context, volumes []*types.Volume) error { render := func(format func(subContext subContext) error) error { for _, volume := range volumes { - if err := format(&volumeContext{v: volume}); err != nil { + if err := format(&volumeContext{v: *volume}); err != nil { return err } } @@ -47,7 +47,7 @@ func VolumeWrite(ctx Context, volumes []*types.Volume) error { type volumeContext struct { HeaderContext - v *types.Volume + v types.Volume } func (c *volumeContext) Name() string { diff --git a/cli/command/formatter/volume_test.go b/cli/command/formatter/volume_test.go index 1d5f74e42c..8c715b3438 100644 --- a/cli/command/formatter/volume_test.go +++ b/cli/command/formatter/volume_test.go @@ -21,22 +21,22 @@ func TestVolumeContext(t *testing.T) { call func() string }{ {volumeContext{ - v: &types.Volume{Name: volumeName}, + v: types.Volume{Name: volumeName}, }, volumeName, nameHeader, ctx.Name}, {volumeContext{ - v: &types.Volume{Driver: "driver_name"}, + v: types.Volume{Driver: "driver_name"}, }, "driver_name", driverHeader, ctx.Driver}, {volumeContext{ - v: &types.Volume{Scope: "local"}, + v: types.Volume{Scope: "local"}, }, "local", scopeHeader, ctx.Scope}, {volumeContext{ - v: &types.Volume{Mountpoint: "mountpoint"}, + v: types.Volume{Mountpoint: "mountpoint"}, }, "mountpoint", mountpointHeader, ctx.Mountpoint}, {volumeContext{ - v: &types.Volume{}, + v: types.Volume{}, }, "", labelsHeader, ctx.Labels}, {volumeContext{ - v: &types.Volume{Labels: map[string]string{"label1": "value1", "label2": "value2"}}, + v: types.Volume{Labels: map[string]string{"label1": "value1", "label2": "value2"}}, }, "label1=value1,label2=value2", labelsHeader, ctx.Labels}, } diff --git a/cli/command/image/images.go b/cli/command/image/images.go index b7dbd05671..0229734ce4 100644 --- a/cli/command/image/images.go +++ b/cli/command/image/images.go @@ -69,7 +69,7 @@ func runImages(dockerCli *command.DockerCli, opts imagesOptions) error { if len(dockerCli.ConfigFile().ImagesFormat) > 0 && !opts.quiet { format = dockerCli.ConfigFile().ImagesFormat } else { - format = "table" + format = formatter.TableFormatKey } } diff --git a/cli/command/network/list.go b/cli/command/network/list.go index dd7b72fea7..9ba803275b 100644 --- a/cli/command/network/list.go +++ b/cli/command/network/list.go @@ -61,7 +61,7 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error { if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !opts.quiet { format = dockerCli.ConfigFile().NetworksFormat } else { - format = "table" + format = formatter.TableFormatKey } } diff --git a/cli/command/volume/list.go b/cli/command/volume/list.go index cdbbaafc61..77ce359771 100644 --- a/cli/command/volume/list.go +++ b/cli/command/volume/list.go @@ -61,7 +61,7 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error { if len(dockerCli.ConfigFile().VolumesFormat) > 0 && !opts.quiet { format = dockerCli.ConfigFile().VolumesFormat } else { - format = "table" + format = formatter.TableFormatKey } } diff --git a/utils/templates/templates.go b/utils/templates/templates.go index 749da3d5af..91c376f38f 100644 --- a/utils/templates/templates.go +++ b/utils/templates/templates.go @@ -18,6 +18,7 @@ var basicFunctions = template.FuncMap{ "title": strings.Title, "lower": strings.ToLower, "upper": strings.ToUpper, + "pad": padWithSpace, } // Parse creates a new annonymous template with the basic functions @@ -31,3 +32,11 @@ func Parse(format string) (*template.Template, error) { func NewParse(tag, format string) (*template.Template, error) { return template.New(tag).Funcs(basicFunctions).Parse(format) } + +// padWithSpace adds whitespace to the input if the input is non-empty +func padWithSpace(source string, prefix, suffix int) string { + if source == "" { + return source + } + return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix) +}