-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_test.go
More file actions
197 lines (170 loc) · 3.49 KB
/
Copy pathutils_test.go
File metadata and controls
197 lines (170 loc) · 3.49 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"io"
"log"
"os"
"os/exec"
"path"
"strconv"
"testing"
)
func deepCompare(file1, file2 string) bool {
const chunkSize = 64000
f1, err := os.Open(file1)
if err != nil {
log.Fatal(err)
}
f2, err := os.Open(file2)
if err != nil {
log.Fatal(err)
}
for {
b1 := make([]byte, chunkSize)
_, err1 := f1.Read(b1)
b2 := make([]byte, chunkSize)
_, err2 := f2.Read(b2)
if err1 != nil || err2 != nil {
if err1 == io.EOF && err2 == io.EOF {
return true
} else if err1 == io.EOF || err2 == io.EOF {
return false
} else {
log.Fatal(err1, err2)
}
}
if !bytes.Equal(b1, b2) {
return false
}
}
}
func Test_fixRelativeDir(t *testing.T) {
pre := "/home"
input := ""
expect := ""
output := fixRelativeDir(input, pre)
if output != expect {
t.Error("output is:", output)
}
input = "./subdir"
expect = "/home/subdir"
output = fixRelativeDir(input, pre)
if output != expect {
t.Error("output is:", output)
}
input = "/boot/subdir"
expect = "/boot/subdir"
output = fixRelativeDir(input, pre)
if output != expect {
t.Error("output is:", output)
}
}
func Test_fixHomePath(t *testing.T) {
input := "~/workspace"
home := os.Getenv("HOME")
expect := path.Join(home, "workspace")
output := fixHomePath(input)
if output != expect {
t.Error("wrong output:", output)
}
input = "$HOME/workspace"
output = fixHomePath(input)
if output != expect {
t.Error("wrong output:", output)
}
input = ""
expect = ""
output = fixHomePath(input)
if output != expect {
t.Error("wrong output:", output)
}
}
func Test_isNumber(t *testing.T) {
input := "12"
expect := true
output := isNumber(input)
if output != expect {
t.Error("wrong output:", output)
}
input = "abc"
expect = false
output = isNumber(input)
if output != expect {
t.Error("wrong output:", output)
}
input = "12abc"
expect = false
output = isNumber(input)
if output != expect {
t.Error("wrong output:", output)
}
input = ""
expect = false
output = isNumber(input)
if output != expect {
t.Error("wrong output:", output)
}
}
func Test_checkFileExist(t *testing.T) {
input := "./utils_test.go"
expect := true
output, err := checkFileExsit(input)
if err != nil {
t.Error(err)
} else if output != expect {
t.Error("output wrong:", output)
}
input = "./utils_test.go.test"
expect = false
output, err = checkFileExsit(input)
if err != nil {
t.Error(err)
} else if output != expect {
t.Error("output wrong:", output)
}
input = ""
expect = false
output, err = checkFileExsit(input)
if err != nil {
t.Error(err)
} else if output != expect {
t.Error("output wrong:", output)
}
}
func Test_checkDirExist(t *testing.T) {
input := "./test_dir"
if err := checkDirExist(input); err != nil {
t.Error(err)
}
os.Remove(input)
}
func Test_copyFileContents(t *testing.T) {
src := "Makefile"
dst := "Makefile.cpy"
err := copyFileContents(src, dst)
if err != nil {
t.Error(err)
}
defer os.Remove(dst)
if !deepCompare(src, dst) {
t.Error("file dose not match")
}
}
func Test_getCpuNum(t *testing.T) {
// use 'grep -c ^processors.: /proc/cpuinfo' to get number of CPU
var result bytes.Buffer
cmd := exec.Command("grep", "-c", "^processor.:", "/proc/cpuinfo")
err := pipeCmd(cmd, &result, false)
if err != nil {
t.Error(err)
}
num := result.String()
expect, err := strconv.Atoi(num[0 : len(num)-1])
if err != nil {
t.Error(err)
}
output := getCpuNum()
if expect != output {
t.Errorf("expect %d, output %d\n", expect, output)
}
}