github.com/gogf/gf/v2@v2.7.4/os/gcmd/gcmd_z_unit_issue_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 "context" 13 "testing" 14 15 "github.com/gogf/gf/v2/encoding/gjson" 16 "github.com/gogf/gf/v2/frame/g" 17 "github.com/gogf/gf/v2/os/gcmd" 18 "github.com/gogf/gf/v2/os/gctx" 19 "github.com/gogf/gf/v2/test/gtest" 20 ) 21 22 type Issue3390CommandCase1 struct { 23 *gcmd.Command 24 } 25 26 type Issue3390TestCase1 struct { 27 g.Meta `name:"index" ad:"test"` 28 } 29 30 type Issue3390Case1Input struct { 31 g.Meta `name:"index"` 32 A string `short:"a" name:"aa"` 33 Be string `short:"b" name:"bb"` 34 } 35 36 type Issue3390Case1Output struct { 37 Content string 38 } 39 40 func (c Issue3390TestCase1) Index(ctx context.Context, in Issue3390Case1Input) (out *Issue3390Case1Output, err error) { 41 out = &Issue3390Case1Output{ 42 Content: gjson.MustEncodeString(in), 43 } 44 return 45 } 46 47 func Test_Issue3390_Case1(t *testing.T) { 48 gtest.C(t, func(t *gtest.T) { 49 root, err := gcmd.NewFromObject(Issue3390TestCase1{}) 50 t.AssertNil(err) 51 command := &Issue3390CommandCase1{root} 52 value, err := command.RunWithSpecificArgs( 53 gctx.New(), 54 []string{"main", "-a", "aaa", "-b", "bbb"}, 55 ) 56 t.AssertNil(err) 57 t.Assert(value.(*Issue3390Case1Output).Content, `{"A":"aaa","Be":"bbb"}`) 58 }) 59 } 60 61 type Issue3390CommandCase2 struct { 62 *gcmd.Command 63 } 64 65 type Issue3390TestCase2 struct { 66 g.Meta `name:"index" ad:"test"` 67 } 68 69 type Issue3390Case2Input struct { 70 g.Meta `name:"index"` 71 A string `short:"b" name:"bb"` 72 Be string `short:"a" name:"aa"` 73 } 74 75 type Issue3390Case2Output struct { 76 Content string 77 } 78 79 func (c Issue3390TestCase2) Index(ctx context.Context, in Issue3390Case2Input) (out *Issue3390Case2Output, err error) { 80 out = &Issue3390Case2Output{ 81 Content: gjson.MustEncodeString(in), 82 } 83 return 84 } 85 func Test_Issue3390_Case2(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 root, err := gcmd.NewFromObject(Issue3390TestCase2{}) 88 t.AssertNil(err) 89 command := &Issue3390CommandCase2{root} 90 value, err := command.RunWithSpecificArgs( 91 gctx.New(), 92 []string{"main", "-a", "aaa", "-b", "bbb"}, 93 ) 94 t.AssertNil(err) 95 t.Assert(value.(*Issue3390Case2Output).Content, `{"A":"bbb","Be":"aaa"}`) 96 }) 97 } 98 99 type Issue3390CommandCase3 struct { 100 *gcmd.Command 101 } 102 103 type Issue3390TestCase3 struct { 104 g.Meta `name:"index" ad:"test"` 105 } 106 107 type Issue3390Case3Input struct { 108 g.Meta `name:"index"` 109 A string `short:"b"` 110 Be string `short:"a"` 111 } 112 113 type Issue3390Case3Output struct { 114 Content string 115 } 116 117 func (c Issue3390TestCase3) Index(ctx context.Context, in Issue3390Case3Input) (out *Issue3390Case3Output, err error) { 118 out = &Issue3390Case3Output{ 119 Content: gjson.MustEncodeString(in), 120 } 121 return 122 } 123 func Test_Issue3390_Case3(t *testing.T) { 124 gtest.C(t, func(t *gtest.T) { 125 root, err := gcmd.NewFromObject(Issue3390TestCase3{}) 126 t.AssertNil(err) 127 command := &Issue3390CommandCase3{root} 128 value, err := command.RunWithSpecificArgs( 129 gctx.New(), 130 []string{"main", "-a", "aaa", "-b", "bbb"}, 131 ) 132 t.AssertNil(err) 133 t.Assert(value.(*Issue3390Case3Output).Content, `{"A":"bbb","Be":"aaa"}`) 134 }) 135 } 136 137 type Issue3390CommandCase4 struct { 138 *gcmd.Command 139 } 140 141 type Issue3390TestCase4 struct { 142 g.Meta `name:"index" ad:"test"` 143 } 144 145 type Issue3390Case4Input struct { 146 g.Meta `name:"index"` 147 A string `short:"a"` 148 Be string `short:"b"` 149 } 150 151 type Issue3390Case4Output struct { 152 Content string 153 } 154 155 func (c Issue3390TestCase4) Index(ctx context.Context, in Issue3390Case4Input) (out *Issue3390Case4Output, err error) { 156 out = &Issue3390Case4Output{ 157 Content: gjson.MustEncodeString(in), 158 } 159 return 160 } 161 162 func Test_Issue3390_Case4(t *testing.T) { 163 gtest.C(t, func(t *gtest.T) { 164 root, err := gcmd.NewFromObject(Issue3390TestCase4{}) 165 t.AssertNil(err) 166 command := &Issue3390CommandCase4{root} 167 value, err := command.RunWithSpecificArgs( 168 gctx.New(), 169 []string{"main", "-a", "aaa", "-b", "bbb"}, 170 ) 171 t.AssertNil(err) 172 t.Assert(value.(*Issue3390Case4Output).Content, `{"A":"aaa","Be":"bbb"}`) 173 }) 174 } 175 176 type Issue3417Test struct { 177 g.Meta `name:"root"` 178 } 179 180 type Issue3417BuildInput struct { 181 g.Meta `name:"build" config:"gfcli.build"` 182 File string `name:"FILE" arg:"true" brief:"building file path"` 183 Name string `short:"n" name:"name" brief:"output binary name"` 184 Version string `short:"v" name:"version" brief:"output binary version"` 185 Arch string `short:"a" name:"arch" brief:"output binary architecture, multiple arch separated with ','"` 186 System string `short:"s" name:"system" brief:"output binary system, multiple os separated with ','"` 187 Output string `short:"o" name:"output" brief:"output binary path, used when building single binary file"` 188 Path string `short:"p" name:"path" brief:"output binary directory path, default is '.'" d:"."` 189 Extra string `short:"e" name:"extra" brief:"extra custom \"go build\" options"` 190 Mod string `short:"m" name:"mod" brief:"like \"-mod\" option of \"go build\", use \"-m none\" to disable go module"` 191 Cgo bool `short:"c" name:"cgo" brief:"enable or disable cgo feature, it's disabled in default" orphan:"true"` 192 VarMap g.Map `short:"r" name:"varMap" brief:"custom built embedded variable into binary"` 193 PackSrc string `short:"ps" name:"packSrc" brief:"pack one or more folders into one go file before building"` 194 PackDst string `short:"pd" name:"packDst" brief:"temporary go file path for pack, this go file will be automatically removed after built" d:"internal/packed/build_pack_data.go"` 195 ExitWhenError bool `short:"ew" name:"exitWhenError" brief:"exit building when any error occurs, specially for multiple arch and system buildings. default is false" orphan:"true"` 196 DumpENV bool `short:"de" name:"dumpEnv" brief:"dump current go build environment before building binary" orphan:"true"` 197 } 198 199 type Issue3417BuildOutput struct { 200 Content string 201 } 202 203 func (c *Issue3417Test) Build(ctx context.Context, in Issue3417BuildInput) (out *Issue3417BuildOutput, err error) { 204 out = &Issue3417BuildOutput{ 205 Content: gjson.MustEncodeString(in), 206 } 207 return 208 } 209 210 func Test_Issue3417(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 command, err := gcmd.NewFromObject(Issue3417Test{}) 213 t.AssertNil(err) 214 value, err := command.RunWithSpecificArgs( 215 gctx.New(), 216 []string{ 217 "gf", "build", 218 "-mod", "vendor", 219 "-v", "0.0.19", 220 "-n", "detect_hardware_os", 221 "-a", "amd64,arm64", 222 "-s", "linux", 223 "-p", "./bin", 224 "-e", "-trimpath -ldflags", 225 "cmd/v3/main.go", 226 }, 227 ) 228 t.AssertNil(err) 229 t.Assert( 230 value.(*Issue3417BuildOutput).Content, 231 `{"File":"cmd/v3/main.go","Name":"detect_hardware_os","Version":"0.0.19","Arch":"amd64,arm64","System":"linux","Output":"","Path":"./bin","Extra":"-trimpath -ldflags","Mod":"vendor","Cgo":false,"VarMap":null,"PackSrc":"","PackDst":"internal/packed/build_pack_data.go","ExitWhenError":false,"DumpENV":false}`, 232 ) 233 }) 234 }