| 1 |
// Copyright (c) 2021, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package trace_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"go/build" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/maxatome/go-testdeep/internal/test" |
| 17 |
"github.com/maxatome/go-testdeep/internal/trace" |
| 18 |
) |
| 19 |
|
| 20 |
func TestIgnorePackage(t *testing.T) { |
| 21 |
const ourPkg = "github.com/maxatome/go-testdeep/internal/trace_test" |
| 22 |
|
| 23 |
trace.Reset() |
| 24 |
|
| 25 |
test.IsFalse(t, trace.IsIgnoredPackage(ourPkg)) |
| 26 |
test.IsTrue(t, trace.IgnorePackage()) |
| 27 |
test.IsTrue(t, trace.IsIgnoredPackage(ourPkg)) |
| 28 |
|
| 29 |
test.IsTrue(t, trace.UnignorePackage()) |
| 30 |
test.IsFalse(t, trace.IsIgnoredPackage(ourPkg)) |
| 31 |
|
| 32 |
test.IsTrue(t, trace.IgnorePackage()) |
| 33 |
test.IsTrue(t, trace.IsIgnoredPackage(ourPkg)) |
| 34 |
|
| 35 |
test.IsFalse(t, trace.IgnorePackage(300)) |
| 36 |
test.IsFalse(t, trace.UnignorePackage(300)) |
| 37 |
} |
| 38 |
|
| 39 |
func TestFindGoModDir(t *testing.T) { |
| 40 |
tmp, err := os.MkdirTemp("", "go-testdeep") |
| 41 |
if err != nil { |
| 42 |
t.Fatalf("TempDir() failed: %s", err) |
| 43 |
} |
| 44 |
final := filepath.Join(tmp, "a", "b", "c", "d", "e") |
| 45 |
|
| 46 |
err = os.MkdirAll(final, 0755) |
| 47 |
if err != nil { |
| 48 |
t.Fatalf("MkdirAll(%s) failed: %s", final, err) |
| 49 |
} |
| 50 |
defer os.RemoveAll(tmp) //nolint: errcheck |
| 51 |
|
| 52 |
test.EqualStr(t, trace.FindGoModDir(final), "") |
| 53 |
|
| 54 |
t.Run("/tmp/.../a/b/c/go.mod", func(t *testing.T) { |
| 55 |
goMod := filepath.Join(tmp, "a", "b", "c", "go.mod") |
| 56 |
|
| 57 |
err := os.WriteFile(goMod, nil, 0644) |
| 58 |
if err != nil { |
| 59 |
t.Fatalf("WriteFile(%s) failed: %s", goMod, err) |
| 60 |
} |
| 61 |
defer os.Remove(goMod) //nolint: errcheck |
| 62 |
|
| 63 |
test.EqualStr(t, |
| 64 |
trace.FindGoModDir(final), |
| 65 |
filepath.Join(tmp, "a", "b", "c")+string(filepath.Separator), |
| 66 |
) |
| 67 |
}) |
| 68 |
|
| 69 |
t.Run("/tmp/go.mod", func(t *testing.T) { |
| 70 |
goMod := filepath.Join(os.TempDir(), "go.mod") |
| 71 |
|
| 72 |
if _, err := os.Stat(goMod); err != nil { |
| 73 |
if !os.IsNotExist(err) { |
| 74 |
t.Fatalf("Stat(%s) failed: %s", goMod, err) |
| 75 |
} |
| 76 |
err := os.WriteFile(goMod, nil, 0644) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("WriteFile(%s) failed: %s", goMod, err) |
| 79 |
} |
| 80 |
defer os.Remove(goMod) //nolint: errcheck |
| 81 |
} |
| 82 |
|
| 83 |
test.EqualStr(t, trace.FindGoModDir(final), "") |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
func TestFindGoModDirLinks(t *testing.T) { |
| 88 |
tmp, err := os.MkdirTemp("", "go-testdeep") |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("TempDir() failed: %s", err) |
| 91 |
} |
| 92 |
|
| 93 |
goModDir := filepath.Join(tmp, "a", "b", "c") |
| 94 |
truePath := filepath.Join(goModDir, "d", "e") |
| 95 |
linkPath := filepath.Join(tmp, "a", "b", "e") |
| 96 |
|
| 97 |
err = os.MkdirAll(truePath, 0755) |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("MkdirAll(%s) failed: %s", truePath, err) |
| 100 |
} |
| 101 |
defer os.RemoveAll(tmp) //nolint: errcheck |
| 102 |
|
| 103 |
err = os.Symlink(truePath, linkPath) |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("Symlink(%s, %s) failed: %s", truePath, linkPath, err) |
| 106 |
} |
| 107 |
|
| 108 |
goMod := filepath.Join(goModDir, "go.mod") |
| 109 |
|
| 110 |
err = os.WriteFile(goMod, nil, 0644) |
| 111 |
if err != nil { |
| 112 |
t.Fatalf("WriteFile(%s) failed: %s", goMod, err) |
| 113 |
} |
| 114 |
defer os.Remove(goMod) //nolint: errcheck |
| 115 |
|
| 116 |
goModDir += string(filepath.Separator) |
| 117 |
|
| 118 |
// Simple FindGoModDir |
| 119 |
test.EqualStr(t, trace.FindGoModDir(truePath), goModDir) |
| 120 |
test.EqualStr(t, trace.FindGoModDir(linkPath), "") // not found |
| 121 |
|
| 122 |
// FindGoModDirLinks |
| 123 |
test.EqualStr(t, trace.FindGoModDirLinks(truePath), goModDir) |
| 124 |
test.EqualStr(t, trace.FindGoModDirLinks(linkPath), goModDir) |
| 125 |
|
| 126 |
test.EqualStr(t, trace.FindGoModDirLinks(tmp), "") |
| 127 |
} |
| 128 |
|
| 129 |
func TestSplitPackageFunc(t *testing.T) { |
| 130 |
pkg, fn := trace.SplitPackageFunc("testing.Fatal") |
| 131 |
test.EqualStr(t, pkg, "testing") |
| 132 |
test.EqualStr(t, fn, "Fatal") |
| 133 |
|
| 134 |
pkg, fn = trace.SplitPackageFunc("github.com/maxatome/go-testdeep/td.Cmp") |
| 135 |
test.EqualStr(t, pkg, "github.com/maxatome/go-testdeep/td") |
| 136 |
test.EqualStr(t, fn, "Cmp") |
| 137 |
|
| 138 |
pkg, fn = trace.SplitPackageFunc("foo/bar/test.(*T).Cmp") |
| 139 |
test.EqualStr(t, pkg, "foo/bar/test") |
| 140 |
test.EqualStr(t, fn, "(*T).Cmp") |
| 141 |
|
| 142 |
pkg, fn = trace.SplitPackageFunc("foo/bar/test.(*X).c.func1") |
| 143 |
test.EqualStr(t, pkg, "foo/bar/test") |
| 144 |
test.EqualStr(t, fn, "(*X).c.func1") |
| 145 |
|
| 146 |
pkg, fn = trace.SplitPackageFunc("foo/bar/test.(*X).c.func1") |
| 147 |
test.EqualStr(t, pkg, "foo/bar/test") |
| 148 |
test.EqualStr(t, fn, "(*X).c.func1") |
| 149 |
|
| 150 |
pkg, fn = trace.SplitPackageFunc("foobar") |
| 151 |
test.EqualStr(t, pkg, "") |
| 152 |
test.EqualStr(t, fn, "foobar") |
| 153 |
|
| 154 |
pkg, fn = trace.SplitPackageFunc("") |
| 155 |
test.EqualStr(t, pkg, "") |
| 156 |
test.EqualStr(t, fn, "") |
| 157 |
} |
| 158 |
|
| 159 |
func d(end string) []trace.Level { return trace.Retrieve(0, end) } |
| 160 |
func c(end string) []trace.Level { return d(end) } |
| 161 |
func b(end string) []trace.Level { return c(end) } |
| 162 |
func a(end string) []trace.Level { return b(end) } |
| 163 |
|
| 164 |
func TestZRetrieve(t *testing.T) { |
| 165 |
trace.Reset() |
| 166 |
|
| 167 |
levels := a("testing.tRunner") |
| 168 |
if !test.EqualInt(t, len(levels), 5) || |
| 169 |
!test.EqualStr(t, levels[0].Func, "d") || |
| 170 |
!test.EqualStr(t, levels[0].Package, "github.com/maxatome/go-testdeep/internal/trace_test") || |
| 171 |
!test.EqualStr(t, levels[1].Func, "c") || |
| 172 |
!test.EqualStr(t, levels[1].Package, "github.com/maxatome/go-testdeep/internal/trace_test") || |
| 173 |
!test.EqualStr(t, levels[2].Func, "b") || |
| 174 |
!test.EqualStr(t, levels[2].Package, "github.com/maxatome/go-testdeep/internal/trace_test") || |
| 175 |
!test.EqualStr(t, levels[3].Func, "a") || |
| 176 |
!test.EqualStr(t, levels[3].Package, "github.com/maxatome/go-testdeep/internal/trace_test") || |
| 177 |
!test.EqualStr(t, levels[4].Func, "TestZRetrieve") || |
| 178 |
!test.EqualStr(t, levels[4].Package, "github.com/maxatome/go-testdeep/internal/trace_test") { |
| 179 |
t.Errorf("%#v", levels) |
| 180 |
} |
| 181 |
|
| 182 |
levels = trace.Retrieve(0, "unknown.unknown") |
| 183 |
maxLevels := len(levels) |
| 184 |
test.IsTrue(t, maxLevels > 2) |
| 185 |
test.EqualStr(t, levels[len(levels)-1].Func, "goexit") // runtime.goexit |
| 186 |
|
| 187 |
for i := range levels { |
| 188 |
test.IsTrue(t, trace.IgnorePackage(i)) |
| 189 |
} |
| 190 |
levels = trace.Retrieve(0, "unknown.unknown") |
| 191 |
test.EqualInt(t, len(levels), 0) |
| 192 |
|
| 193 |
// Init GOPATH filter |
| 194 |
trace.Reset() |
| 195 |
trace.Init() |
| 196 |
|
| 197 |
test.IsTrue(t, trace.IgnorePackage()) |
| 198 |
levels = trace.Retrieve(0, "unknown.unknown") |
| 199 |
test.EqualInt(t, len(levels), maxLevels-1) |
| 200 |
} |
| 201 |
|
| 202 |
type FakeFrames struct { |
| 203 |
frames []runtime.Frame |
| 204 |
cur int |
| 205 |
} |
| 206 |
|
| 207 |
func (f *FakeFrames) Next() (runtime.Frame, bool) { |
| 208 |
if f.cur >= len(f.frames) { |
| 209 |
return runtime.Frame{}, false |
| 210 |
} |
| 211 |
f.cur++ |
| 212 |
return f.frames[f.cur-1], f.cur < len(f.frames) |
| 213 |
} |
| 214 |
|
| 215 |
func TestZRetrieveFake(t *testing.T) { |
| 216 |
saveCallersFrames, saveGOPATH := trace.CallersFrames, build.Default.GOPATH |
| 217 |
defer func() { |
| 218 |
trace.CallersFrames, build.Default.GOPATH = saveCallersFrames, saveGOPATH |
| 219 |
}() |
| 220 |
|
| 221 |
var fakeFrames FakeFrames |
| 222 |
trace.CallersFrames = func(_ []uintptr) trace.Frames { return &fakeFrames } |
| 223 |
build.Default.GOPATH = "/foo/bar" |
| 224 |
|
| 225 |
trace.Reset() |
| 226 |
trace.Init() |
| 227 |
|
| 228 |
fakeFrames = FakeFrames{ |
| 229 |
frames: []runtime.Frame{ |
| 230 |
{}, |
| 231 |
{Function: "", File: "/foo/bar/src/zip/zip.go", Line: 23}, |
| 232 |
{Function: "", File: "/foo/bar/pkg/mod/zzz/zzz.go", Line: 42}, |
| 233 |
{Function: "", File: "/bar/foo.go", Line: 34}, |
| 234 |
{Function: "pkg.MyFunc"}, |
| 235 |
{}, |
| 236 |
}, |
| 237 |
} |
| 238 |
levels := trace.Retrieve(0, "pipo") |
| 239 |
if test.EqualInt(t, len(levels), 4) { |
| 240 |
test.EqualStr(t, levels[0].Func, "<unknown function>") |
| 241 |
test.EqualStr(t, levels[0].Package, "") |
| 242 |
test.EqualStr(t, levels[0].FileLine, "zip/zip.go:23") |
| 243 |
|
| 244 |
test.EqualStr(t, levels[1].Func, "<unknown function>") |
| 245 |
test.EqualStr(t, levels[1].Package, "") |
| 246 |
test.EqualStr(t, levels[1].FileLine, "zzz/zzz.go:42") |
| 247 |
|
| 248 |
test.EqualStr(t, levels[2].Func, "<unknown function>") |
| 249 |
test.EqualStr(t, levels[2].Package, "") |
| 250 |
test.EqualStr(t, levels[2].FileLine, "/bar/foo.go:34") |
| 251 |
|
| 252 |
test.EqualStr(t, levels[3].Func, "MyFunc") |
| 253 |
test.EqualStr(t, levels[3].Package, "pkg") |
| 254 |
test.EqualStr(t, levels[3].FileLine, "") |
| 255 |
} else { |
| 256 |
t.Errorf("%#v", levels) |
| 257 |
} |
| 258 |
} |