chore(tests): some test improvements (#725)

This commit is contained in:
Samuel Berthe
2025-11-05 17:36:41 +01:00
committed by GitHub
parent a2f72cce17
commit a86ec54bc6
4 changed files with 42 additions and 12 deletions
+13 -6
View File
@@ -10,14 +10,21 @@
- [ ] 🧪 This helper is tested
- [ ] 🏎️ My code limits memory allocation and is fast
- [ ] 🧞‍♂️ This helper is immutable and my tests prove it
- [ ] ✍️ I implemented the parallel and mutable variants
- [ ] 📖 My helper has been added to README
- [ ] 🔬 An example has been added to xxxxx_example_test.go
- [ ] ⛹️ An example has been created on https://go.dev/play
- [ ] ✍️ I implemented the parallel, iterator and mutable variants
- [ ] 🔬 An example has been added to lo_example_test.go
- [ ] ⛹️ An example has been created on https://go.dev/play and added in comments
- [ ] 📖 My helper has been added to documentation
- [ ] in README.md
- [ ] in docs/data/*.md
- [ ] in docs/static/llms.txt
## Conventions
- Returning `(ok bool)` is often better than `(err error)`
- `panic(...)` must be limited
- Helpers should allow batching (eg: receive variadic arguments)
- Use an index at the end of the helper name to declare variants (eg: `lo.Must0`, `lo.Must1`, `lo.Must2`...)
- Helpers should receive variadic arguments when relevent
- Add variants of your helper when relevant:
- Variable number of arguments: `lo.Must0`, `lo.Must1`, `lo.Must2`, ...
- Predicate with index: `lo.SliceToMap` vs `lo.SliceToMapI`
- With lazy callback: `lo.Ternary` vs `lo.TernaryF`
- ...
+2 -1
View File
@@ -35,8 +35,9 @@ jobs:
- name: Test
run: make test
- name: Test
- name: Test coverage
run: make coverage
if: matrix.go == '1.18'
- name: Codecov
uses: codecov/codecov-action@v5
+27 -4
View File
@@ -1,6 +1,9 @@
package lo
import (
"fmt"
"runtime"
"strconv"
"testing"
"time"
@@ -18,14 +21,34 @@ func testWithTimeout(t *testing.T, timeout time.Duration) {
t.Helper()
testFinished := make(chan struct{})
t.Cleanup(func() { close(testFinished) })
go func() { //nolint:staticcheck
t.Cleanup(func() {
close(testFinished)
})
line := ""
funcName := ""
var pc [1]uintptr
n := runtime.Callers(2, pc[:])
if n > 0 {
frames := runtime.CallersFrames(pc[:])
frame, _ := frames.Next()
line = frame.File + ":" + strconv.Itoa(frame.Line)
funcName = frame.Function
}
go func() {
select {
case <-testFinished:
case <-time.After(timeout):
t.Errorf("test timed out after %s", timeout)
t.FailNow() //nolint:govet,staticcheck
if line == "" || funcName == "" {
panic(fmt.Sprintf("Test timed out after: %v", timeout))
}
panic(fmt.Sprintf("%s: Test timed out after: %v\n%s", funcName, timeout, line))
// t.Errorf("Test timed out after: %v", timeout)
// os.Exit(1)
}
}()
}
-1
View File
@@ -1,5 +1,4 @@
//go:build !race
// +build !race
package lo