1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-27 22:49:15 +02:00

refactor: replace context.Background() with t.Context()/b.Context() in tests (#7352)

Based on the Go version we currently use, the dependency already
supports 1.24+, which allows using `t.Context()` and `b.Context()` in
unit tests and benchmarks respectively.

- Enable `context-background` and `context-todo` in
[`usetesting`](https://golangci-lint.run/docs/linters/configuration/#usetesting)
- Adjust the code to support linter detection

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
Flc゛
2025-09-23 15:52:45 +08:00
committed by GitHub
parent 2389f4488f
commit 80cb909774
106 changed files with 763 additions and 778 deletions

View File

@@ -4,7 +4,6 @@
package resource
import (
"context"
"fmt"
"testing"
@@ -19,7 +18,7 @@ func TestDetectOnePair(t *testing.T) {
t.Setenv(resourceAttrKey, "key=value")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, NewSchemaless(attribute.String("key", "value")), res)
}
@@ -28,7 +27,7 @@ func TestDetectURIEncodingOnePair(t *testing.T) {
t.Setenv(resourceAttrKey, "key=x+y+z?q=123")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, NewSchemaless(attribute.String("key", "x+y+z?q=123")), res)
}
@@ -38,7 +37,7 @@ func TestDetectMultiPairs(t *testing.T) {
t.Setenv(resourceAttrKey, "key=value, k = v , a= x, a=z, b=c%2Fd")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, NewSchemaless(
attribute.String("key", "value"),
@@ -53,7 +52,7 @@ func TestDetectURIEncodingMultiPairs(t *testing.T) {
t.Setenv("x", "1")
t.Setenv(resourceAttrKey, "key=x+y+z,namespace=localhost/test&verify")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, NewSchemaless(
attribute.String("key", "x+y+z"),
@@ -64,7 +63,7 @@ func TestDetectURIEncodingMultiPairs(t *testing.T) {
func TestEmpty(t *testing.T) {
t.Setenv(resourceAttrKey, " ")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, Empty(), res)
}
@@ -72,7 +71,7 @@ func TestEmpty(t *testing.T) {
func TestNoResourceAttributesSet(t *testing.T) {
t.Setenv(svcNameKey, "bar")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, res, NewSchemaless(
semconv.ServiceName("bar"),
@@ -82,7 +81,7 @@ func TestNoResourceAttributesSet(t *testing.T) {
func TestMissingKeyError(t *testing.T) {
t.Setenv(resourceAttrKey, "key=value,key")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
assert.Error(t, err)
assert.Equal(t, err, fmt.Errorf("%w: %v", errMissingValue, "[key]"))
assert.Equal(t, res, NewSchemaless(
@@ -93,7 +92,7 @@ func TestMissingKeyError(t *testing.T) {
func TestInvalidPercentDecoding(t *testing.T) {
t.Setenv(resourceAttrKey, "key=%invalid")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
assert.NoError(t, err)
assert.Equal(t, NewSchemaless(
attribute.String("key", "%invalid"),
@@ -104,7 +103,7 @@ func TestDetectServiceNameFromEnv(t *testing.T) {
t.Setenv(resourceAttrKey, "key=value,service.name=foo")
t.Setenv(svcNameKey, "bar")
detector := &fromEnv{}
res, err := detector.Detect(context.Background())
res, err := detector.Detect(t.Context())
require.NoError(t, err)
assert.Equal(t, res, NewSchemaless(
attribute.String("key", "value"),