buddy fix

This commit is contained in:
2026-09-25 11:55:50 +05:30
parent 276e12beb9
commit d562691f42
5 changed files with 134 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package config
import (
"os"
"strings"
"testing"
)
@@ -206,3 +207,55 @@ func TestEachDefaultIsStillOverridable(t *testing.T) {
t.Fatalf("a local model was refused: %s", cfg.Why())
}
}
// The container reads its own configuration from a file beside the binary.
//
// The Dockerfile copies `.env.production` into the runtime image and sets
// APP_ENV=production, so `loadEnvFiles` reads it on boot. This asserts the
// mechanism rather than the Dockerfile — a COPY line is easy to check by eye
// and easy to believe wrongly, and the failure it produces is a server that
// starts fine with a variable silently unset.
func TestTheEnvironmentFileBesideTheBinaryIsRead(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
if err := os.WriteFile(".env.production",
[]byte("ASSISTANT_API_KEY=from-the-file\n"), 0o600); err != nil {
t.Fatalf("writing the fixture: %v", err)
}
t.Setenv("APP_ENV", "production")
// Registered with t.Setenv first so it is restored on return, then removed:
// godotenv does not overwrite a variable that is PRESENT, and an empty
// string is present. Setting it to "" would have tested nothing.
t.Setenv("ASSISTANT_API_KEY", "placeholder")
os.Unsetenv("ASSISTANT_API_KEY")
loadEnvFiles()
if os.Getenv("ASSISTANT_API_KEY") != "from-the-file" {
t.Fatal("the environment file beside the binary was not read")
}
}
func TestThePlatformStillWinsOverTheFile(t *testing.T) {
// godotenv never overwrites a variable already in the environment, so a
// value set on the hosting platform overrides the committed file without
// the file having to change. Both mechanisms work; neither fights the other.
dir := t.TempDir()
t.Chdir(dir)
if err := os.WriteFile(".env.production",
[]byte("ASSISTANT_API_KEY=from-the-file\n"), 0o600); err != nil {
t.Fatalf("writing the fixture: %v", err)
}
t.Setenv("APP_ENV", "production")
t.Setenv("ASSISTANT_API_KEY", "from-the-platform")
loadEnvFiles()
if got := os.Getenv("ASSISTANT_API_KEY"); got != "from-the-platform" {
t.Fatalf("the file overrode the platform: ASSISTANT_API_KEY=%q", got)
}
}