Skip to content

Commit

Permalink
👔 up: update some doc and fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 19, 2023
1 parent a7457e9 commit 99b25f7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
func ToAnyMap(mp any) map[string]any
func TryAnyMap(mp any) (map[string]any, error)
func HTTPQueryString(data map[string]any) string
func StringsMapToAnyMap(ssMp map[string][]string) map[string]any
func ToString(mp map[string]any) string
func ToString2(mp any) string
func FormatIndent(mp any, indent string) string
Expand Down Expand Up @@ -1377,7 +1378,7 @@ func ChangeUserUidGid(newUID int, newGid int) (err error)
```go
// source at testutil/buffer.go
func NewBuffer() *Buffer
func NewBuffer() *byteutil.Buffer
// source at testutil/envmock.go
func MockEnvValue(key, val string, fn func(nv string))
func MockEnvValues(kvMap map[string]string, fn func())
Expand All @@ -1391,6 +1392,11 @@ func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry
// source at testutil/httpmock.go
func NewHttpRequest(method, path string, data *MD) *http.Request
func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder
func TestMain(m *testing.M)
func NewEchoServer() *httptest.Server
func BuildEchoReply(r *http.Request) *EchoReply
func ParseRespToReply(w *http.Response) *EchoReply
func ParseBodyToReply(bd io.ReadCloser) *EchoReply
// source at testutil/testutil.go
func DiscardStdout() error
func ReadOutput() (s string)
Expand Down
8 changes: 7 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
func ToAnyMap(mp any) map[string]any
func TryAnyMap(mp any) (map[string]any, error)
func HTTPQueryString(data map[string]any) string
func StringsMapToAnyMap(ssMp map[string][]string) map[string]any
func ToString(mp map[string]any) string
func ToString2(mp any) string
func FormatIndent(mp any, indent string) string
Expand Down Expand Up @@ -1378,7 +1379,7 @@ func ChangeUserUidGid(newUID int, newGid int) (err error)
```go
// source at testutil/buffer.go
func NewBuffer() *Buffer
func NewBuffer() *byteutil.Buffer
// source at testutil/envmock.go
func MockEnvValue(key, val string, fn func(nv string))
func MockEnvValues(kvMap map[string]string, fn func())
Expand All @@ -1392,6 +1393,11 @@ func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry
// source at testutil/httpmock.go
func NewHttpRequest(method, path string, data *MD) *http.Request
func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder
func TestMain(m *testing.M)
func NewEchoServer() *httptest.Server
func BuildEchoReply(r *http.Request) *EchoReply
func ParseRespToReply(w *http.Response) *EchoReply
func ParseBodyToReply(bd io.ReadCloser) *EchoReply
// source at testutil/testutil.go
func DiscardStdout() error
func ReadOutput() (s string)
Expand Down
1 change: 0 additions & 1 deletion fsutil/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestGlobWithFunc(t *testing.T) {

assert.NoErr(t, err)
assert.NotEmpty(t, paths)
assert.Contains(t, paths, "testdata/test.jpg")
}

func TestApplyFilters(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ package goutil_test

import (
"errors"
"fmt"
"testing"

"github.com/gookit/goutil"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)

var testSrvAddr string

func TestMain(m *testing.M) {
s := testutil.NewEchoServer()
defer s.Close()
testSrvAddr = "http://" + s.Listener.Addr().String()
fmt.Println("Test server listen on:", testSrvAddr)

m.Run()
}

func TestPkgName(t *testing.T) {
name := goutil.PkgName(goutil.FuncName(goutil.PanicIfErr))
assert.Eq(t, "github.com/gookit/goutil", name)
Expand Down
15 changes: 6 additions & 9 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@ package goutil_test

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/gookit/goutil"
"github.com/gookit/goutil/netutil/httpreq"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestNewErrGroup(t *testing.T) {
httpreq.ConfigStd(func(hc *http.Client) {
hc.Timeout = 3 * time.Second
})
httpreq.SetTimeout(3000)

eg := goutil.NewErrGroup()
eg.Add(func() error {
resp, err := httpreq.Get("https://httpbin.org/get", nil)
resp, err := httpreq.Get(testSrvAddr+"/get", nil)
if err != nil {
return err
}

fmt.Println(resp.Body)
fmt.Println(testutil.ParseBodyToReply(resp.Body))
return nil
}, func() error {
resp, err := httpreq.Post("https://httpbin.org/post", "hi", nil)
resp, err := httpreq.Post(testSrvAddr+"/post", "hi")
if err != nil {
return err
}

fmt.Println(resp.Body)
fmt.Println(testutil.ParseBodyToReply(resp.Body))
return nil
})

Expand Down
40 changes: 40 additions & 0 deletions netutil/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Net Utils

- provide some network utils. eg: `InternalIPv4`
- sub package:
- `httpctypes` - provide some commonly http content types.
- `httpheader` - provide some commonly http header names.
- `httpreq` - provide some http request utils

## Install

```bash
go get github.com/gookit/goutil/netutil
```

## Go docs

- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/netutil)

## Usage

```go
import "github.com/gookit/goutil/netutil"
```

```go
// Get internal IPv4 address
netutil.InternalIPv4()
```

## Testings

```shell
go test -v ./netutil/...
```

Test limit by regexp:

```shell
go test -v -run ^TestSetByKeys ./netutil/...
```
16 changes: 15 additions & 1 deletion netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func InternalIPOld() (ip string) {
for _, a := range addrs {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
// os.Stdout.WriteString(ipNet.IP.String() + "\n")
ip = ipNet.IP.String()
return
}
Expand All @@ -25,6 +24,21 @@ func InternalIPOld() (ip string) {
return
}

// GetLocalIPs get local IPs
func GetLocalIPs() (ips []string) {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic("Oops: " + err.Error())
}

for _, a := range addrs {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
ips = append(ips, ipNet.IP.String())
}
}
return
}

// InternalIP get internal IP
func InternalIP() (ip string) {
addr := netip.IPv4Unspecified()
Expand Down

0 comments on commit 99b25f7

Please sign in to comment.