Skip to content

Commit ac5dfa1

Browse files
committed
👔 up: test,base - update some test and base func
- upgrade deps to latest version
1 parent 66e926c commit ac5dfa1

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

basefn/basefn.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,41 @@ func MustOK(err error) {
5454
}
5555
}
5656

57-
// Must if error is not empty, will panic
57+
// Must return like (v, error). will panic on error, otherwise return v.
58+
//
59+
// Usage:
60+
//
61+
// // old
62+
// v, err := fn()
63+
// if err != nil {
64+
// panic(err)
65+
// }
66+
//
67+
// // new
68+
// v := goutil.Must(fn())
5869
func Must[T any](v T, err error) T {
5970
if err != nil {
6071
panic(err)
6172
}
6273
return v
6374
}
6475

76+
// MustIgnore for return like (v, error). Ignore return v and will panic on error.
77+
//
78+
// Useful for io, file operation func: (n int, err error)
79+
//
80+
// Usage:
81+
//
82+
// // old
83+
// _, err := fn()
84+
// if err != nil {
85+
// panic(err)
86+
// }
87+
//
88+
// // new
89+
// basefn.MustIgnore(fn())
90+
func MustIgnore(_ any, err error) { PanicErr(err) }
91+
6592
// ErrOnFail return input error on cond is false, otherwise return nil
6693
func ErrOnFail(cond bool, err error) error {
6794
return OrError(cond, err)

basefn/basefn_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,19 @@ func TestPanicIf(t *testing.T) {
3030
}
3131

3232
func TestPanicErr(t *testing.T) {
33+
basefn.MustOK(nil)
3334
basefn.PanicErr(nil)
3435
assert.Panics(t, func() {
3536
basefn.PanicErr(errors.New("a error"))
3637
})
38+
39+
// must ignore
40+
assert.NotPanics(t, func() {
41+
basefn.MustIgnore(nil, nil)
42+
})
43+
assert.Panics(t, func() {
44+
basefn.MustIgnore(nil, errors.New("a error"))
45+
})
3746
}
3847

3948
func TestPanicf(t *testing.T) {

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ go 1.19
44

55
require (
66
github.com/gookit/color v1.5.4
7-
golang.org/x/sync v0.5.0
8-
golang.org/x/sys v0.15.0
9-
golang.org/x/term v0.15.0
7+
golang.org/x/sync v0.6.0
8+
golang.org/x/sys v0.16.0
9+
golang.org/x/term v0.16.0
1010
golang.org/x/text v0.14.0
1111
)
1212

go.sum

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
66
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
77
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
88
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
9-
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
10-
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
11-
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
12-
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13-
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
14-
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
9+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
10+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
11+
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
12+
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13+
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
14+
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
1515
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
1616
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
1717
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

testutil/httpmock.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewHttpRequest(method, path string, data *MD) *http.Request {
3232
return NewHTTPRequest(method, path, data)
3333
}
3434

35-
// NewHTTPRequest for http testing
35+
// NewHTTPRequest quick create request for http testing
3636
// Usage:
3737
//
3838
// req := NewHttpRequest("GET", "/path", nil)
@@ -130,6 +130,21 @@ func (r *EchoReply) ContentType() string {
130130
return r.Headers["Content-Type"].(string)
131131
}
132132

133+
// EchoServer for testing http request.
134+
type EchoServer struct {
135+
*httptest.Server
136+
}
137+
138+
// HostAddr get host address. eg: 127.0.0.1:8999
139+
func (s *EchoServer) HostAddr() string {
140+
return s.Listener.Addr().String()
141+
}
142+
143+
// HTTPHost get http host address. eg: http://127.0.0.1:8999
144+
func (s *EchoServer) HTTPHost() string {
145+
return "http://" + s.HostAddr()
146+
}
147+
133148
// NewEchoServer create an echo server for testing.
134149
//
135150
// Usage on testing:
@@ -140,7 +155,7 @@ func (r *EchoReply) ContentType() string {
140155
// // create server
141156
// s := testutil.NewEchoServer()
142157
// defer s.Close()
143-
// testSrvAddr = "https://" + s.Listener.Addr().String()
158+
// testSrvAddr = s.HTTPHost()
144159
// fmt.Println("Test server listen on:", testSrvAddr)
145160
//
146161
// m.Run()
@@ -150,8 +165,8 @@ func (r *EchoReply) ContentType() string {
150165
// res := http.Get(testSrvAddr)
151166
// rpl := testutil.ParseRespToReply(res)
152167
// // assert ...
153-
func NewEchoServer() *httptest.Server {
154-
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
168+
func NewEchoServer() *EchoServer {
169+
hs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
155170
w.Header().Set("Content-Type", "application/json; charset=utf-8")
156171
w.Header().Set("Server", "goutil/echo-server")
157172
w.WriteHeader(http.StatusOK)
@@ -164,6 +179,10 @@ func NewEchoServer() *httptest.Server {
164179
_, _ = w.Write([]byte(`{"error": "encode error"}`))
165180
}
166181
}))
182+
183+
return &EchoServer{
184+
Server: hs,
185+
}
167186
}
168187

169188
// BuildEchoReply build reply body data

testutil/httpmock_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestMain(m *testing.M) {
1818
s := testutil.NewEchoServer()
1919
defer s.Close()
2020

21-
testSrvAddr = "https://" + s.Listener.Addr().String()
21+
testSrvAddr = s.HTTPHost()
2222
fmt.Println("server addr:", testSrvAddr)
2323

2424
m.Run()

0 commit comments

Comments
 (0)