Skip to content

Commit

Permalink
👔 up: test,fs - update some test util and fs util logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 4, 2023
1 parent 66764f6 commit c7af933
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion comdef/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type IntOrFloat interface {
Int | Float
}

// XintOrFloat interface type. all (x)int and float types
// XintOrFloat interface type. all int, uint and float types
type XintOrFloat interface {
Int | Uint | Float
}
Expand Down
6 changes: 3 additions & 3 deletions fsutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func PathExists(path string) bool {

// IsDir reports whether the named directory exists.
func IsDir(path string) bool {
if path == "" {
if path == "" || len(path) > 468 {
return false
}

Expand All @@ -61,7 +61,7 @@ func FileExists(path string) bool {

// IsFile reports whether the named file or directory exists.
func IsFile(path string) bool {
if path == "" {
if path == "" || len(path) > 468 {
return false
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func IsZipFile(filepath string) bool {
return bytes.Equal(buf, []byte("PK\x03\x04"))
}

// PathMatch check for a string.
// PathMatch check for a string. alias of path.Match()
func PathMatch(pattern, s string) bool {
ok, err := path.Match(pattern, s)
if err != nil {
Expand Down
31 changes: 25 additions & 6 deletions fsutil/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func MkParentDir(fpath string) error {
// open/create files
// ************************************************************

// some flag consts for open file
// some commonly flag consts for open file
const (
FsCWAFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND // create, append write-only
FsCWTFlags = os.O_CREATE | os.O_WRONLY | os.O_TRUNC // create, override write-only
Expand All @@ -60,6 +60,10 @@ const (
)

// OpenFile like os.OpenFile, but will auto create dir.
//
// Usage:
//
// file, err := OpenFile("path/to/file.txt", FsCWFlags, 0666)
func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) {
fileDir := path.Dir(filepath)
if err := os.MkdirAll(fileDir, DefaultDirPerm); err != nil {
Expand All @@ -73,22 +77,37 @@ func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) {
return file, nil
}

/* TODO MustOpenFile() */
// MustOpenFile like os.OpenFile, but will auto create dir.
//
// Usage:
//
// file := MustOpenFile("path/to/file.txt", FsCWFlags, 0666)
func MustOpenFile(filepath string, flag int, perm os.FileMode) *os.File {
file, err := OpenFile(filepath, flag, perm)
if err != nil {
panic(err)
}
return file
}

// QuickOpenFile like os.OpenFile, open for append write. if not exists, will create it.
//
// Alias of OpenAppendFile()
func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error) {
flag := basefn.FirstOr(fileFlag, FsCWAFlags)
return OpenFile(filepath, flag, DefaultFilePerm)
}

// OpenAppendFile like os.OpenFile, open for append write. if not exists, will create it.
func OpenAppendFile(filepath string) (*os.File, error) {
return OpenFile(filepath, FsCWAFlags, DefaultFilePerm)
func OpenAppendFile(filepath string, filePerm ...os.FileMode) (*os.File, error) {
perm := basefn.FirstOr(filePerm, DefaultFilePerm)
return OpenFile(filepath, FsCWAFlags, perm)
}

// OpenTruncFile like os.OpenFile, open for override write. if not exists, will create it.
func OpenTruncFile(filepath string) (*os.File, error) {
return OpenFile(filepath, FsCWTFlags, DefaultFilePerm)
func OpenTruncFile(filepath string, filePerm ...os.FileMode) (*os.File, error) {
perm := basefn.FirstOr(filePerm, DefaultFilePerm)
return OpenFile(filepath, FsCWTFlags, perm)
}

// OpenReadFile like os.OpenFile, open file for read contents
Expand Down
8 changes: 4 additions & 4 deletions testutil/assert/assertions_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,28 @@ func (as *Assertions) NotEqual(want, give any, fmtAndArgs ...any) *Assertions {
}

// Lt asserts that the give(intX) should not be less than max
func (as *Assertions) Lt(give, max int, fmtAndArgs ...any) *Assertions {
func (as *Assertions) Lt(give, max any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Lt(as.t, give, max, fmtAndArgs...)
return as
}

// Lte asserts that the give(intX) should not be less than or equal to max
func (as *Assertions) Lte(give, max int, fmtAndArgs ...any) *Assertions {
func (as *Assertions) Lte(give, max any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Lte(as.t, give, max, fmtAndArgs...)
return as
}

// Gt asserts that the give(intX) should not be greater than min
func (as *Assertions) Gt(give, min int, fmtAndArgs ...any) *Assertions {
func (as *Assertions) Gt(give, min any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Gt(as.t, give, min, fmtAndArgs...)
return as
}

// Gte asserts that the give(intX) should not be greater than or equal to min
func (as *Assertions) Gte(give, min int, fmtAndArgs ...any) *Assertions {
func (as *Assertions) Gte(give, min any, fmtAndArgs ...any) *Assertions {
as.t.Helper()
as.ok = Gte(as.t, give, min, fmtAndArgs...)
return as
Expand Down
38 changes: 22 additions & 16 deletions testutil/assert/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,38 +504,44 @@ func NotEq(t TestingT, want, give any, fmtAndArgs ...any) bool {
return true
}

// Lt asserts that the give(intX) should not be less than max
func Lt(t TestingT, give, max int, fmtAndArgs ...any) bool {
gInt, err := mathutil.ToInt(give)
if err == nil && gInt < max {
// Lt asserts that the give(intX,uintX,floatX) should not be less than max
func Lt(t TestingT, give, max any, fmtAndArgs ...any) bool {
if mathutil.Compare(give, max, "lt") {
return true
}

t.Helper()
return fail(t, fmt.Sprintf("Given should later than or equal %d(but was %d)", max, gInt), fmtAndArgs)
return fail(t, fmt.Sprintf("Given %v should later than %v", give, max), fmtAndArgs)
}

// Lte asserts that the give(intX) should not be less than or equals to max
func Lte(t TestingT, give, max int, fmtAndArgs ...any) bool {
// Lte asserts that the give(intX,uintX,floatX) should not be less than or equals to max
func Lte(t TestingT, give, max any, fmtAndArgs ...any) bool {
if mathutil.Compare(give, max, "lte") {
return true
}

t.Helper()
return Lt(t, give, max+1, fmtAndArgs...)
return fail(t, fmt.Sprintf("Given %v should later than %v", give, max), fmtAndArgs)
}

// Gt asserts that the give(intX) should not be greater than min
func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool {
gInt, err := mathutil.ToInt(give)
if err == nil && gInt > min {
// Gt asserts that the give(intX,uintX,floatX) should not be greater than min
func Gt(t TestingT, give, min any, fmtAndArgs ...any) bool {
if mathutil.Compare(give, min, "gt") {
return true
}

t.Helper()
return fail(t, fmt.Sprintf("Given should gater than or equal %d(but was %d)", min, gInt), fmtAndArgs)
return fail(t, fmt.Sprintf("Given %v should gater than %v", give, min), fmtAndArgs)
}

// Gte asserts that the give(intX) should not be greater than or equals to min
func Gte(t TestingT, give, min int, fmtAndArgs ...any) bool {
// Gte asserts that the give(intX,uintX,floatX) should not be greater than or equals to min
func Gte(t TestingT, give, min any, fmtAndArgs ...any) bool {
if mathutil.Compare(give, min, "gte") {
return true
}

t.Helper()
return Gt(t, give, min-1, fmtAndArgs...)
return fail(t, fmt.Sprintf("Given %v should gater than or equal %v", give, min), fmtAndArgs)
}

// IsType assert data type equals
Expand Down
13 changes: 12 additions & 1 deletion testutil/fsmock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package testutil

import "io/fs"
import (
"io/fs"
"path"

"github.com/gookit/goutil/basefn"
)

// DirEnt create a fs.DirEntry
type DirEnt struct {
Expand All @@ -11,6 +16,12 @@ type DirEnt struct {
Err error
}

// NewDirEnt create a fs.DirEntry
func NewDirEnt(fpath string, isDir ...bool) *DirEnt {
isd := basefn.FirstOr(isDir, false)
return &DirEnt{Nam: path.Base(fpath), Dir: isd, Typ: fs.ModePerm}
}

// Name get
func (d *DirEnt) Name() string {
return d.Nam
Expand Down

0 comments on commit c7af933

Please sign in to comment.