This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathutil_test.go
178 lines (163 loc) · 3.33 KB
/
util_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
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
package main
import (
"errors"
"fmt"
"regexp"
)
func ExampleLogIf() {
fmt.Println(LogIf(nil))
err := fmt.Errorf("generated error")
if LogIf(err) {
fmt.Println(LastError)
}
// Output:
// false
// generated error
}
func shouldPanic(flag bool) (err error) {
defer Recover(&err)
if flag {
err := errors.New("Panic mode")
FatalIf(err)
}
fmt.Println("no panic")
return nil
}
func ExampleFatalIf() {
fmt.Println(shouldPanic(false))
fmt.Println(shouldPanic(true))
// Output:
// no panic
// <nil>
// Panic mode
}
func ExampleSys() {
*DryRunFlag = false
Sys("/bin/echo 1 2 3")
Sys("/bin/echo 3", "4", "5")
Sys("@sh -c", "echo foo >/tmp/foo")
fmt.Print(Sys("cat /tmp/foo"))
Sys("@sh -c", "echo bar >/tmp/bar")
fmt.Print(Sys("@cat /tmp/bar"))
*DryRunFlag = true
// Output:
// 1 2 3
// 3 4 5
// foo
// foo
// bar
}
func ExampleSysErr() {
*DryRunFlag = false
SysErr("/bin/echo 1 2 3")
SysErr("/bin/echo 3", "4", "5")
SysErr("@sh -c", "echo foo >/tmp/foo")
fmt.Print(Sys("cat /tmp/foo"))
SysErr("@sh -c", "echo bar >/tmp/bar")
fmt.Print(Sys("@cat /tmp/bar"))
_, err := SysErr("false")
fmt.Println("ERR", err)
_, err = SysErr("donotexist")
fmt.Println("ERR", err)
*DryRunFlag = true
// Output:
// 1 2 3
// 3 4 5
// foo
// foo
// bar
// ERR exit status 1
// ERR exec: "donotexist": executable file not found in $PATH
}
func ExampleSysCd() {
*DryRunFlag = false
Sys("@mkdir -p /tmp/example-syscd/hello-i-am-a-dir")
fmt.Println(SysCd("/tmp/example-syscd", "@ls"))
*DryRunFlag = true
// Output:
// hello-i-am-a-dir
}
func ExampleSysSh() {
*DryRunFlag = false
show(SysSh("@rm -Rf /tmp/simple ; mkdir /tmp/simple ; ls -d /tmp/simple"))
SysSh("cd /tmp/simple ; touch hello ; ls")
*DryRunFlag = true
// Output:
// /tmp/simple
//
// hello
}
func ExampleDryRun() {
DryRunPush("first", "second")
fmt.Println(Sys("dummy"))
fmt.Println(Sys("dummy", "alpha", "beta"))
fmt.Printf("'%s'\n", Sys("dummy"))
// Output:
// dummy
// first
// dummy alpha beta
// second
// dummy
// ''
}
func ExampleDryRunErr() {
DryRunPush("first", "second", "!third")
out, err := SysErr("dummy")
fmt.Println(1, out, err)
out, err = SysErr("dummy", "alpha", "beta")
fmt.Println(2, out, err)
out, err = SysErr("dummy")
fmt.Println(3, "out", out, "err", err)
out, err = SysErr("dummy")
fmt.Println(4, "out", out, "err", err)
// Output:
// dummy
// 1 first <nil>
// dummy alpha beta
// 2 second <nil>
// dummy
// 3 out err third
// dummy
// 4 out err <nil>
}
func ExampleShowError() {
ShowError(nil)
ShowError(errors.New("error"))
// Output:
// error
}
func ExampleRun() {
//*DryRunFlag = false
DryRunPush("", "", "wrong")
fmt.Println(Run("docker pull busybox"))
fmt.Println(Run("@docker pull busybox"))
fmt.Println(Run("@docker pull busybox1"))
// Output:
// docker pull busybox
// <nil>
// <nil>
// wrong
}
func ExampleRandomString() {
re := regexp.MustCompile(`[a-zA-Z0-9]`)
rnd := RandomString(32)
fmt.Printf("%s\n", re.ReplaceAll([]byte(rnd), []byte("*")))
// Output:
// ********************************
}
func ExampleInput() {
DryRunPush("hello", "world")
fmt.Println(Input("how are you", "fine"))
fmt.Println(Input("", ""))
// Output:
// hello
// world
}
func ExampleSelect() {
DryRunPush("hello", "world")
fmt.Println(Select("how are you", "fine"))
fmt.Println(Select("", ""))
// Output:
// hello
// world
}