-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_test.go
97 lines (85 loc) · 2.6 KB
/
cmd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com
import (
"bufio"
"context"
"io"
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseArgs(t *testing.T) {
parts := ParseArgs(`tower.exe -c tower.yaml -p "eee\"ddd" -t aaaa`)
expected := []string{"tower.exe", "-c", "tower.yaml", "-p", "eee\"ddd", "-t", "aaaa"}
assert.Equal(t, expected, parts)
parts = ParseArgs(`tower.exe -c tower.yaml -p "eee\"ddd" -t aaaa`) // more space
assert.Equal(t, expected, parts)
parts = ParseArgs(`tower.exe -c=tower.yaml -p="eee\"ddd" -t=aaaa`)
assert.Equal(t, expected, parts)
parts = ParseArgs(`tower.exe -c=tower.yaml -p='eee"ddd' -t=aaaa`)
assert.Equal(t, expected, parts)
parts = ParseArgs(`tower.exe -c tower.yaml -p "eee\"ddd" -t aaaa`) // space and tab
assert.Equal(t, expected, parts)
parts = ParseArgs(`tower.exe -c tower.yaml -p 'eee\'ddd' -t aaaa`)
assert.Equal(t, []string{"tower.exe", "-c", "tower.yaml", "-p", "eee'ddd", "-t", "aaaa"}, parts)
parts = ParseArgs(`tower.exe -c tower.yaml -p ' eee\'ddd ' -t aaaa`)
assert.Equal(t, []string{"tower.exe", "-c", "tower.yaml", "-p", " eee'ddd ", "-t", "aaaa"}, parts)
}
func TestParseFields(t *testing.T) {
parts := ParseFields(`drwxr-xr-x 1 root root 0 2023-11-19 04:18 'test test2'`)
expected := []string{"drwxr-xr-x", "1", "root", "root", "0", "2023-11-19", "04:18", "test test2"}
assert.Equal(t, expected, parts)
}
func TestParseEnvVar(t *testing.T) {
os.Setenv(`TESTENV`, `1`)
v := ParseEnvVar(`ab{$TESTENV}c`)
assert.Equal(t, `ab1c`, v)
v = ParseEnvVar(`ab{$NOTEXISTS:ok}c`)
assert.Equal(t, `abokc`, v)
}
func TestParseWindowsEnvVar(t *testing.T) {
os.Setenv(`TESTENV`, `2`)
v := ParseWindowsEnvVar(`ab{%TESTENV%}c`)
assert.Equal(t, `ab2c`, v)
v = ParseWindowsEnvVar(`ab{%NOTEXISTS:ok%}c`)
assert.Equal(t, `abokc`, v)
}
func TestCmdChanReader(t *testing.T) {
c := NewCmdChanReader()
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
c.Close()
}()
wg := sync.WaitGroup{}
wg.Add(5)
go func() {
in := bufio.NewReader(c)
for {
select {
case <-ctx.Done():
return
default:
input, err := in.ReadString(LF)
if err != nil && err != io.EOF {
t.Log(err.Error())
return
}
wg.Done()
t.Log(input)
}
}
}()
c.SendStringAndWait("OK\n")
c.SendStringAndWait("OK1\n")
c.SendStringAndWait("OK2\n")
c.SendStringAndWait("OK3\n")
c.SendStringAndWait("OK4\n")
wg.Wait()
}
func TestParseCmdArgs(t *testing.T) {
args := ParseCmdArgs(`c.exe`, `--a`, `b`)
assert.Equal(t, map[string]string{"a": "b"}, args)
args = ParseCmdArgs(`c.exe`, `--a`)
assert.Equal(t, map[string]string{"a": ""}, args)
}