fix FileWithLineNum compatible with various platforms

This commit is contained in:
daheige 2021-06-11 21:20:19 +08:00
parent 5b65b02805
commit 07e8943443
2 changed files with 9 additions and 3 deletions

View File

@ -15,8 +15,9 @@ var gormSourceDir string
func init() {
_, file, _, _ := runtime.Caller(0)
// 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
// the separator in the path returned by runtime.Caller is /,
// which is a known problem with go, but for compatibility with the path,
// the filepath.Dir mode is used here
gormSourceDir = filepath.Dir(filepath.Dir(file))
}
@ -24,7 +25,8 @@ func init() {
func FileWithLineNum() string {
for i := 1; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
// compatible with various platforms
if ok && (!strings.HasPrefix(filepath.Dir(file), gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
return file + ":" + strconv.FormatInt(int64(line), 10)
}
}

View File

@ -12,3 +12,7 @@ func TestIsValidDBNameChar(t *testing.T) {
}
}
}
func TestFileWithLineNum(t *testing.T) {
t.Log("file line with num: ", FileWithLineNum())
}