Skip to content

Commit

Permalink
Merge pull request #6 from saschagrunert/env-test
Browse files Browse the repository at this point in the history
Move env related functionality into own package and apply proper testing
  • Loading branch information
k8s-ci-robot committed Mar 9, 2021
2 parents 3103ed2 + 857c10e commit f013614
Show file tree
Hide file tree
Showing 9 changed files with 482 additions and 66 deletions.
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.

0 comments on commit f013614

Please sign in to comment.