2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-09-05 11:38:36 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
// go test *.go -bench=".*" -benchmem
|
|
|
|
|
|
|
|
package gcmd_test
|
|
|
|
|
|
|
|
import (
|
2020-12-04 23:44:54 +08:00
|
|
|
"github.com/gogf/gf/os/genv"
|
2019-09-05 11:38:36 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/os/gcmd"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
)
|
|
|
|
|
2019-09-05 15:16:25 +08:00
|
|
|
func Test_Default(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-12-04 23:29:20 +08:00
|
|
|
gcmd.Init([]string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}...)
|
|
|
|
t.Assert(len(gcmd.GetArgAll()), 2)
|
|
|
|
t.Assert(gcmd.GetArg(1), "path")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gcmd.GetArg(100, "test"), "test")
|
2020-12-04 23:29:20 +08:00
|
|
|
t.Assert(gcmd.GetOpt("force"), "remove")
|
|
|
|
t.Assert(gcmd.GetOpt("n"), "root")
|
|
|
|
t.Assert(gcmd.ContainsOpt("fq"), true)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gcmd.ContainsOpt("p"), true)
|
|
|
|
t.Assert(gcmd.ContainsOpt("none"), false)
|
|
|
|
t.Assert(gcmd.GetOpt("none", "value"), "value")
|
2019-09-05 15:16:25 +08:00
|
|
|
})
|
2020-12-14 23:00:22 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
gcmd.Init([]string{"gf", "gen", "-h"}...)
|
|
|
|
t.Assert(len(gcmd.GetArgAll()), 2)
|
|
|
|
t.Assert(gcmd.GetOpt("h"), "")
|
|
|
|
t.Assert(gcmd.ContainsOpt("h"), true)
|
|
|
|
})
|
2019-09-05 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 11:38:36 +08:00
|
|
|
func Test_BuildOptions(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-05 11:38:36 +08:00
|
|
|
s := gcmd.BuildOptions(g.MapStrStr{
|
|
|
|
"n": "john",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s, "-n=john")
|
2019-09-05 11:38:36 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-05 11:38:36 +08:00
|
|
|
s := gcmd.BuildOptions(g.MapStrStr{
|
|
|
|
"n": "john",
|
|
|
|
}, "-test")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(s, "-testn=john")
|
2019-09-05 11:38:36 +08:00
|
|
|
})
|
|
|
|
}
|
2020-12-04 23:44:54 +08:00
|
|
|
|
|
|
|
func Test_GetWithEnv(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
genv.Set("TEST", "1")
|
|
|
|
defer genv.Remove("TEST")
|
|
|
|
t.Assert(gcmd.GetWithEnv("test"), 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
genv.Set("TEST", "1")
|
|
|
|
defer genv.Remove("TEST")
|
|
|
|
gcmd.Init("-test", "2")
|
|
|
|
t.Assert(gcmd.GetWithEnv("test"), 2)
|
|
|
|
})
|
|
|
|
}
|