fix FileWithLineNum compatible with various platforms
This commit is contained in:
parent
5b65b02805
commit
07e8943443
@ -15,8 +15,9 @@ var gormSourceDir string
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_, file, _, _ := runtime.Caller(0)
|
_, file, _, _ := runtime.Caller(0)
|
||||||
// Here is the directory to get the gorm source code. Here, the filepath.Dir mode is enough,
|
// the separator in the path returned by runtime.Caller is /,
|
||||||
// and the filepath is compatible with various operating systems
|
// 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))
|
gormSourceDir = filepath.Dir(filepath.Dir(file))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +25,8 @@ func init() {
|
|||||||
func FileWithLineNum() string {
|
func FileWithLineNum() string {
|
||||||
for i := 1; i < 15; i++ {
|
for i := 1; i < 15; i++ {
|
||||||
_, file, line, ok := runtime.Caller(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)
|
return file + ":" + strconv.FormatInt(int64(line), 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,3 +12,7 @@ func TestIsValidDBNameChar(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFileWithLineNum(t *testing.T) {
|
||||||
|
t.Log("file line with num: ", FileWithLineNum())
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user