2022-12-19 03:04:34 +00:00
|
|
|
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
|
2022-12-31 05:53:24 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-12-19 03:04:34 +00:00
|
|
|
package runtime_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-31 05:53:24 +00:00
|
|
|
"git.rob.mx/nidito/chinampa/pkg/env"
|
2022-12-19 03:04:34 +00:00
|
|
|
. "git.rob.mx/nidito/chinampa/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnabled(t *testing.T) {
|
2022-12-31 05:53:24 +00:00
|
|
|
defer func() { os.Setenv(env.Verbose, "") }()
|
2022-12-19 03:04:34 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Func func() bool
|
|
|
|
Expects bool
|
|
|
|
}{
|
|
|
|
{
|
2022-12-31 05:53:24 +00:00
|
|
|
Name: env.Verbose,
|
2022-12-19 03:04:34 +00:00
|
|
|
Func: VerboseEnabled,
|
|
|
|
Expects: true,
|
|
|
|
},
|
|
|
|
{
|
2022-12-31 05:53:24 +00:00
|
|
|
Name: env.Verbose,
|
|
|
|
Func: SilenceEnabled,
|
|
|
|
Expects: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: env.ValidationDisabled,
|
2022-12-19 03:04:34 +00:00
|
|
|
Func: ValidationEnabled,
|
|
|
|
},
|
|
|
|
{
|
2022-12-31 05:53:24 +00:00
|
|
|
Name: env.NoColor,
|
2022-12-19 03:04:34 +00:00
|
|
|
Func: ColorEnabled,
|
|
|
|
},
|
|
|
|
{
|
2022-12-31 05:53:24 +00:00
|
|
|
Name: env.HelpUnstyled,
|
2022-12-19 03:04:34 +00:00
|
|
|
Func: ColorEnabled,
|
|
|
|
},
|
|
|
|
{
|
2022-12-31 05:53:24 +00:00
|
|
|
Name: env.Debug,
|
2022-12-19 03:04:34 +00:00
|
|
|
Func: DebugEnabled,
|
|
|
|
Expects: true,
|
|
|
|
},
|
|
|
|
{
|
2022-12-31 05:53:24 +00:00
|
|
|
Name: env.HelpUnstyled,
|
2022-12-19 03:04:34 +00:00
|
|
|
Func: UnstyledHelpEnabled,
|
|
|
|
Expects: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
fname := runtime.FuncForPC(reflect.ValueOf(c.Func).Pointer()).Name()
|
|
|
|
name := fmt.Sprintf("%v/%s", fname, c.Name)
|
|
|
|
enabled := []string{
|
|
|
|
"yes", "true", "1", "enabled",
|
|
|
|
}
|
|
|
|
for _, val := range enabled {
|
|
|
|
t.Run("enabled-"+val, func(t *testing.T) {
|
|
|
|
os.Setenv(c.Name, val)
|
|
|
|
if c.Func() != c.Expects {
|
|
|
|
t.Fatalf("%s wasn't enabled with a valid value: %s", name, val)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
disabled := []string{"", "no", "false", "0", "disabled"}
|
|
|
|
for _, val := range disabled {
|
|
|
|
t.Run("disabled-"+val, func(t *testing.T) {
|
|
|
|
os.Setenv(c.Name, val)
|
|
|
|
if c.Func() == c.Expects {
|
|
|
|
t.Fatalf("%s was enabled with falsy value: %s", name, val)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentMapEnabled(t *testing.T) {
|
|
|
|
trueString := strconv.FormatBool(true)
|
2022-12-31 05:53:24 +00:00
|
|
|
os.Setenv(env.ForceColor, trueString)
|
|
|
|
os.Setenv(env.Debug, trueString)
|
|
|
|
os.Setenv(env.Verbose, trueString)
|
2022-12-19 03:04:34 +00:00
|
|
|
|
|
|
|
res := EnvironmentMap()
|
|
|
|
if res == nil {
|
|
|
|
t.Fatalf("Expected map, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]string{
|
2022-12-31 05:53:24 +00:00
|
|
|
env.ForceColor: "always",
|
|
|
|
env.Debug: trueString,
|
|
|
|
env.Verbose: trueString,
|
2022-12-19 03:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(res, expected) {
|
|
|
|
t.Fatalf("Unexpected result from enabled environment. Wanted %v, got %v", res, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentMapDisabled(t *testing.T) {
|
|
|
|
trueString := strconv.FormatBool(true)
|
|
|
|
// clear COLOR
|
2022-12-31 05:53:24 +00:00
|
|
|
os.Unsetenv(env.ForceColor)
|
2022-12-19 03:04:34 +00:00
|
|
|
// set NO_COLOR
|
2022-12-31 05:53:24 +00:00
|
|
|
os.Setenv(env.NoColor, trueString)
|
|
|
|
os.Unsetenv(env.Debug)
|
|
|
|
os.Unsetenv(env.Verbose)
|
|
|
|
os.Setenv(env.Silent, trueString)
|
2022-12-19 03:04:34 +00:00
|
|
|
|
|
|
|
res := EnvironmentMap()
|
|
|
|
if res == nil {
|
|
|
|
t.Fatalf("Expected map, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]string{
|
2022-12-31 05:53:24 +00:00
|
|
|
env.NoColor: trueString,
|
|
|
|
env.Silent: trueString,
|
2022-12-19 03:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(res, expected) {
|
|
|
|
t.Fatalf("Unexpected result from disabled environment. Wanted %v, got %v", res, expected)
|
|
|
|
}
|
|
|
|
}
|