Skip to content

Commit

Permalink
test: add test for cmd/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jan 6, 2024
1 parent ac71292 commit e3fdd5a
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 17 deletions.
57 changes: 40 additions & 17 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
FlareModel "github.com/soulteary/flare/config/model"
)

func parseCLI(baseFlags FlareModel.Flags) FlareModel.Flags {

func GetCliFlags() (*FlareModel.Flags, *flags.FlagSet) {
var cliFlags = new(FlareModel.Flags)
options := flags.NewFlagSet("appFlags", flags.ContinueOnError)
options.SortFlags = false
Expand Down Expand Up @@ -45,14 +44,14 @@ func parseCLI(baseFlags FlareModel.Flags) FlareModel.Flags {

_ = options.Parse(os.Args)

exit := ExcuteCLI(cliFlags, options)
if exit {
os.Exit(0)
}
GetVersion(true)
return cliFlags, options
}

// 用于判断参数是否存在
func GetFlagsMaps() map[string]bool {
keys := make(map[string]bool)
if len(os.Args) <= 1 {
return keys
}
trimValue := regexp.MustCompile(`=.*`)
for _, key := range os.Args[1:] {
if key[:2] == "--" {
Expand All @@ -61,24 +60,48 @@ func parseCLI(baseFlags FlareModel.Flags) FlareModel.Flags {
keys[trimValue.ReplaceAllString(key[1:], "")] = true
}
}
return keys
}

func CheckFlagsExists(dict map[string]bool, keys []string) bool {
for _, key := range keys {
if dict[key] {
return true
}
}
return false
}

func parseCLI(baseFlags FlareModel.Flags) FlareModel.Flags {
userOptionsFromCLI, originFlags := GetCliFlags()
exit := ExcuteCLI(userOptionsFromCLI, originFlags)
if exit {
os.Exit(0)
}
GetVersion(true)

// 用于判断参数是否存在
keys := GetFlagsMaps()

cliFlags := userOptionsFromCLI

if keys[_KEY_PORT] || keys[_KEY_PORT_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_PORT, _KEY_PORT_SHORT}) {
baseFlags.Port = cliFlags.Port
}

if keys[_KEY_MINI_REQUEST] || keys[_KEY_MINI_REQUEST_SHORT] || keys[_KEY_MINI_REQUEST_OLD] {
if CheckFlagsExists(keys, []string{_KEY_MINI_REQUEST, _KEY_MINI_REQUEST_SHORT, _KEY_MINI_REQUEST_OLD}) {
baseFlags.EnableMinimumRequest = cliFlags.EnableMinimumRequest
}

if keys[_KEY_DISABLE_LOGIN] || keys[_KEY_DISABLE_LOGIN_SHORT] || keys[_KEY_DISABLE_LOGIN_OLD] {
if CheckFlagsExists(keys, []string{_KEY_DISABLE_LOGIN, _KEY_DISABLE_LOGIN_SHORT, _KEY_DISABLE_LOGIN_OLD}) {
baseFlags.DisableLoginMode = cliFlags.DisableLoginMode
}

if keys[_KEY_DISABLE_CSP] || keys[_KEY_DISABLE_CSP_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_DISABLE_CSP, _KEY_DISABLE_CSP_SHORT}) {
baseFlags.DisableCSP = cliFlags.DisableCSP
}

if keys[_KEY_VISIBILITY] || keys[_KEY_VISIBILITY_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_VISIBILITY, _KEY_VISIBILITY_SHORT}) {
baseFlags.Visibility = cliFlags.Visibility
// 判断是否为白名单中的词,以及强制转换内容为大写
if strings.ToUpper(cliFlags.Visibility) != FlareDefine.DEFAULT_VISIBILITY &&
Expand All @@ -91,19 +114,19 @@ func parseCLI(baseFlags FlareModel.Flags) FlareModel.Flags {
baseFlags.Visibility = strings.ToUpper(baseFlags.Visibility)
}

if keys[_KEY_ENABLE_OFFLINE] || keys[_KEY_ENABLE_OFFLINE_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_ENABLE_OFFLINE, _KEY_ENABLE_OFFLINE_SHORT}) {
baseFlags.EnableOfflineMode = cliFlags.EnableOfflineMode
}

if keys[_KEY_ENABLE_DEPRECATED_NOTICE] || keys[_KEY_ENABLE_DEPRECATED_NOTICE_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_ENABLE_DEPRECATED_NOTICE, _KEY_ENABLE_DEPRECATED_NOTICE_SHORT}) {
baseFlags.EnableDeprecatedNotice = cliFlags.EnableDeprecatedNotice
}

if keys[_KEY_ENABLE_GUIDE] || keys[_KEY_ENABLE_GUIDE_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_ENABLE_GUIDE, _KEY_ENABLE_GUIDE_SHORT}) {
baseFlags.EnableGuide = cliFlags.EnableGuide
}

if keys[_KEY_ENABLE_EDITOR] || keys[_KEY_ENABLE_EDITOR_SHORT] {
if CheckFlagsExists(keys, []string{_KEY_ENABLE_EDITOR, _KEY_ENABLE_EDITOR_SHORT}) {
baseFlags.EnableEditor = cliFlags.EnableEditor
}

Expand Down
143 changes: 143 additions & 0 deletions cmd/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package FlareCMD_test

import (
"flag"
"os"
"testing"

FlareCMD "github.com/soulteary/flare/cmd"
FlareDefine "github.com/soulteary/flare/config/define"
"github.com/stretchr/testify/assert"
)

func TestGetCliFlags(t *testing.T) {
originalArgs := os.Args
defer func() { os.Args = originalArgs }()

tests := []struct {
name string
args []string
wantPort int
wantEnable bool
}{
{
name: "empty args",
args: []string{""},
wantPort: FlareDefine.DEFAULT_PORT,
wantEnable: false,
},
{
name: "set port and enable guide",
args: []string{"app", "--port", "9090", "--enable-guide"},
wantPort: 9090,
wantEnable: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = append([]string{"app"}, tt.args...)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
gotFlags, _ := FlareCMD.GetCliFlags()
assert.Equal(t, tt.wantPort, gotFlags.Port)
})
}
}

func TestGetFlagsMaps(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

tests := []struct {
name string
args []string
want map[string]bool
}{
{
name: "test single dash flags",
args: []string{"cmd", "-foo", "-bar=value", "-baz"},
want: map[string]bool{"foo": true, "bar": true, "baz": true},
},
{
name: "test double dash flags",
args: []string{"cmd", "--alpha", "--beta=ok", "--gamma"},
want: map[string]bool{"alpha": true, "beta": true, "gamma": true},
},
{
name: "test mixed dash flags",
args: []string{"cmd", "--apple", "-banana=yellow", "--cherry", "-date"},
want: map[string]bool{"apple": true, "banana": true, "cherry": true, "date": true},
},
{
name: "test no flags",
args: []string{"cmd"},
want: map[string]bool{},
},
{
name: "test empty args",
args: []string{},
want: map[string]bool{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
got := FlareCMD.GetFlagsMaps()
assert.Equal(t, tt.want, got)
})
}
}

func TestCheckFlagsExists(t *testing.T) {
tests := []struct {
name string
dict map[string]bool
keys []string
expect bool
}{
{
name: "all false",
dict: map[string]bool{"a": false, "b": false, "c": false},
keys: []string{"a", "b"},
expect: false,
},
{
name: "one true",
dict: map[string]bool{"a": true, "b": false, "c": false},
keys: []string{"a", "b"},
expect: true,
},
{
name: "none existent",
dict: map[string]bool{"a": true, "b": true},
keys: []string{"c", "d"},
expect: false,
},
{
name: "empty keys",
dict: map[string]bool{"a": true, "b": true},
keys: []string{},
expect: false,
},
{
name: "empty dict",
dict: map[string]bool{},
keys: []string{"a", "b"},
expect: false,
},
{
name: "nil dict",
dict: nil,
keys: []string{"a", "b"},
expect: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := FlareCMD.CheckFlagsExists(tt.dict, tt.keys)
assert.Equal(t, result, tt.expect)
})
}
}

0 comments on commit e3fdd5a

Please sign in to comment.