-
Notifications
You must be signed in to change notification settings - Fork 42
/
fscmds_test.go
134 lines (112 loc) · 2.99 KB
/
fscmds_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package upx
import (
"fmt"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
/*
mkdir /path/to/mkdir/case1
cd /path/to/mkdir
mkdir case2
cd case2
mkdir ../case3
cd ../case3
ls /path/to/mkdir
*/
func TestMkdirAndCdAndPwd(t *testing.T) {
SetUp()
defer TearDown()
base := path.Join(ROOT, "mkdir")
case1 := path.Join(base, "case1")
b, err := Upx("mkdir", case1)
assert.NoError(t, err)
assert.Equal(t, string(b), "")
Upx("cd", base)
b, _ = Upx("pwd")
assert.Equal(t, string(b), base+"\n")
case2 := path.Join(base, "case2")
b, err = Upx("mkdir", "case2")
assert.NoError(t, err)
assert.Equal(t, string(b), "")
Upx("cd", "case2")
b, _ = Upx("pwd")
assert.Equal(t, string(b), case2+"\n")
case3 := path.Join(base, "case3")
b, err = Upx("mkdir", "../case3")
assert.NoError(t, err)
assert.Equal(t, string(b), "")
Upx("cd", "../case3")
b, _ = Upx("pwd")
assert.Equal(t, string(b), case3+"\n")
// check
b, err = Upx("ls", base)
assert.NoError(t, err)
output := string(b)
lines := strings.Split(output, "\n")
assert.Equal(t, len(lines), 4)
assert.Equal(t, strings.Contains(output, " case1\n"), true)
assert.Equal(t, strings.Contains(output, " case2\n"), true)
assert.Equal(t, strings.Contains(output, " case3\n"), true)
}
/*
ls /path/to/file
ls -r /path/to/dir
ls -c 10 /path/to/dir
ls -d /path/to/dir
ls -r -d -c 10 /path/to/dir
*/
func TestLs(t *testing.T) {
base := ROOT + "/ls"
dirs, files := []string{}, []string{}
func() {
SetUp()
Upx("mkdir", base)
Upx("cd", base)
for i := 0; i < 11; i++ {
Upx("mkdir", fmt.Sprintf("dir%d", i))
dirs = append(dirs, fmt.Sprintf("dir%d", i))
}
CreateFile("FILE")
for i := 0; i < 5; i++ {
Upx("put", "FILE", fmt.Sprintf("FILE%d", i))
files = append(files, fmt.Sprintf("FILE%d", i))
}
}()
defer func() {
for _, file := range files {
Upx("rm", file)
}
for _, dir := range dirs {
Upx("rm", dir)
}
TearDown()
}()
b, err := Upx("ls")
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(b), "\n")), len(dirs)+len(files)+1)
normal, err := Upx("ls", base)
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(normal), "\n")), len(dirs)+len(files)+1)
c := (len(dirs) + len(files)) - 1
limited, err := Upx("ls", "-c", fmt.Sprint(c))
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(limited), "\n")), c+1)
folders, err := Upx("ls", "-d")
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(folders), "\n")), len(dirs)+1)
c = len(dirs) - 1
lfolders, err := Upx("ls", "-d", "-c", fmt.Sprint(c))
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(lfolders), "\n")), c+1)
for _, line := range strings.Split(string(lfolders), "\n")[0:c] {
assert.Equal(t, strings.HasPrefix(line, "drwxrwxrwx "), true)
}
lfiles, err := Upx("ls", "FILE*")
assert.NoError(t, err)
assert.Equal(t, len(strings.Split(string(lfiles), "\n")), 6)
reversed, err := Upx("ls", "-r", base)
assert.NoError(t, err)
assert.NotEqual(t, string(reversed), string(normal))
}