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,
|
|
|
|
},
|
|
|
|
{
|
2023-01-01 00:07:54 +00:00
|
|
|
Name: env.Silent,
|
2022-12-31 05:53:24 +00:00
|
|
|
Func: SilenceEnabled,
|
2023-01-01 00:07:54 +00:00
|
|
|
Expects: true,
|
2022-12-31 05:53:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 06:54:49 +00:00
|
|
|
func TestSilent(t *testing.T) {
|
|
|
|
origArgs := os.Args
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Args = origArgs
|
|
|
|
})
|
|
|
|
t.Run("SILENT = silence", func(t *testing.T) {
|
|
|
|
t.Setenv(env.Silent, "1")
|
|
|
|
t.Setenv(env.Verbose, "")
|
|
|
|
os.Args = []string{}
|
|
|
|
if !SilenceEnabled() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SILENT + VERBOSE = silence", func(t *testing.T) {
|
|
|
|
t.Setenv(env.Silent, "1")
|
|
|
|
t.Setenv(env.Verbose, "1")
|
|
|
|
os.Args = []string{}
|
|
|
|
if SilenceEnabled() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("VERBOSE + --silent = silent", func(t *testing.T) {
|
|
|
|
t.Setenv(env.Silent, "")
|
|
|
|
t.Setenv(env.Verbose, "1")
|
|
|
|
os.Args = []string{"some", "random", "--silent", "args"}
|
|
|
|
if !SilenceEnabled() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("--silent = silent", func(t *testing.T) {
|
|
|
|
t.Setenv(env.Silent, "")
|
|
|
|
t.Setenv(env.Verbose, "")
|
|
|
|
os.Args = []string{"some", "random", "--silent", "args"}
|
|
|
|
if !SilenceEnabled() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nothing = nothing", func(t *testing.T) {
|
|
|
|
t.Setenv(env.Silent, "")
|
|
|
|
t.Setenv(env.Verbose, "")
|
|
|
|
os.Args = []string{"some", "random", "args"}
|
|
|
|
if SilenceEnabled() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:04:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|