Skip to content

Commit

Permalink
Fix notifier tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DariaKunoichi committed Feb 19, 2024
1 parent e004cea commit a41982d
Showing 1 changed file with 79 additions and 16 deletions.
95 changes: 79 additions & 16 deletions v2/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bugsnag_test

import (
"fmt"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -61,21 +62,52 @@ func TestStackframesAreSkippedCorrectly(t *testing.T) {
defer notifier.AutoNotify()
crash("NaN")
}()
assertStackframesMatch(t, []errors.StackFrame{
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4", File: "notifier_test.go"},
})

var expected []errors.StackFrame
golangVersion := runtime.Version()
// TODO remove this after dropping support for Golang 1.11
// Golang version < 1.12 cannot unwrap inlined functions correctly.
if strings.HasPrefix(golangVersion, "go1.11") {
expected = append(expected,
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4.1", File: "notifier_test.go"}, //inlined crash func
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4", File: "notifier_test.go"},
)
} else {
expected = append(expected,
errors.StackFrame{Name: "crash", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func4", File: "notifier_test.go"},
)
}

assertStackframesMatch(t, expected)
})
t.Run("bugsnag.AutoNotify", func(st *testing.T) {
func() {
defer func() { recover() }()
defer bugsnag.AutoNotify()
crash("NaN")
}()
assertStackframesMatch(t, []errors.StackFrame{
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5", File: "notifier_test.go"},
})
var expected []errors.StackFrame
golangVersion := runtime.Version()
// TODO remove this after dropping support for Golang 1.11
// Golang version < 1.12 cannot unwrap inlined functions correctly.
if strings.HasPrefix(golangVersion, "go1.11") {
expected = append(expected,
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5.1", File: "notifier_test.go"}, //inlined crash func
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5", File: "notifier_test.go"},
)
} else {
expected = append(expected,
errors.StackFrame{Name: "crash", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func5", File: "notifier_test.go"},
)
}

assertStackframesMatch(t, expected)
})

// Expect the following frames to be present for *.Recover
Expand All @@ -92,20 +124,50 @@ func TestStackframesAreSkippedCorrectly(t *testing.T) {
defer notifier.Recover()
crash("NaN")
}()
assertStackframesMatch(t, []errors.StackFrame{
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6", File: "notifier_test.go"},
})
var expected []errors.StackFrame
golangVersion := runtime.Version()
// TODO remove this after dropping support for Golang 1.11
// Golang version < 1.12 cannot unwrap inlined functions correctly.
if strings.HasPrefix(golangVersion, "go1.11") {
expected = append(expected,
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6.1", File: "notifier_test.go"}, //inlined crash func
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6", File: "notifier_test.go"},
)
} else {
expected = append(expected,
errors.StackFrame{Name: "crash", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func6", File: "notifier_test.go"},
)
}

assertStackframesMatch(t, expected)
})
t.Run("bugsnag.Recover", func(st *testing.T) {
func() {
defer bugsnag.Recover()
crash("NaN")
}()
assertStackframesMatch(t, []errors.StackFrame{
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7", File: "notifier_test.go"},
})
var expected []errors.StackFrame
golangVersion := runtime.Version()
// TODO remove this after dropping support for Golang 1.11
// Golang version < 1.12 cannot unwrap inlined functions correctly.
if strings.HasPrefix(golangVersion, "go1.11") {
expected = append(expected,
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7.1", File: "notifier_test.go"}, //inlined crash func
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7", File: "notifier_test.go"},
)
} else {
expected = append(expected,
errors.StackFrame{Name: "crash", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7.1", File: "notifier_test.go"},
errors.StackFrame{Name: "TestStackframesAreSkippedCorrectly.func7", File: "notifier_test.go"},
)
}

assertStackframesMatch(t, expected)
})
}

Expand Down Expand Up @@ -187,6 +249,7 @@ func assertStackframesMatch(t *testing.T, expected []errors.StackFrame) {
if strings.HasSuffix(file, expectedFrame.File) && expectedFrame.Name == method {
lastmatch = index
matched++
break
}
}
}
Expand Down

0 comments on commit a41982d

Please sign in to comment.