github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/flags/common_test.go (about)

     1  package flags
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	cliconfig "github.com/docker/cli/cli/config"
     8  	"github.com/spf13/pflag"
     9  	"gotest.tools/assert"
    10  	is "gotest.tools/assert/cmp"
    11  )
    12  
    13  func TestCommonOptionsInstallFlags(t *testing.T) {
    14  	flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
    15  	opts := NewCommonOptions()
    16  	opts.InstallFlags(flags)
    17  
    18  	err := flags.Parse([]string{
    19  		"--tlscacert=\"/foo/cafile\"",
    20  		"--tlscert=\"/foo/cert\"",
    21  		"--tlskey=\"/foo/key\"",
    22  	})
    23  	assert.NilError(t, err)
    24  	assert.Check(t, is.Equal("/foo/cafile", opts.TLSOptions.CAFile))
    25  	assert.Check(t, is.Equal("/foo/cert", opts.TLSOptions.CertFile))
    26  	assert.Check(t, is.Equal(opts.TLSOptions.KeyFile, "/foo/key"))
    27  }
    28  
    29  func defaultPath(filename string) string {
    30  	return filepath.Join(cliconfig.Dir(), filename)
    31  }
    32  
    33  func TestCommonOptionsInstallFlagsWithDefaults(t *testing.T) {
    34  	flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
    35  	opts := NewCommonOptions()
    36  	opts.InstallFlags(flags)
    37  
    38  	err := flags.Parse([]string{})
    39  	assert.NilError(t, err)
    40  	assert.Check(t, is.Equal(defaultPath("ca.pem"), opts.TLSOptions.CAFile))
    41  	assert.Check(t, is.Equal(defaultPath("cert.pem"), opts.TLSOptions.CertFile))
    42  	assert.Check(t, is.Equal(defaultPath("key.pem"), opts.TLSOptions.KeyFile))
    43  }