-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
safety guard to avoid typos with testcase environment variables
- Loading branch information
Showing
13 changed files
with
142 additions
and
29 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
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
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,58 @@ | ||
package environ | ||
|
||
import ( | ||
"os" | ||
"slices" | ||
"strings" | ||
|
||
"go.llib.dev/testcase/internal" | ||
) | ||
|
||
// KeySeed is the environment variable key that will be checked for a pseudo random seed, | ||
// which will be used to randomize the order of executions between test cases. | ||
const KeySeed = `TESTCASE_SEED` | ||
|
||
// KeyOrdering is the environment variable key that will be checked for testCase determine | ||
// what order of execution should be used between test cases in a testing group. | ||
// The default sorting behavior is pseudo random based on an the seed. | ||
// | ||
// Mods: | ||
// - defined: execute testCase in the order which they are being defined | ||
// - random: pseudo random based ordering between tests. | ||
const KeyOrdering = `TESTCASE_ORDERING` | ||
const KeyOrdering2 = `TESTCASE_ORDER` | ||
|
||
const KeyDebug = "TESTCASE_DEBUG" | ||
|
||
var acceptedKeys = []string{ | ||
KeySeed, | ||
KeyOrdering, | ||
KeyOrdering2, | ||
KeyDebug, | ||
} | ||
|
||
func init() { CheckEnvKeys() } | ||
|
||
func CheckEnvKeys() { | ||
var got bool | ||
for _, envPair := range os.Environ() { | ||
ekv := strings.SplitN(envPair, "=", 2) // best effort to split, but it might not be platform agnostic | ||
if len(ekv) != 2 { | ||
continue | ||
} | ||
key, _ := ekv[0], ekv[1] | ||
|
||
if !strings.HasPrefix(key, "TESTCASE_") { | ||
continue | ||
} | ||
|
||
if !slices.Contains(acceptedKeys, key) { | ||
got = true | ||
internal.Warn("unrecognised testcase variable:", key) | ||
} | ||
} | ||
if got { | ||
internal.Warn("check if you might have a typo.") | ||
internal.Warn("accepted environment variables:", strings.Join(acceptedKeys, ", ")) | ||
} | ||
} |
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,32 @@ | ||
package environ_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"go.llib.dev/testcase/assert" | ||
"go.llib.dev/testcase/internal" | ||
"go.llib.dev/testcase/internal/env" | ||
"go.llib.dev/testcase/internal/environ" | ||
"go.llib.dev/testcase/random" | ||
) | ||
|
||
func Test_checkEnvKeys(t *testing.T) { | ||
t.Run("when invalid testcase env variable is present in the env", func(t *testing.T) { | ||
out := internal.StubWarn(t) | ||
rnd := random.New(random.CryptoSeed{}) | ||
key := fmt.Sprintf("TESTCASE_%s", rnd.StringNC(rnd.IntB(0, 10), random.CharsetASCII())) | ||
env.SetEnv(t, key, rnd.StringNC(5, random.CharsetASCII())) | ||
environ.CheckEnvKeys() | ||
assert.NotEmpty(t, out.String()) | ||
assert.Contain(t, out.String(), key) | ||
assert.Contain(t, out.String(), "typo") | ||
}) | ||
t.Run("when only valid env variables are present in the env", func(t *testing.T) { | ||
out := internal.StubWarn(t) | ||
env.SetEnv(t, environ.KeySeed, "123") | ||
env.SetEnv(t, environ.KeyOrdering, "defined") | ||
environ.CheckEnvKeys() | ||
assert.Empty(t, out.String()) | ||
}) | ||
} |
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,24 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var warnOutput io.Writer = os.Stderr | ||
|
||
func Warn(vs ...any) { | ||
out := append([]any{"[WARN]", "[TESTCASE]"}, vs...) | ||
fmt.Fprintln(warnOutput, out...) | ||
} | ||
|
||
func StubWarn(tb testing.TB) *bytes.Buffer { | ||
original := warnOutput | ||
tb.Cleanup(func() { warnOutput = original }) | ||
var buf bytes.Buffer | ||
warnOutput = &buf | ||
return &buf | ||
} |
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
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