github.com/gogf/gf@v1.16.9/os/gcmd/gcmd_z_unit_default_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcmd_test
    10  
    11  import (
    12  	"github.com/gogf/gf/os/genv"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/frame/g"
    16  	"github.com/gogf/gf/os/gcmd"
    17  
    18  	"github.com/gogf/gf/test/gtest"
    19  )
    20  
    21  func Test_Default(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		gcmd.Init([]string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}...)
    24  		t.Assert(len(gcmd.GetArgAll()), 2)
    25  		t.Assert(gcmd.GetArg(1), "path")
    26  		t.Assert(gcmd.GetArg(100, "test"), "test")
    27  		t.Assert(gcmd.GetOpt("force"), "remove")
    28  		t.Assert(gcmd.GetOpt("n"), "root")
    29  		t.Assert(gcmd.ContainsOpt("fq"), true)
    30  		t.Assert(gcmd.ContainsOpt("p"), true)
    31  		t.Assert(gcmd.ContainsOpt("none"), false)
    32  		t.Assert(gcmd.GetOpt("none", "value"), "value")
    33  	})
    34  	gtest.C(t, func(t *gtest.T) {
    35  		gcmd.Init([]string{"gf", "gen", "-h"}...)
    36  		t.Assert(len(gcmd.GetArgAll()), 2)
    37  		t.Assert(gcmd.GetOpt("h"), "")
    38  		t.Assert(gcmd.ContainsOpt("h"), true)
    39  	})
    40  }
    41  
    42  func Test_BuildOptions(t *testing.T) {
    43  	gtest.C(t, func(t *gtest.T) {
    44  		s := gcmd.BuildOptions(g.MapStrStr{
    45  			"n": "john",
    46  		})
    47  		t.Assert(s, "-n=john")
    48  	})
    49  
    50  	gtest.C(t, func(t *gtest.T) {
    51  		s := gcmd.BuildOptions(g.MapStrStr{
    52  			"n": "john",
    53  		}, "-test")
    54  		t.Assert(s, "-testn=john")
    55  	})
    56  }
    57  
    58  func Test_GetWithEnv(t *testing.T) {
    59  	gtest.C(t, func(t *gtest.T) {
    60  		genv.Set("TEST", "1")
    61  		defer genv.Remove("TEST")
    62  		t.Assert(gcmd.GetOptWithEnv("test"), 1)
    63  	})
    64  	gtest.C(t, func(t *gtest.T) {
    65  		genv.Set("TEST", "1")
    66  		defer genv.Remove("TEST")
    67  		gcmd.Init("-test", "2")
    68  		t.Assert(gcmd.GetOptWithEnv("test"), 2)
    69  	})
    70  }