Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move env related functionality into own package and apply proper testing #6

Merged
merged 1 commit into from Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions util/env.go → env/env.go
Expand Up @@ -14,24 +14,24 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package util
package env

import (
"os"
"sigs.k8s.io/release-utils/env/internal"
)

// EnvDefault returns either the provided environment variable for the given
// `key` or the default value `def` if not set.
func EnvDefault(key, def string) string {
value, ok := os.LookupEnv(key)
// Default returns either the provided environment variable for the given key
// or the default value def if not set.
func Default(key, def string) string {
value, ok := internal.Impl.LookupEnv(key)
if !ok || value == "" {
return def
}
return value
}

// IsEnvSet returns true if an environment variable is set
func IsEnvSet(key string) bool {
_, ok := os.LookupEnv(key)
// IsSet returns true if an environment variable is set.
func IsSet(key string) bool {
_, ok := internal.Impl.LookupEnv(key)
return ok
}
97 changes: 97 additions & 0 deletions env/env_test.go
@@ -0,0 +1,97 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package env

import (
"testing"

"github.com/stretchr/testify/require"

"sigs.k8s.io/release-utils/env/internal"
"sigs.k8s.io/release-utils/env/internal/internalfakes"
)

func TestDefault(t *testing.T) {
for _, tc := range []struct {
prepare func(*internalfakes.FakeImpl)
defaultValue string
expected string
}{
{ // default LookupEnvReturns empty string and false
prepare: func(mock *internalfakes.FakeImpl) {
mock.LookupEnvReturns("", false)
},
defaultValue: "default",
expected: "default",
},
{ // default LookupEnvReturns empty string and true
prepare: func(mock *internalfakes.FakeImpl) {
mock.LookupEnvReturns("", true)
},
defaultValue: "default",
expected: "default",
},
{ // default LookupEnvReturns string and false
prepare: func(mock *internalfakes.FakeImpl) {
mock.LookupEnvReturns("value", false)
},
defaultValue: "default",
expected: "default",
},
{ // value is set
prepare: func(mock *internalfakes.FakeImpl) {
mock.LookupEnvReturns("value", true)
},
defaultValue: "default",
expected: "value",
},
} {
mock := &internalfakes.FakeImpl{}
tc.prepare(mock)
internal.Impl = mock

res := Default("key", tc.defaultValue)
require.Equal(t, tc.expected, res)
}
}

func TestIsSet(t *testing.T) {
for _, tc := range []struct {
prepare func(*internalfakes.FakeImpl)
expected bool
}{
{ // LookupEnvReturns false
prepare: func(mock *internalfakes.FakeImpl) {
mock.LookupEnvReturns("", false)
},
expected: false,
},
{ // LookupEnvReturns true
prepare: func(mock *internalfakes.FakeImpl) {
mock.LookupEnvReturns("", true)
},
expected: true,
},
} {
mock := &internalfakes.FakeImpl{}
tc.prepare(mock)
internal.Impl = mock

res := IsSet("key")
require.Equal(t, tc.expected, res)
}
}
34 changes: 34 additions & 0 deletions env/internal/impl.go
@@ -0,0 +1,34 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internal

import "os"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

//counterfeiter:generate . impl
type impl interface {
LookupEnv(key string) (string, bool)
}

type defImpl struct{}

var Impl impl = &defImpl{}

func (defImpl) LookupEnv(key string) (string, bool) {
return os.LookupEnv(key)
}
128 changes: 128 additions & 0 deletions env/internal/internalfakes/fake_impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.