Merge pull request #1096 from thaJeztah/easier_testing_docker_version

Add test for version output aligning
This commit is contained in:
Silvin 2018-05-31 14:55:12 +02:00 committed by GitHub
commit 31d99ba630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 13 deletions

View File

@ -0,0 +1,9 @@
Client:
Version: 18.99.5-ce
API version: 1.38
Go version: go1.10.2
Git commit: deadbeef
Built: Wed May 30 22:21:05 2018
OS/Arch: linux/amd64
Experimental: true
Orchestrator: swarm

View File

@ -14,6 +14,7 @@ import (
"github.com/docker/cli/kubernetes"
"github.com/docker/cli/templates"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
kubernetesClient "k8s.io/client-go/kubernetes"
@ -121,19 +122,10 @@ func reformatDate(buildTime string) string {
}
func runVersion(dockerCli command.Cli, opts *versionOptions) error {
templateFormat := versionTemplate
tmpl := templates.New("version")
if opts.format != "" {
templateFormat = opts.format
} else {
tmpl = tmpl.Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder})
}
var err error
tmpl, err = tmpl.Parse(templateFormat)
tmpl, err := newVersionTemplate(opts.format)
if err != nil {
return cli.StatusError{StatusCode: 64,
Status: "Template parsing error: " + err.Error()}
return cli.StatusError{StatusCode: 64, Status: err.Error()}
}
vd := versionInfo{
@ -200,15 +192,30 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
})
}
}
t := tabwriter.NewWriter(dockerCli.Out(), 15, 1, 1, ' ', 0)
if err2 := tmpl.Execute(t, vd); err2 != nil && err == nil {
if err2 := prettyPrintVersion(dockerCli, vd, tmpl); err2 != nil && err == nil {
err = err2
}
return err
}
func prettyPrintVersion(dockerCli command.Cli, vd versionInfo, tmpl *template.Template) error {
t := tabwriter.NewWriter(dockerCli.Out(), 15, 1, 1, ' ', 0)
err := tmpl.Execute(t, vd)
t.Write([]byte("\n"))
t.Flush()
return err
}
func newVersionTemplate(templateFormat string) (*template.Template, error) {
if templateFormat == "" {
templateFormat = versionTemplate
}
tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder})
tmpl, err := tmpl.Parse(templateFormat)
return tmpl, errors.Wrap(err, "Template parsing error")
}
func getDetailsOrder(v types.ComponentVersion) []string {
out := make([]string, 0, len(v.Details))
for k := range v.Details {

View File

@ -8,6 +8,7 @@ import (
"github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api"
@ -44,6 +45,30 @@ func TestVersionWithOrchestrator(t *testing.T) {
assert.Check(t, is.Contains(cleanTabs(cli.OutBuffer().String()), "Orchestrator: swarm"))
}
func TestVersionAlign(t *testing.T) {
vi := versionInfo{
Client: clientVersion{
Version: "18.99.5-ce",
APIVersion: "1.38",
DefaultAPIVersion: "1.38",
GitCommit: "deadbeef",
GoVersion: "go1.10.2",
Os: "linux",
Arch: "amd64",
BuildTime: "Wed May 30 22:21:05 2018",
Experimental: true,
Orchestrator: "swarm",
},
}
cli := test.NewFakeCli(&fakeClient{})
tmpl, err := newVersionTemplate("")
assert.NilError(t, err)
assert.NilError(t, prettyPrintVersion(cli, vi, tmpl))
assert.Check(t, golden.String(cli.OutBuffer().String(), "docker-client-version.golden"))
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))
}
func cleanTabs(line string) string {
return strings.Join(strings.Fields(line), " ")
}