aboutsummaryrefslogtreecommitdiff
path: root/lib/os/exec/exec_test.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-06-04 00:26:16 +0700
committerShulhan <m.shulhan@gmail.com>2020-06-04 00:26:16 +0700
commit427251e6f320849022e55510051797a10d4e9d83 (patch)
tree1d336732c82da228a4e25e7e64c1623d9885e09f /lib/os/exec/exec_test.go
parenta7e3a3852678175bf29e50c4c5a53a3ed2ab7921 (diff)
downloadpakakeh.go-427251e6f320849022e55510051797a10d4e9d83.tar.xz
os/exec: new package to simplify the standard "os/exec"
New extension to standard package is function ParseCommandArgs() that receive input as string and return itas command and list of arguments. Unlike strings.Fields() which only separated the field by space, ParseCommandArgs can detect possible single, double, or back quotes. Another extension is Run() function that accept the string command to be executed and their standard output and error.
Diffstat (limited to 'lib/os/exec/exec_test.go')
-rw-r--r--lib/os/exec/exec_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/os/exec/exec_test.go b/lib/os/exec/exec_test.go
new file mode 100644
index 00000000..ad0e82b0
--- /dev/null
+++ b/lib/os/exec/exec_test.go
@@ -0,0 +1,52 @@
+// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package exec
+
+import (
+ "testing"
+
+ "github.com/shuLhan/share/lib/test"
+)
+
+func TestParseCommandArg(t *testing.T) {
+ cases := []struct {
+ in string
+ expCmd string
+ expArgs []string
+ }{{
+ in: ``,
+ }, {
+ in: `a `,
+ expCmd: `a`,
+ expArgs: nil,
+ }, {
+ in: `a "b c"`,
+ expCmd: `a`,
+ expArgs: []string{`b c`},
+ }, {
+ in: `a "b'c"`,
+ expCmd: `a`,
+ expArgs: []string{`b'c`},
+ }, {
+ in: `'a "b'c"`,
+ expCmd: `a "b`,
+ expArgs: []string{`c`},
+ }, {
+ in: "a `b c`",
+ expCmd: `a`,
+ expArgs: []string{`b c`},
+ }, {
+ in: "a `b'c`",
+ expCmd: `a`,
+ expArgs: []string{`b'c`},
+ }}
+
+ for _, c := range cases {
+ t.Logf(c.in)
+ gotCmd, gotArgs := ParseCommandArgs(c.in)
+ test.Assert(t, "cmd", c.expCmd, gotCmd, true)
+ test.Assert(t, "args", c.expArgs, gotArgs, true)
+ }
+}