package ncli_test import ( "git.noahlan.cn/noahlan/ntool/ncli" "git.noahlan.cn/noahlan/ntool/ntest/assert" "strings" "testing" ) func TestCurrentShell(t *testing.T) { path := ncli.CurrentShell(true) if path != "" { assert.NotEmpty(t, path) assert.True(t, ncli.HasShellEnv(path)) path = ncli.CurrentShell(false) assert.NotEmpty(t, path) } } func TestExecCmd(t *testing.T) { ret, err := ncli.ExecCmd("cmd", []string{"/c", "echo", "OK"}) assert.NoErr(t, err) // *nix: "OK\n" win: "OK\r\n" assert.Eq(t, "OK", strings.TrimSpace(ret)) ret, err = ncli.ExecCommand("cmd", []string{"/c", "echo", "OK1"}) assert.NoErr(t, err) assert.Eq(t, "OK1", strings.TrimSpace(ret)) ret, err = ncli.QuickExec("cmd /c echo OK2") assert.NoErr(t, err) assert.Eq(t, "OK2", strings.TrimSpace(ret)) ret, err = ncli.ExecLine("cmd /c echo OK3") assert.NoErr(t, err) assert.Eq(t, "OK3", strings.TrimSpace(ret)) } func TestShellExec(t *testing.T) { ret, err := ncli.ShellExec("echo OK") assert.NoErr(t, err) // *nix: "OK\n" win: "OK\r\n" assert.Eq(t, "OK", strings.TrimSpace(ret)) ret, err = ncli.ShellExec("echo OK", "powershell") assert.NoErr(t, err) assert.Eq(t, "OK", strings.TrimSpace(ret)) } func TestLineBuild(t *testing.T) { s := ncli.LineBuild("myapp", []string{"-a", "val0", "arg0"}) assert.Eq(t, "myapp -a val0 arg0", s) s = ncli.BuildLine("./myapp", []string{ "-a", "val0", "-m", "this is message", "arg0", }) assert.Eq(t, `./myapp -a val0 -m "this is message" arg0`, s) } func TestParseLine(t *testing.T) { args := ncli.ParseLine(`./app top sub -a ddd --xx "msg"`) assert.Len(t, args, 7) assert.Eq(t, "msg", args[6]) args = ncli.String2OSArgs(`./app top sub --msg "has inner 'quote'"`) //dump.P(args) assert.Len(t, args, 5) assert.Eq(t, "has inner 'quote'", args[4]) // exception line string. args = ncli.ParseLine(`./app top sub -a ddd --xx msg"`) // dump.P(args) assert.Len(t, args, 7) assert.Eq(t, "msg\"", args[6]) args = ncli.StringToOSArgs(`./app top sub -a ddd --xx "msg "text"`) // dump.P(args) assert.Len(t, args, 7) assert.Eq(t, "msg \"text", args[6]) } func TestWorkdir(t *testing.T) { assert.NotEmpty(t, ncli.Workdir()) assert.NotEmpty(t, ncli.BinDir()) assert.NotEmpty(t, ncli.BinFile()) assert.NotEmpty(t, ncli.BinName()) } func TestColorPrint(t *testing.T) { // code gen by: kite gen parse ncli/_demo/gen-code.tpl ncli.Redp("p:red color message, ") ncli.Redf("f:%s color message, ", "red") ncli.Redln("ln:red color message print in cli.") ncli.Bluep("p:blue color message, ") ncli.Bluef("f:%s color message, ", "blue") ncli.Blueln("ln:blue color message print in cli.") ncli.Cyanp("p:cyan color message, ") ncli.Cyanf("f:%s color message, ", "cyan") ncli.Cyanln("ln:cyan color message print in cli.") ncli.Grayp("p:gray color message, ") ncli.Grayf("f:%s color message, ", "gray") ncli.Grayln("ln:gray color message print in cli.") ncli.Greenp("p:green color message, ") ncli.Greenf("f:%s color message, ", "green") ncli.Greenln("ln:green color message print in cli.") ncli.Yellowp("p:yellow color message, ") ncli.Yellowf("f:%s color message, ", "yellow") ncli.Yellowln("ln:yellow color message print in cli.") ncli.Magentap("p:magenta color message, ") ncli.Magentaf("f:%s color message, ", "magenta") ncli.Magentaln("ln:magenta color message print in cli.") ncli.Infop("p:info color message, ") ncli.Infof("f:%s color message, ", "info") ncli.Infoln("ln:info color message print in cli.") ncli.Successp("p:success color message, ") ncli.Successf("f:%s color message, ", "success") ncli.Successln("ln:success color message print in cli.") ncli.Warnp("p:warn color message, ") ncli.Warnf("f:%s color message, ", "warn") ncli.Warnln("ln:warn color message print in cli.") ncli.Errorp("p:error color message, ") ncli.Errorf("f:%s color message, ", "error") ncli.Errorln("ln:error color message print in cli.") } func TestBuildOptionHelpName(t *testing.T) { assert.Eq(t, "-a, -b", ncli.BuildOptionHelpName([]string{"a", "b"})) assert.Eq(t, "-h, --help", ncli.BuildOptionHelpName([]string{"h", "help"})) } func TestShellQuote(t *testing.T) { assert.Eq(t, `"'"`, ncli.ShellQuote("'")) assert.Eq(t, `""`, ncli.ShellQuote("")) assert.Eq(t, `" "`, ncli.ShellQuote(" ")) assert.Eq(t, `"ab s"`, ncli.ShellQuote("ab s")) assert.Eq(t, `"ab's"`, ncli.ShellQuote("ab's")) assert.Eq(t, `'ab"s'`, ncli.ShellQuote(`ab"s`)) assert.Eq(t, "abs", ncli.ShellQuote("abs")) }