mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
ac5cf17317
* rename interface, types and methods. some type changes and refactor * update dependent methods and variables * fix unit tests * a bit more refactor and fix * concurrent safe singleton * return old Options struct * refactor creating config provider and fix nil pointer derefernce * fix unit test and linter errors * introduce resetting config provider (for unit tests) * fix annoying error message when config provider is not configured --------- Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com> Co-authored-by: Muhammadali Nazarov <muhammadalinazarov@gmail.com>
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
//go:build unit
|
|
// +build unit
|
|
|
|
package orchestrator
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func Test_envVarsAreSet(t *testing.T) {
|
|
t.Run("Test envVarsAreSet", func(t *testing.T) {
|
|
defer resetEnv(os.Environ())
|
|
os.Clearenv()
|
|
|
|
envVars := []string{"GITHUB_ACTION", "GITHUB_ACTIONS"}
|
|
|
|
os.Setenv("GITHUB_ACTION", "true")
|
|
tmp := envVarsAreSet(envVars)
|
|
assert.True(t, tmp)
|
|
|
|
os.Unsetenv("GITHUB_ACTION")
|
|
os.Setenv("GITHUB_ACTIONS", "true")
|
|
tmp = envVarsAreSet(envVars)
|
|
assert.True(t, tmp)
|
|
|
|
os.Setenv("GITHUB_ACTION", "1")
|
|
os.Setenv("GITHUB_ACTIONS", "false")
|
|
tmp = envVarsAreSet(envVars)
|
|
assert.True(t, tmp)
|
|
|
|
os.Setenv("GITHUB_ACTION", "false")
|
|
os.Setenv("GITHUB_ACTIONS", "0")
|
|
tmp = envVarsAreSet(envVars)
|
|
assert.False(t, tmp)
|
|
})
|
|
}
|
|
|
|
func Test_getEnv(t *testing.T) {
|
|
type args struct {
|
|
key string
|
|
fallback string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
envVar string
|
|
}{
|
|
{
|
|
name: "environment variable found",
|
|
args: args{key: "debug", fallback: "fallback"},
|
|
want: "found",
|
|
envVar: "debug",
|
|
},
|
|
{
|
|
name: "fallback variable",
|
|
args: args{key: "debug", fallback: "fallback"},
|
|
want: "fallback",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
defer resetEnv(os.Environ())
|
|
os.Clearenv()
|
|
os.Setenv(tt.envVar, "found")
|
|
assert.Equalf(t, tt.want, getEnv(tt.args.key, tt.args.fallback), "getEnv(%v, %v)", tt.args.key, tt.args.fallback)
|
|
})
|
|
}
|
|
}
|