Skip to content

Commit

Permalink
Move env related functionality into own package and apply proper testing
Browse files Browse the repository at this point in the history
The first thing we do is moving the `util/env.go` into a new sub-package
`env/`. The package is now using an internal `impl` to be able to apply
properly mocked unit testing which is not dependent from the machine
they're running on.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Mar 8, 2021
1 parent 3103ed2 commit fecc7fb
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 66 deletions.
18 changes: 9 additions & 9 deletions util/env.go → env/env.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
}
112 changes: 112 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.

112 changes: 112 additions & 0 deletions env/internal/internalfakes/fake_impl_iface.go

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

Loading

0 comments on commit fecc7fb

Please sign in to comment.