-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move env related functionality into own package and apply proper testing
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
1 parent
3103ed2
commit fecc7fb
Showing
9 changed files
with
450 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.