fix FileWithLineNum func and add gormSourceDir unit test
This commit is contained in:
parent
3f2a3be940
commit
b2742c2b18
@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@ -14,17 +15,22 @@ var gormSourceDir string
|
||||
|
||||
func init() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
gormSourceDir = strings.Replace(file, `utils.utils.go`, "", -1)
|
||||
// Here is the directory to get the gorm source code. Here, the filepath.Dir mode is enough,
|
||||
// and the filepath is compatible with various operating systems
|
||||
gormSourceDir = filepath.Dir(filepath.Dir(file))
|
||||
}
|
||||
|
||||
// FileWithLineNum return the file name and line number of the current file
|
||||
func FileWithLineNum() string {
|
||||
for i := 2; i < 15; i++ {
|
||||
for i := 1; i < 15; i++ {
|
||||
_, file, line, ok := runtime.Caller(i)
|
||||
|
||||
// fmt.Println("file:", file, " line: ", line, "ok: ", ok)
|
||||
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
||||
return file + ":" + strconv.FormatInt(int64(line), 10)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
|
12
utils_filenum_test.go
Normal file
12
utils_filenum_test.go
Normal file
@ -0,0 +1,12 @@
|
||||
package gorm
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"gorm.io/gorm/utils"
|
||||
)
|
||||
|
||||
func TestFileWithLineNum(t *testing.T) {
|
||||
log.Println("FileWithLineNum: ", utils.FileWithLineNum())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user