chinampa/pkg/runtime/runtime_test.go

319 lines
6.5 KiB
Go
Raw Permalink Normal View History

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"
"strings"
2022-12-19 03:04:34 +00:00
"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 withEnv(t *testing.T, env map[string]string) {
prevEnv := os.Environ()
for _, entry := range prevEnv {
parts := strings.SplitN(entry, "=", 2)
os.Unsetenv(parts[0])
}
for k, v := range env {
os.Setenv(k, v)
}
t.Cleanup(func() {
ResetParsedFlagsCache()
2022-12-19 03:04:34 +00:00
for k := range env {
os.Unsetenv(k)
}
for _, entry := range prevEnv {
parts := strings.SplitN(entry, "=", 2)
os.Setenv(parts[0], parts[1])
}
})
}
func TestCombinations(t *testing.T) {
args := append([]string{}, os.Args...)
t.Cleanup(func() { os.Args = args })
cases := []struct {
Env map[string]string
Args []string
Func func() bool
Expects bool
}{
{
Env: map[string]string{},
Args: []string{},
Func: VerboseEnabled,
Expects: false,
},
{
Env: map[string]string{env.Verbose: "1"},
Args: []string{"--silent"},
Func: VerboseEnabled,
Expects: false,
},
{
Env: map[string]string{env.Verbose: "1"},
Args: []string{},
Func: VerboseEnabled,
Expects: true,
},
{
Env: map[string]string{env.Silent: "1"},
Args: []string{},
Func: VerboseEnabled,
Expects: false,
},
{
Env: map[string]string{},
Args: []string{},
Func: SilenceEnabled,
Expects: false,
},
{
Env: map[string]string{env.Silent: "1"},
Args: []string{},
Func: SilenceEnabled,
Expects: true,
},
{
Env: map[string]string{env.Silent: "1"},
Args: []string{"--verbose"},
Func: SilenceEnabled,
Expects: false,
},
{
Env: map[string]string{env.Verbose: "1"},
Args: []string{"--silent"},
Func: SilenceEnabled,
Expects: true,
},
{
Env: map[string]string{env.ForceColor: "1"},
Args: []string{"--no-color"},
Func: ColorEnabled,
Expects: false,
},
{
Env: map[string]string{},
Args: []string{},
Func: ColorEnabled,
Expects: true,
},
{
Env: map[string]string{env.ForceColor: "1"},
Args: []string{},
Func: ColorEnabled,
Expects: true,
},
{
Env: map[string]string{env.ForceColor: "1"},
Args: []string{"--no-color"},
Func: ColorEnabled,
Expects: false,
},
{
Env: map[string]string{env.NoColor: "1"},
Args: []string{},
Func: ColorEnabled,
Expects: false,
},
{
Env: map[string]string{env.NoColor: "1"},
Args: []string{"--color"},
Func: ColorEnabled,
Expects: true,
},
}
for _, c := range cases {
fname := runtime.FuncForPC(reflect.ValueOf(c.Func).Pointer()).Name()
name := fmt.Sprintf("%v/%v/%s", fname, c.Env, c.Args)
t.Run(name, func(t *testing.T) {
withEnv(t, c.Env)
os.Args = c.Args
if res := c.Func(); res != c.Expects {
t.Fatalf("%s got %v wanted: %v", name, res, c.Expects)
}
})
}
}
func TestEnabled(t *testing.T) {
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-19 03:04:34 +00:00
{
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.Debug,
2022-12-19 03:04:34 +00:00
Func: DebugEnabled,
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) {
withEnv(t, map[string]string{c.Name: val})
2022-12-19 03:04:34 +00:00
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) {
withEnv(t, map[string]string{c.Name: val})
2022-12-19 03:04:34 +00:00
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) {
args := append([]string{}, os.Args...)
t.Cleanup(func() { os.Args = args })
2023-01-02 06:54:49 +00:00
t.Run("SILENT = silence", func(t *testing.T) {
withEnv(t, map[string]string{
env.Silent: "1",
env.Verbose: "",
})
2023-01-02 06:54:49 +00:00
os.Args = []string{}
if !SilenceEnabled() {
t.Fail()
}
})
t.Run("SILENT+VERBOSE=silence", func(t *testing.T) {
withEnv(t, map[string]string{
env.Silent: "1",
env.Verbose: "1",
})
2023-01-02 06:54:49 +00:00
os.Args = []string{}
if !SilenceEnabled() {
2023-01-02 06:54:49 +00:00
t.Fail()
}
})
t.Run("VERBOSE+--silent=silent", func(t *testing.T) {
withEnv(t, map[string]string{
env.Silent: "0",
env.Verbose: "1",
})
2023-01-02 06:54:49 +00:00
os.Args = []string{"some", "random", "--silent", "args"}
if !SilenceEnabled() {
t.Fail()
}
})
t.Run("--silent=silent", func(t *testing.T) {
withEnv(t, map[string]string{
env.Silent: "",
env.Verbose: "",
})
2023-01-02 06:54:49 +00:00
os.Args = []string{"some", "random", "--silent", "args"}
if !SilenceEnabled() {
t.Fail()
}
})
t.Run("nothing = nothing", func(t *testing.T) {
withEnv(t, map[string]string{
env.Silent: "",
env.Verbose: "",
})
2023-01-02 06:54:49 +00:00
os.Args = []string{"some", "random", "args"}
if SilenceEnabled() {
t.Fail()
}
if VerboseEnabled() {
t.Fail()
}
2023-01-02 06:54:49 +00:00
})
}
2022-12-19 03:04:34 +00:00
func TestEnvironmentMapEnabled(t *testing.T) {
trueString := strconv.FormatBool(true)
withEnv(t, map[string]string{
env.ForceColor: trueString,
env.Debug: trueString,
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)
}
}