testify icon indicating copy to clipboard operation
testify copied to clipboard

[BUG] assertions.go: CallerInfo() Not Get Actual File Path When Workdir Changed

Open yishangru opened this issue 3 years ago • 1 comments

https://github.com/stretchr/testify/blob/181cea6eab8b2de7071383eca4be32a424db38dd/assert/assertions.go#L144

Above link directs to the line to blame. This will remove the absolute path info retrieved from runtime.Caller(i).

parts := strings.Split(file, "/")
file = parts[len(parts)-1]

Reproduce:

  1. set workdir before test start
  2. call assert.True(t, false)

yishangru avatar Sep 20 '22 14:09 yishangru

A simple fix would be like this:

		parts := strings.Split(file, "/")
		// file := parts[len(parts)-1]
		filename := parts[len(parts)-1]
		if len(parts) > 1 {
			dir := parts[len(parts)-2]
			//if (dir != "assert" && dir != "mock" && dir != "require") || file == "mock_test.go" {
			if (dir != "assert" && dir != "mock" && dir != "require") || filename == "mock_test.go" {
				//path, _ := filepath.Abs(file)
				callers = append(callers, fmt.Sprintf("%s:%d", file, line))
			}
		}

yishangru avatar Sep 20 '22 15:09 yishangru