Skip to content

Commit

Permalink
Use T.Setenv instead of own setenv func
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed May 26, 2022
1 parent 12a4952 commit 0b37e5b
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions propagators/autoprop/propagator_test.go
Expand Up @@ -16,7 +16,6 @@ package autoprop

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,22 +24,6 @@ import (
"go.opentelemetry.io/otel/propagation"
)

// setenv sets and unsets an environment variable in the context of a test
// run. It is used instead of t.Setenv so as to support Go <1.17. It can be
// replaced with t.Setenv(key, value) when this support is dropped.
func setenv(t *testing.T, key, value string) {
prevValue, ok := os.LookupEnv(key)
if err := os.Setenv(key, value); err != nil {
t.Fatalf("cannot set environment variable: %v", err)
}

if ok {
t.Cleanup(func() { _ = os.Setenv(key, prevValue) })
} else {
t.Cleanup(func() { _ = os.Unsetenv(key) })
}
}

type handler struct {
err error
}
Expand All @@ -52,7 +35,7 @@ func TestNewTextMapPropagatorInvalidEnvVal(t *testing.T) {
otel.SetErrorHandler(h)

const name = "invalid-name"
setenv(t, otelPropagatorsEnvKey, name)
t.Setenv(otelPropagatorsEnvKey, name)
_ = NewTextMapPropagator()
assert.ErrorIs(t, h.err, errUnknownPropagator)
}
Expand All @@ -79,6 +62,6 @@ func TestNewTextMapPropagatorSingleNoOverhead(t *testing.T) {
}

func TestNewTextMapPropagatorMultiEnvNone(t *testing.T) {
setenv(t, otelPropagatorsEnvKey, "b3,none,tracecontext")
t.Setenv(otelPropagatorsEnvKey, "b3,none,tracecontext")
assert.Equal(t, noop, NewTextMapPropagator())
}

0 comments on commit 0b37e5b

Please sign in to comment.