Files
magpie/internal/edit/crlf_test.go
T
Charan Rathore 0c38071c63 edit: keep a CRLF file's line endings when dotenv, TOML and top-level YAML lines are added or replaced (#829)
SetEnvFile, the TOML setters and the top-level YAML setters build the lines they add with \n, then join the whole file. In a file saved with \r\n (the usual case for a config edited on Windows) that left the new or replaced lines with a bare \n among \r\n ones. SetEnvFile turned "A=1\r\nB=2\r\n" with A set to 9 into "A=9\nB=2\r\n", and SetTOMLTop, a new TOML key or table, and SetYAMLTop did the same.

The files are written back through joinLinesLike, which turns every line break into \r\n when the original used \r\n for all of them. Untouched lines already carry their \r, so only the lines the edit added change; a file that mixed endings is written as before.

TestSetEnvFileKeepsCRLF, TestSetTOMLKeepsCRLF and TestSetYAMLTopKeepsCRLF fail on the old code; TestSetEnvFileKeepsMixedEndings pins that a mixed file is left alone. go vet and go test ./internal/edit pass on this branch and on current main with it merged; the full go test -tags nogui ./... run (under a temp HOME) passes except internal/sessions TestCodexArchivedSameSizeRewriteInvalidatesDiscovery, which fails the same way on unmodified main here.
2026-10-05 04:29:46 +08:00

77 lines
2.3 KiB
Go

package edit
import "testing"
// A file that uses \r\n for every line break keeps it when a line is added,
// replaced or removed: Windows editors save dotenv, TOML and YAML configs that
// way, and a lone \n in the middle of one shows up as a changed line in diffs
// and trips tools that read the file line by line.
func TestSetEnvFileKeepsCRLF(t *testing.T) {
p := tmpFile(t, ".env", "A=1\r\nB=2\r\n")
if err := SetEnvFile(p, KV{Path: "A", Value: "9"}, KV{Path: "C", Value: "3"}); err != nil {
t.Fatal(err)
}
if got, want := read(t, p), "A=9\r\nB=2\r\nC=3\r\n"; got != want {
t.Fatalf("got %q, want %q", got, want)
}
if err := DelEnvFile(p, "B"); err != nil {
t.Fatal(err)
}
if got, want := read(t, p), "A=9\r\nC=3\r\n"; got != want {
t.Fatalf("after delete got %q, want %q", got, want)
}
}
func TestSetEnvFileKeepsMixedEndings(t *testing.T) {
p := tmpFile(t, ".env", "A=1\r\nB=2\n")
if err := SetEnvFile(p, KV{Path: "C", Value: "3"}); err != nil {
t.Fatal(err)
}
if got, want := read(t, p), "A=1\r\nB=2\nC=3\n"; got != want {
t.Fatalf("got %q, want %q", got, want)
}
}
func TestSetTOMLKeepsCRLF(t *testing.T) {
p := tmpFile(t, "config.toml", "top = 1\r\n[a]\r\nk = \"v\"\r\n[b]\r\nz = 2\r\n")
if err := SetTOMLTop(p, KV{Path: "top", Value: 9}, KV{Path: "t2", Value: "x"}); err != nil {
t.Fatal(err)
}
if err := SetTOMLKey(p, "a", "n", "added"); err != nil {
t.Fatal(err)
}
if err := SetTOMLTable(p, "c", KV{Path: "q", Value: "1"}); err != nil {
t.Fatal(err)
}
if err := DelTOMLKey(p, "b", "z"); err != nil {
t.Fatal(err)
}
want := "top = \"9\"\r\nt2 = \"x\"\r\n[a]\r\nk = \"v\"\r\nn = \"added\"\r\n\r\n[c]\r\nq = \"1\"\r\n"
if got := read(t, p); got != want {
t.Fatalf("got %q, want %q", got, want)
}
}
func TestSetYAMLTopKeepsCRLF(t *testing.T) {
p := tmpFile(t, "config.yaml", "A: 1\r\nB: 2\r\n")
if err := SetYAMLTop(p, KV{Path: "A", Value: "9"}, KV{Path: "C", Value: "3"}); err != nil {
t.Fatal(err)
}
got := read(t, p)
for i := 0; i < len(got); i++ {
if got[i] == '\n' && (i == 0 || got[i-1] != '\r') {
t.Fatalf("bare \\n at %d in %q", i, got)
}
}
if err := DelYAMLTop(p, "B"); err != nil {
t.Fatal(err)
}
got = read(t, p)
for i := 0; i < len(got); i++ {
if got[i] == '\n' && (i == 0 || got[i-1] != '\r') {
t.Fatalf("after delete: bare \\n at %d in %q", i, got)
}
}
}