refactor(10.2-01): nest framework packages under modules
- Move remaining beach packages and embedded admin assets\n- Rewrite framework, example, build, and gate paths
This commit is contained in:
120
modules/compass/env_test.go
Normal file
120
modules/compass/env_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package compass
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnvDoubleUnderscoreMapsToDotPath(t *testing.T) {
|
||||
dir := writeConfigTree(t, map[string]string{
|
||||
"database.yaml": "host: localhost\n",
|
||||
"app.yaml": "name: base\n",
|
||||
})
|
||||
cfg, err := Open(Options{
|
||||
Dir: dir,
|
||||
Environ: []string{
|
||||
"SUMMER_DATABASE__HOST=from-env",
|
||||
"SUMMER_GOLEM15__HELLO__POSTS_PER_PAGE=25",
|
||||
"IGNORED_VALUE=nope",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
if got := cfg.String("database.host"); got != "from-env" {
|
||||
t.Fatalf("database.host = %q, want from-env", got)
|
||||
}
|
||||
if got := cfg.Int("golem15.hello.posts_per_page"); got != 25 {
|
||||
t.Fatalf("golem15.hello.posts_per_page = %d, want 25", got)
|
||||
}
|
||||
if cfg.Has("ignored_value") || cfg.Has("IGNORED_VALUE") {
|
||||
t.Fatal("non-SUMMER_ variables must not enter config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingleUnderscoreRemainsLiteralLeaf(t *testing.T) {
|
||||
dir := writeConfigTree(t, map[string]string{
|
||||
"app.yaml": "name: base\n",
|
||||
})
|
||||
cfg, err := Open(Options{
|
||||
Dir: dir,
|
||||
Environ: []string{
|
||||
"SUMMER_GOLEM15__HELLO__POSTS_PER_PAGE=9",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
if !cfg.Has("golem15.hello.posts_per_page") {
|
||||
t.Fatal("expected golem15.hello.posts_per_page")
|
||||
}
|
||||
if cfg.Has("golem15.hello.posts.per.page") {
|
||||
t.Fatal("single underscores must not become path separators")
|
||||
}
|
||||
if got := cfg.Int("golem15.hello.posts_per_page"); got != 9 {
|
||||
t.Fatalf("posts_per_page = %d, want 9", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDotEnvFillsOnlyAbsentRealEnvKeysAndDoesNotMutateProcess(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
dir := filepath.Join(root, "config")
|
||||
if err := os.Mkdir(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("name: base\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dotenv := "SUMMER_APP__NAME=from-dotenv\nSUMMER_DATABASE__HOST=dotenv-host\n"
|
||||
if err := os.WriteFile(filepath.Join(root, ".env"), []byte(dotenv), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const sentinel = "SUMMER_COMPASS_DOTENV_SENTINEL"
|
||||
if _, ok := os.LookupEnv(sentinel); ok {
|
||||
t.Fatalf("%s already set in process env", sentinel)
|
||||
}
|
||||
|
||||
cfg, err := Open(Options{
|
||||
Dir: dir,
|
||||
Environ: []string{
|
||||
"SUMMER_APP__NAME=from-real",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
if got := cfg.String("app.name"); got != "from-real" {
|
||||
t.Fatalf("app.name = %q, want from-real (real env wins over .env)", got)
|
||||
}
|
||||
if got := cfg.String("database.host"); got != "dotenv-host" {
|
||||
t.Fatalf("database.host = %q, want dotenv-host (.env fills absent keys)", got)
|
||||
}
|
||||
if _, ok := os.LookupEnv("SUMMER_DATABASE__HOST"); ok {
|
||||
t.Fatal("Open must not mutate process environment with .env values")
|
||||
}
|
||||
if _, ok := os.LookupEnv(sentinel); ok {
|
||||
t.Fatal("Open must not mutate process environment")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSUMMER_ENVFromProcessSelectsOverlay(t *testing.T) {
|
||||
dir := writeConfigTree(t, map[string]string{
|
||||
"app.yaml": "name: base\n",
|
||||
"env/development/app.yaml": "name: hello-dev\n",
|
||||
})
|
||||
cfg, err := Open(Options{
|
||||
Dir: dir,
|
||||
Environ: []string{"SUMMER_ENV=development"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
if cfg.Environment() != "development" {
|
||||
t.Fatalf("Environment = %q, want development", cfg.Environment())
|
||||
}
|
||||
if got := cfg.String("app.name"); got != "hello-dev" {
|
||||
t.Fatalf("name = %q, want hello-dev", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user