github.com/sandwich-go/boost@v1.3.29/xcmd/cmd_test.go (about)

     1  package xcmd
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	. "github.com/smartystreets/goconvey/convey"
    12  )
    13  
    14  func cleanParsedOptions() { parsedOptions = make(map[string]string) }
    15  
    16  func TestCommand(t *testing.T) {
    17  	SetFlagPrefix("test_")
    18  
    19  	cleanParsedOptions()
    20  	Convey("add duplicated command flag should panic", t, func() {
    21  		var name = fmt.Sprintf("%stest", GetFlagPrefix())
    22  		So(func() { MustAddFlag(name, "test") }, ShouldNotPanic)
    23  		So(func() { MustAddFlag(name, "test") }, ShouldPanic)
    24  	})
    25  
    26  	cleanParsedOptions()
    27  	Convey("command flag format", t, func() {
    28  		Convey("without FlagPrefix should panic", func() {
    29  			So(func() { MustAddFlag("test", "test") }, ShouldPanic)
    30  		})
    31  		Convey("contain invalid char should panic", func() {
    32  			for _, invalidChar := range invalidChars {
    33  				So(func() { MustAddFlag(fmt.Sprintf("%s%stest", GetFlagPrefix(), string(invalidChar)), "test") }, ShouldPanic)
    34  			}
    35  		})
    36  	})
    37  
    38  	cleanParsedOptions()
    39  	Convey("command init", t, func() {
    40  		var name1 = fmt.Sprintf("%sdebug", GetFlagPrefix())
    41  		var name2 = fmt.Sprintf("%sp1", GetFlagPrefix())
    42  		var name3 = fmt.Sprintf("%sp2", GetFlagPrefix())
    43  		Init(fmt.Sprintf("--%s=true", name1), fmt.Sprintf("--%s", name2), "test", fmt.Sprintf("--%s", name3))
    44  		So(IsTrue(GetOptWithEnv(name1)), ShouldBeTrue)
    45  		So(GetOptWithEnv(name2), ShouldEqual, "test")
    46  		So(IsFalse(GetOptWithEnv(name3)), ShouldBeTrue)
    47  	})
    48  
    49  	cleanParsedOptions()
    50  	Convey("flag provided but not defined", t, func() {
    51  		var name1 = strings.TrimSuffix(GetFlagPrefix(), "_")
    52  		var name2 = fmt.Sprintf("%sp1", GetFlagPrefix())
    53  		Convey("should occur", func() {
    54  			flag.CommandLine.Init(name1, flag.PanicOnError)
    55  			os.Args = append(os.Args, fmt.Sprintf("--%s=true", name2))
    56  			flag.CommandLine.SetOutput(ioutil.Discard)
    57  			So(func() { _ = flag.CommandLine.Parse(os.Args[1:]) }, ShouldPanic)
    58  		})
    59  		Convey("should not occur after call DeclareInto", func() {
    60  			MustAddFlag(name2, "true")
    61  			DeclareInto(flag.CommandLine)
    62  			So(func() { _ = flag.CommandLine.Parse(os.Args[1:]) }, ShouldNotPanic)
    63  		})
    64  	})
    65  
    66  	cleanParsedOptions()
    67  	Convey("parameter order,command line flag => os env", t, func() {
    68  		var name = fmt.Sprintf("%s_p2", GetFlagPrefix())
    69  		err := os.Setenv(name, "v2")
    70  		So(err, ShouldBeNil)
    71  		os.Args = append(os.Args, fmt.Sprintf("--%s=v1", name))
    72  		So(ContainsOpt(name), ShouldBeTrue)
    73  		So(GetOptWithEnv(name), ShouldEqual, "v1")
    74  		So(GetOptWithEnv(name), ShouldEqual, "v1")
    75  	})
    76  }