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

Remove Unused env.go and env_test.go; Update gen.go in internal (#6556)

This PR removes the env.go and env_test.go files from the
/internal/internaltest directory, as they are no longer used in the
project.

Additionally, this PR updates the gen.go file in the internal package to
reflect the removal of these unused files.

Closes
[#6526](https://github.com/open-telemetry/opentelemetry-go/issues/6520).
This commit is contained in:
Meyank
2025-03-28 15:43:43 +05:30
committed by GitHub
parent 03fa67d907
commit b17e9748d2
3 changed files with 0 additions and 318 deletions

View File

@@ -7,8 +7,6 @@ package internal // import "go.opentelemetry.io/otel/internal"
//go:generate gotmpl --body=./shared/matchers/expecter.go.tmpl "--data={}" --out=matchers/expecter.go
//go:generate gotmpl --body=./shared/matchers/temporal_matcher.go.tmpl "--data={}" --out=matchers/temporal_matcher.go
//go:generate gotmpl --body=./shared/internaltest/env.go.tmpl "--data={}" --out=internaltest/env.go
//go:generate gotmpl --body=./shared/internaltest/env_test.go.tmpl "--data={}" --out=internaltest/env_test.go
//go:generate gotmpl --body=./shared/internaltest/errors.go.tmpl "--data={}" --out=internaltest/errors.go
//go:generate gotmpl --body=./shared/internaltest/harness.go.tmpl "--data={\"matchersImportPath\": \"go.opentelemetry.io/otel/internal/matchers\"}" --out=internaltest/harness.go
//go:generate gotmpl --body=./shared/internaltest/text_map_carrier.go.tmpl "--data={}" --out=internaltest/text_map_carrier.go

View File

@@ -1,90 +0,0 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/internaltest/env.go.tmpl
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internaltest // import "go.opentelemetry.io/otel/internal/internaltest"
import (
"os"
)
type Env struct {
Name string
Value string
Exists bool
}
// EnvStore stores and recovers environment variables.
type EnvStore interface {
// Records the environment variable into the store.
Record(key string)
// Restore recovers the environment variables in the store.
Restore() error
}
var _ EnvStore = (*envStore)(nil)
type envStore struct {
store map[string]Env
}
func (s *envStore) add(env Env) {
s.store[env.Name] = env
}
func (s *envStore) Restore() error {
var err error
for _, v := range s.store {
if v.Exists {
err = os.Setenv(v.Name, v.Value)
} else {
err = os.Unsetenv(v.Name)
}
if err != nil {
return err
}
}
return nil
}
func (s *envStore) setEnv(key, value string) error {
s.Record(key)
err := os.Setenv(key, value)
if err != nil {
return err
}
return nil
}
func (s *envStore) Record(key string) {
originValue, exists := os.LookupEnv(key)
s.add(Env{
Name: key,
Value: originValue,
Exists: exists,
})
}
func NewEnvStore() EnvStore {
return newEnvStore()
}
func newEnvStore() *envStore {
return &envStore{store: make(map[string]Env)}
}
func SetEnvVariables(env map[string]string) (EnvStore, error) {
envStore := newEnvStore()
for k, v := range env {
err := envStore.setEnv(k, v)
if err != nil {
return nil, err
}
}
return envStore, nil
}

View File

@@ -1,226 +0,0 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/internaltest/env_test.go.tmpl
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internaltest
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type EnvStoreTestSuite struct {
suite.Suite
}
func (s *EnvStoreTestSuite) Test_add() {
envStore := newEnvStore()
e := Env{
Name: "name",
Value: "value",
Exists: true,
}
envStore.add(e)
envStore.add(e)
s.Len(envStore.store, 1)
}
func (s *EnvStoreTestSuite) TestRecord() {
testCases := []struct {
name string
env Env
expectedEnvStore *envStore
}{
{
name: "record exists env",
env: Env{
Name: "name",
Value: "value",
Exists: true,
},
expectedEnvStore: &envStore{store: map[string]Env{
"name": {
Name: "name",
Value: "value",
Exists: true,
},
}},
},
{
name: "record exists env, but its value is empty",
env: Env{
Name: "name",
Value: "",
Exists: true,
},
expectedEnvStore: &envStore{store: map[string]Env{
"name": {
Name: "name",
Value: "",
Exists: true,
},
}},
},
{
name: "record not exists env",
env: Env{
Name: "name",
Exists: false,
},
expectedEnvStore: &envStore{store: map[string]Env{
"name": {
Name: "name",
Exists: false,
},
}},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
if tc.env.Exists {
s.NoError(os.Setenv(tc.env.Name, tc.env.Value))
}
envStore := newEnvStore()
envStore.Record(tc.env.Name)
s.Equal(tc.expectedEnvStore, envStore)
if tc.env.Exists {
s.NoError(os.Unsetenv(tc.env.Name))
}
})
}
}
func (s *EnvStoreTestSuite) TestRestore() {
testCases := []struct {
name string
env Env
expectedEnvValue string
expectedEnvExists bool
}{
{
name: "exists env",
env: Env{
Name: "name",
Value: "value",
Exists: true,
},
expectedEnvValue: "value",
expectedEnvExists: true,
},
{
name: "no exists env",
env: Env{
Name: "name",
Exists: false,
},
expectedEnvExists: false,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
envStore := newEnvStore()
envStore.add(tc.env)
// Backup
backup := newEnvStore()
backup.Record(tc.env.Name)
s.Require().NoError(os.Unsetenv(tc.env.Name))
s.NoError(envStore.Restore())
v, exists := os.LookupEnv(tc.env.Name)
s.Equal(tc.expectedEnvValue, v)
s.Equal(tc.expectedEnvExists, exists)
// Restore
s.Require().NoError(backup.Restore())
})
}
}
func (s *EnvStoreTestSuite) Test_setEnv() {
testCases := []struct {
name string
key string
value string
expectedEnvStore *envStore
expectedEnvValue string
expectedEnvExists bool
}{
{
name: "normal",
key: "name",
value: "value",
expectedEnvStore: &envStore{store: map[string]Env{
"name": {
Name: "name",
Value: "other value",
Exists: true,
},
}},
expectedEnvValue: "value",
expectedEnvExists: true,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
envStore := newEnvStore()
// Backup
backup := newEnvStore()
backup.Record(tc.key)
s.Require().NoError(os.Setenv(tc.key, "other value"))
s.NoError(envStore.setEnv(tc.key, tc.value))
s.Equal(tc.expectedEnvStore, envStore)
v, exists := os.LookupEnv(tc.key)
s.Equal(tc.expectedEnvValue, v)
s.Equal(tc.expectedEnvExists, exists)
// Restore
s.Require().NoError(backup.Restore())
})
}
}
func TestEnvStoreTestSuite(t *testing.T) {
suite.Run(t, new(EnvStoreTestSuite))
}
func TestSetEnvVariables(t *testing.T) {
envs := map[string]string{
"name1": "value1",
"name2": "value2",
}
// Backup
backup := newEnvStore()
for k := range envs {
backup.Record(k)
}
defer func() {
require.NoError(t, backup.Restore())
}()
store, err := SetEnvVariables(envs)
assert.NoError(t, err)
require.IsType(t, &envStore{}, store)
concreteStore := store.(*envStore)
assert.Len(t, concreteStore.store, 2)
assert.Equal(t, backup, concreteStore)
}