Files
magpie/internal/edit/env_duplicate_test.go
Charan Rathore 60b301a9cd edit: a dotenv key set twice reads and writes its last line, as dotenv does (#832)
GetEnvFile returned the first line for a key and SetEnvFile replaced the first one. Gemini CLI reads ~/.gemini/.env with dotenv.parse, where a later line overrides an earlier one, so in a file with GEMINI_API_KEY twice the Gemini CLI uses the last, while magpie read and wrote the first. Wiring Gemini CLI to the gateway then changed a line the CLI ignored, and the check for whether it was wired read a value the CLI did not use.

GetEnvFile now returns the last line for the key and setLine replaces the last matching line (setLine is only used by SetEnvFile). A key that is set once behaves as before.

TestGetEnvFileLastDuplicateWins and TestSetEnvFileReplacesLastDuplicate fail on the old code. go vet and go test ./internal/edit ./internal/agent pass.
2026-10-05 07:02:42 +08:00

28 lines
836 B
Go

package edit
import "testing"
// A dotenv key that appears twice takes its last value (Gemini CLI reads the
// file with dotenv, where a later line overrides an earlier one), so reads
// return the last line and a write replaces it, rather than the first.
func TestGetEnvFileLastDuplicateWins(t *testing.T) {
p := tmpFile(t, ".env", "A=1\nB=x\nA=2\n")
if v, ok := GetEnvFile(p, "A"); !ok || v != "2" {
t.Fatalf("A = %q, %v; want 2", v, ok)
}
}
func TestSetEnvFileReplacesLastDuplicate(t *testing.T) {
p := tmpFile(t, ".env", "A=1\nB=x\nA=2\n")
if err := SetEnvFile(p, KV{Path: "A", Value: "3"}); err != nil {
t.Fatal(err)
}
if got, want := read(t, p), "A=1\nB=x\nA=3\n"; got != want {
t.Fatalf("got %q, want %q", got, want)
}
if v, _ := GetEnvFile(p, "A"); v != "3" {
t.Fatalf("A reads %q after setting 3", v)
}
}