Skip to content

Commit

Permalink
🎨 fmt: fix some code style for sysutil, dump, testutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 29, 2023
1 parent f39f74c commit b05e3ed
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestStruct_CannotExportField(t *testing.T) {
assert.Contains(t, str, "opt4: string(\"abc\"),")
}

func TestStruct_InterfaceField(t *testing.T) {
func ExampleStruct_interfaceField() {
myS1 := struct {
// cannotExport any // ok
cannotExport st1 // ok
Expand All @@ -251,7 +251,7 @@ func TestStruct_InterfaceField(t *testing.T) {
fmt.Println(myS1)
}

func TestStruct_MapInterfacedValue(t *testing.T) {
func ExampleStruct_mapInterfacedValue() {
myS2 := &struct {
cannotExport map[string]any
}{
Expand Down
1 change: 1 addition & 0 deletions netutil/httpctype/content_type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Package httpctype list some common http content-type
package httpctype

// Key is the header key of Content-Type
const Key = "Content-Type"

// there are some HTTP Content-Type with charset of the most common data formats.
Expand Down
1 change: 1 addition & 0 deletions netutil/httpheader/header.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package httpheader provides some common http header names.
package httpheader

// some common http header names
Expand Down
13 changes: 10 additions & 3 deletions sysutil/user_nonwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ import (
)

// ChangeUserByName change work user by new username.
func ChangeUserByName(newUname string) (err error) {
func ChangeUserByName(newUname string) error {
u := MustFindUser(newUname)
// syscall.Setlogin(newUname)
return ChangeUserUidGid(strutil.IntOrPanic(u.Uid), strutil.IntOrPanic(u.Gid))
return ChangeUserUIDGid(strutil.IntOrPanic(u.Uid), strutil.IntOrPanic(u.Gid))
}

// ChangeUserUidGid change work user by new username uid,gid
func ChangeUserUidGid(newUID int, newGid int) (err error) {
//
// Deprecated: use ChangeUserUIDGid instead
func ChangeUserUidGid(newUID int, newGid int) error {

Check warning on line 21 in sysutil/user_nonwin.go

View workflow job for this annotation

GitHub Actions / Test on go 1.20 and ubuntu-latest

func ChangeUserUidGid should be ChangeUserUIDGid
return ChangeUserUIDGid(newUID, newGid)
}

// ChangeUserUIDGid change work user by new username uid,gid
func ChangeUserUIDGid(newUID int, newGid int) (err error) {
if newUID > 0 {
err = syscall.Setuid(newUID)

Expand Down
9 changes: 8 additions & 1 deletion sysutil/user_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ package sysutil

// ChangeUserByName change work user by new username.
func ChangeUserByName(newUname string) (err error) {
return ChangeUserUidGid(0, 0)
return ChangeUserUIDGid(0, 0)
}

// ChangeUserUidGid change work user by new username uid,gid
//
// Deprecated: use ChangeUserUIDGid instead
func ChangeUserUidGid(newUid int, newGid int) (err error) {
return ChangeUserUIDGid(newUid, newGid)
}

// ChangeUserUIDGid change work user by new username uid,gid
func ChangeUserUIDGid(newUid int, newGid int) (err error) {
return nil
}
1 change: 1 addition & 0 deletions testutil/assert/assertions_methods.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package assert

// Nil asserts that the given is a nil value
func (as *Assertions) Nil(give any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Nil(as.t, give, fmtAndArgs...)
Expand Down
9 changes: 9 additions & 0 deletions testutil/fakeobj/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ func (f *FileInfo) Reset() *FileInfo {

// fs.File methods.

// Stat returns the FileInfo structure describing file.
func (f *FileInfo) Stat() (fs.FileInfo, error) {
return f, nil
}

// Read reads up to len(p) bytes into p.
func (f *FileInfo) Read(p []byte) (int, error) {
if f.offset >= len(f.Contents) {
return 0, io.EOF
Expand All @@ -109,32 +111,39 @@ func (f *FileInfo) Read(p []byte) (int, error) {
return n, nil
}

// Close closes the file
func (f *FileInfo) Close() error {
return f.CloseErr
}

// fs.FileInfo methods.

// Name returns the base name of the file.
func (f *FileInfo) Name() string {
return f.Nam
}

// Size returns the length in bytes for regular files; system-dependent for others.
func (f *FileInfo) Size() int64 {
return int64(len(f.Contents))
}

// Mode returns file mode bits.
func (f *FileInfo) Mode() fs.FileMode {
return f.Mod
}

// ModTime returns the modification time.
func (f *FileInfo) ModTime() time.Time {
return f.Mt
}

// IsDir returns true if the file is a directory.
func (f *FileInfo) IsDir() bool {
return f.Dir
}

// Sys returns underlying data source (can return nil).
func (f *FileInfo) Sys() any {
return nil
}
1 change: 1 addition & 0 deletions testutil/fakeobj/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ func TestNewFileInfo(t *testing.T) {
assert.True(t, fi.IsDir())
assert.Gt(t, int(fi.Mode()), 0)
assert.Equal(t, "dir", fi.Name())
assert.Nil(t, fi.Sys())
}
13 changes: 10 additions & 3 deletions testutil/httpmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ type (
}
)

// NewHttpRequest for http testing
// NewHttpRequest for http testing, alias of NewHTTPRequest()
//
// Deprecated: use NewHTTPRequest() instead.
func NewHttpRequest(method, path string, data *MD) *http.Request {

Check warning on line 31 in testutil/httpmock.go

View workflow job for this annotation

GitHub Actions / Test on go 1.20 and ubuntu-latest

func NewHttpRequest should be NewHTTPRequest
return NewHTTPRequest(method, path, data)
}

// NewHTTPRequest for http testing
// Usage:
//
// req := NewHttpRequest("GET", "/path", nil)
Expand All @@ -42,7 +49,7 @@ type (
// BodyString: "data string",
// Headers: M{"x-head": "val"}
// })
func NewHttpRequest(method, path string, data *MD) *http.Request {
func NewHTTPRequest(method, path string, data *MD) *http.Request {
var body io.Reader
if data != nil {
if data.Body != nil {
Expand Down Expand Up @@ -97,7 +104,7 @@ func NewHttpRequest(method, path string, data *MD) *http.Request {
func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder {
// w.Result() will return http.Response
w := httptest.NewRecorder()
r := NewHttpRequest(method, path, data)
r := NewHTTPRequest(method, path, data)

// s := httptest.NewServer()
h.ServeHTTP(w, r)
Expand Down

0 comments on commit b05e3ed

Please sign in to comment.