Skip to content

Commit

Permalink
👔 up(fs): update the Name() func for handle empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 20, 2023
1 parent 4ab579b commit bec4ab9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions fsutil/check_test.go
Expand Up @@ -23,6 +23,7 @@ func TestFsUtil_common(t *testing.T) {
assert.Eq(t, "test.jpg", fsutil.PathName("testdata/test.jpg"))

assert.Eq(t, "test.jpg", fsutil.Name("path/to/test.jpg"))
assert.Eq(t, "", fsutil.Name(""))

if runtime.GOOS == "windows" {
assert.Eq(t, "path\\to", fsutil.Dir("path/to/test.jpg"))
Expand Down
7 changes: 6 additions & 1 deletion fsutil/info.go
Expand Up @@ -17,7 +17,12 @@ func PathName(fpath string) string { return path.Base(fpath) }
// Name get file/dir name from full path.
//
// eg: path/to/main.go => main.go
func Name(fpath string) string { return filepath.Base(fpath) }
func Name(fpath string) string {
if fpath == "" {
return ""
}
return filepath.Base(fpath)
}

// FileExt get filename ext. alias of path.Ext()
//
Expand Down
4 changes: 2 additions & 2 deletions testutil/httpmock.go
Expand Up @@ -85,13 +85,13 @@ func NewHttpRequest(method, path string, data *MD) *http.Request {
// body := strings.NewReader("string ...")
// res := MockRequest(handler, "POST", "/path", &MD{
// Body: body,
// Headers: M{"x-head": "val"}
// HeaderM: M{"x-head": "val"}
// })
//
// // with data 2
// res := MockRequest(handler, "POST", "/path", &MD{
// BodyString: "data string",
// Headers: M{"x-head": "val"}
// HeaderM: M{"x-head": "val"}
// })
func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder {
// w.Result() will return http.Response
Expand Down

0 comments on commit bec4ab9

Please sign in to comment.