From 395e7beec0ef61ff714618c794ee16bebacee62b Mon Sep 17 00:00:00 2001 From: "gascity-eddie[bot]" <338826946+gascity-eddie[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 08:49:00 +0000 Subject: [PATCH] fix(config): stop `bd config set` dropping the file's trailing newline (#7669) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(config): keep config.yaml's trailing newline on bd config set updateYamlKey's flat-key path, which `bd config set` takes for every single-segment key, read the document with bufio.Scanner and joined the lines back with "\n". ScanLines drops the final terminator, so the result was exactly one newline short of any terminated config.yaml: "issue_prefix: vp\ndolt.mode: server\n" came back without its final newline, a spurious no-newline-at-end-of-file change in a git-tracked file. Re-attach the input's own trailing run at the return, the tail commentOutYamlKey got for the unset side in #6749. One statement covers both the in-place and the appended branch, and a document without a trailing newline still does not acquire one. The TODO that recorded the defect is removed in the next commit. The test file's header now covers both verbs; the set test follows the unset ones. AC: AC-1 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * chore(config): drop the TODO that deferred the bd config set newline fix The TODO above updateYamlKey's return recorded that `bd config set` was knowingly left one newline short, as a scope call in #6749. The previous commit fixed that, so the TODO now describes a defect that no longer exists. Check: searching internal/config/yaml_config.go for the phrase "`bd config set` is knowingly left unfixed" finds one match at the base and none after this commit. AC: AC-11 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin a set's blank-line trailing run, not just a final newline A config.yaml that ends in one or two blank lines came back from `bd config set` one newline short, so the output still ended in "\n". Compare the exact trailing run, as the unset table does, so that loss stays visible. Both rows fail on the base code (run "\n" vs "\n\n", and "\n\n" vs "\n\n\n") and pass with the tail fix. AC: AC-2 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin the appended key's terminator on bd config set When the key is absent, updateYamlKey appends it as the new last line, and the old join left that line unterminated: "x\n" came back "x\n\nnewkey: \"v\"". Pin the exact output for "x\n", where the separator blank line is kept and the newline follows the new key, plus a table row for the trailing run. Both fail on the base code and pass with the tail fix. AC: AC-3 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin a set appended over a blank-line tail For "x\n\n" without the key, the existing blank line already separates, so updateYamlKey adds no second one and the document's own two-newline run now follows the new key: "x\n\nnewkey: \"v\"\n\n". Restoring a single newline would leave this exact shape one short again, so pin it exactly, plus the matching table row. Both fail on the base code, which returned "x\n\nnewkey: \"v\"", and pass with the tail fix. AC: AC-4 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin that a set does not invent a trailing newline The repair re-attaches the document's own trailing run instead of appending a newline, so a config.yaml that ends without one still ends without one after `bd config set`. Pin that for both branches: a present key rewritten in place, and an absent key appended as the new last line. This passes on the base as well; it is a pin on "preserve, not append". The always-append mutant (`return out + "\n", nil`) turns both inputs red. AC: AC-5 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin a set's output on a CRLF config.yaml A set of a single-segment key has always rewritten a CRLF document to LF, because the scan drops each line's "\r". What the old join added on top was a missing terminator: "actor: a\r\nother: x\r\n" came back "actor: \"b\"\nother: x". With the restored tail it comes back "actor: \"b\"\nother: x\n", which is also how an unset of the same document ends. Pin the exact output, and check the trailing run against commentOutYamlKey on the same input, in a subtest named after unset's CRLF subtest. It fails on the base (exact and parity) and passes with the tail from AC-1. AC: AC-6 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * fix(config): keep a CRLF config.yaml's blank-line tail on bd config set The restored tail measures the trailing run on the raw content, where strings.TrimRight(content, "\n") stops at the last "\r". On a CRLF file the re-attached run was therefore always a bare "\n", however many blank lines followed: "actor: a\r\nother: x\r\n\r\n\r\n" came back "…x\n", one newline shorter than even the base's "…x\n\n". Normalize CRLF the way commentOutYamlKey does before the scan, so the run is the LF run and a set ends the file exactly as an unset of the same document does. The scan output is unchanged, since ScanLines already drops each line's "\r". The line sits below the nested dispatch, not at the top: some updateNestedYamlKey branches carry "\r" through on untouched lines, and this fix leaves that path alone. Also correct the comment on unset's CRLF subtest, which still blamed a leftover "\r" for the bare-"\n" tail; commentOutYamlKey normalizes up front, so that mechanism no longer exists and the new set cases contradict it. AC: AC-7 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin the trailing newline through SetYamlConfig setYamlConfigAtPath writes updateYamlKey's output verbatim, so the dropped newline reached the git-tracked config.yaml. Add the writer-level twin of TestUnsetThroughTheFileKeepsTheTrailingNewline: a .beads/config.yaml ending in a blank line, BEADS_DIR pointed at it, SetYamlConfig("actor", "bob"), and the re-read file must keep its "\n\n" run. It first checks that the value landed, so an untouched file cannot pass. On the base the run comes back "\n"; with the tail from AC-1 it is "\n\n". AC: AC-8 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * test(config): pin that a set of a dotted key bypasses the CRLF normalization The CRLF normalization in updateYamlKey sits below the nested dispatch on purpose. replaceFlatKeyLine and appendNestedYamlKey keep "\r" on the lines they leave alone, so hoisting the line above the dispatch would silently change what a dotted-key set writes: "issue_prefix: vp\r\ndolt.mode: server\r\n" comes back "issue_prefix: vp\r\ndolt.mode: \"embedded\"\n" today and would lose the first "\r". Compare updateYamlKey with updateNestedYamlKey on four CRLF documents for dolt.mode: a literal dotted key, a nested block, an absent key, and a comment-only document. This passes on the base as well; it pins the boundary, not the nested writer's outputs. The hoisted mutant turns the literal-dotted-key and comment-only rows red; the other two are re-marshalled to LF either way. AC: AC-9 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 * fix(config): append an absent key after a blank-line tail as one line updateYamlKey's flat path appends an absent key after the scan's empty tokens for the file's trailing blank lines, so those blank lines already sit ahead of the new line; the tail rule then re-attached content's whole run after it as well. A set of a new single-segment key on a file ending in blank lines therefore repeated them at EOF: "x\n\n" came back "x\n\nnewkey: \"v\"\n\n", "x\n\n\n" came back "x\n\n\nnewkey: \"v\"\n\n\n", and CRLF "x\r\n\r\n" came back "x\n\nnewkey: \"v\"\n\n" -- the diff noise this PR exists to remove. The appended branch now re-attaches only the one terminator bufio.Scanner dropped, and only when content had it; the in-place branch keeps the TrimRight + content's-run rule it shares with commentOutYamlKey. The appended-key subtest now wants "x\n\nnewkey: \"v\"\n", adds "x\n\n\n" and CRLF rows, and drops the comment claiming a single terminator would leave it one newline short. The suffix-run table drops its blank-tail append row, whose expectation assumed the file's run follows the key, and TestSetOfAnAbsentKeyThroughTheFileAddsOneLine pins the shape through SetYamlConfig. The doc comment states the nested boundary and the tail rule. Not changed here: the unchecked scanner.Err() is #7670's fix (its guard lands right after this scan loop, and ./internal/config/ is green on the union of both PRs). The stale bufio.Scanner narration in commentOutYamlKey's comment stays a follow-up, since #7613 edits that function. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Eddie the Engineer Co-authored-by: Claude Opus 5.5 Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com> --- internal/config/yaml_config.go | 45 +++- internal/config/yaml_unset_newline_test.go | 249 +++++++++++++++++++-- 2 files changed, 267 insertions(+), 27 deletions(-) diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index ceb0477e7..046e0f67a 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -862,6 +862,13 @@ func findProjectBeadsDir() string { // updateYamlKey updates a key in yaml content, handling commented-out keys. // If the key exists (commented or not), it updates it in place. // If the key doesn't exist, it appends it at the end. +// +// A dotted key is written by updateNestedYamlKey, and its result is returned +// as is (TestUpdateYamlKeyLeavesTheNestedPathAlone pins that boundary). A +// single-segment key is edited line by line: a CRLF document comes back LF, +// and the end of the file is kept. An edit in place leaves the trailing +// newline run as it was; an appended key is the new last line, terminated +// only if the file was. func updateYamlKey(content, key, value string) (string, error) { if strings.Contains(key, ".") { if updated, ok, err := updateNestedYamlKey(content, key, value); err != nil { @@ -871,6 +878,14 @@ func updateYamlKey(content, key, value string) (string, error) { } } + // Normalize CRLF the way commentOutYamlKey does. The scan below already + // drops each line's "\r"; this makes the trailing run re-attached at the + // return the LF run too, so a CRLF file ending in blank lines keeps them. + // It sits below the nested dispatch on purpose: some updateNestedYamlKey + // branches carry "\r" through on untouched lines, and this fix leaves them + // alone. + content = strings.ReplaceAll(content, "\r\n", "\n") + formattedValue := formatYamlValue(value) newLine := fmt.Sprintf("%s: %s", key, formattedValue) @@ -906,19 +921,27 @@ func updateYamlKey(content, key, value string) (string, error) { result = append(result, "") } result = append(result, newLine) + + // The file's trailing blank lines were scanned as empty tokens, so they + // already sit ahead of the new line and separate it, and the join is one + // newline short: the terminator bufio.Scanner dropped. Re-attaching + // content's whole run, as the return below does for an edit in place, + // would repeat them after the key: "x\n\n" would come back + // "x\n\nnewkey: \"v\"\n\n", not "x\n\nnewkey: \"v\"\n". A document + // without a trailing newline still does not acquire one. + out := strings.Join(result, "\n") + if strings.HasSuffix(content, "\n") { + out += "\n" + } + return out, nil } - // TODO: `bd config set` is knowingly left unfixed here. This flat-key path - // has the same bufio.Scanner + strings.Join shape that commentOutYamlKey - // had, so it is still exactly one newline short for any terminated - // document: updateYamlKey("issue_prefix: vp\ndolt.mode: server\n", - // "issue_prefix", "zz") returns a string with no terminator, and the - // key-not-found branch above is worse, emitting "x\n\nnewkey: \"v\"" for - // "x\n". The same one-line tail applied at commentOutYamlKey's return - // belongs here too; it is out of scope for a fix aimed at unset. Only this - // flat path is affected -- updateNestedYamlKey re-marshals through - // yaml.Node and is already newline-faithful. - return strings.Join(result, "\n"), nil + // Preserve the document's trailing newlines. bufio.Scanner drops the final + // terminator, so the join is one newline short of any terminated input. Same + // rule as commentOutYamlKey's return: re-attach content's own run, so a + // document without a trailing newline does not acquire one. + out := strings.Join(result, "\n") + return strings.TrimRight(out, "\n") + content[len(strings.TrimRight(content, "\n")):], nil } func updateNestedYamlKey(content, key, value string) (string, bool, error) { diff --git a/internal/config/yaml_unset_newline_test.go b/internal/config/yaml_unset_newline_test.go index 56064c3d2..47dea8907 100644 --- a/internal/config/yaml_unset_newline_test.go +++ b/internal/config/yaml_unset_newline_test.go @@ -7,17 +7,20 @@ import ( "testing" ) -// An unset must not also rewrite the end of the file. commentOutYamlKey used to -// drop the document's trailing newline, so `bd config unset` produced a -// no-newline-at-end-of-file change on top of the line it meant to comment out — -// and did so even for a key the document does not contain, i.e. when nothing -// was edited at all. A config.yaml is git-tracked, so that is a spurious line -// in someone's review. +// Neither an unset nor a set may also rewrite the end of the file. +// commentOutYamlKey and updateYamlKey's flat-key path both used to drop the +// document's trailing newline, so `bd config unset` and `bd config set` each +// produced a no-newline-at-end-of-file change on top of the line they meant to +// edit — and unset did so even for a key the document does not contain, i.e. +// when nothing was edited at all. A config.yaml is git-tracked, so that is a +// spurious line in someone's review. The unset tests come first; the set tests +// follow them, starting at TestUpdateYamlKeyPreservesTheTrailingNewline. // // The guarantee is scoped to LF documents. A CRLF file is still normalized to -// LF by an unset that actually comments a key out — that predates this fix and -// is pinned below as known-and-accepted rather than quietly claimed away. An -// unset that changes nothing no longer rewrites the file at all; see +// LF by an unset that actually comments a key out, and by any set of a +// single-segment key — that predates both fixes and is pinned below as +// known-and-accepted rather than quietly claimed away. An unset that changes +// nothing no longer rewrites the file at all; see // TestUnsetOfAnAbsentKeyLeavesACRLFFileUntouched. func TestCommentOutYamlKeyPreservesTheTrailingNewline(t *testing.T) { for _, tc := range []struct { @@ -60,13 +63,13 @@ func TestCommentOutYamlKeyPreservesTheTrailingNewline(t *testing.T) { } }) - // bufio.ScanLines strips the "\r" from every line, so an unset rewrites a - // CRLF document to LF throughout. That is pre-existing and unchanged by the - // trailing-run logic, but the tail is where it is least visible: TrimRight - // on "\n" leaves the final "\r" behind, so the run that gets re-attached is - // a bare "\n" and the end of the file reads clean while every line ending - // above it changed. Pinned so the normalization is documented, and so a - // future change to it has to be deliberate. + // commentOutYamlKey normalizes CRLF to LF before anything else, so an unset + // rewrites a CRLF document to LF throughout. That is pre-existing and + // unchanged by the trailing-run logic, which measures the run after the + // normalization: each trailing "\r\n" comes back as one "\n", so the end of + // the file reads clean while every line ending above it changed. Pinned so + // the normalization is documented, and so a future change to it has to be + // deliberate. t.Run("CRLF is normalized to LF, trailing run included", func(t *testing.T) { out, err := commentOutYamlKey("issue_prefix: vp\r\ndolt.mode: server\r\n", "dolt.mode") if err != nil { @@ -251,3 +254,217 @@ func TestUnsetOfAPresentKeyInACRLFFileReportsTheWrite(t *testing.T) { assertWritten(t, path) }) } + +// The set twin of TestCommentOutYamlKeyPreservesTheTrailingNewline. +// updateYamlKey's flat-key path read the document with bufio.Scanner and joined +// the lines back with "\n" — the shape commentOutYamlKey used to have — so it +// came back exactly one newline short of any terminated document. Every key +// here is a single segment, because only those reach that path: updateYamlKey +// hands every dotted key to updateNestedYamlKey, a different writer, so a +// dotted-key row would pass without running the code under test at all. +func TestUpdateYamlKeyPreservesTheTrailingNewline(t *testing.T) { + for _, tc := range []struct { + name, content, key string + }{ + {"key present", "issue_prefix: vp\ndolt.mode: server\n", "issue_prefix"}, + {"commented key present", "# actor: \"\"\nother: x\n", "actor"}, + {"key on the last line", "other: x\nactor: a\n", "actor"}, + {"key absent, appended", "issue_prefix: vp\n", "actor"}, + + // As on the unset side, a run of blank lines lost exactly one newline, + // so the output still ended in "\n" and only an exact comparison of the + // run can see the loss. An absent key appended to a file that ends like + // this is not a row here: its blank lines end up ahead of the new key, + // so the run after the key is not the file's run. The appended-key + // subtest below pins those outputs exactly. + {"ends in a blank line", "actor: a\nother: x\n\n", "actor"}, + {"ends in two blank lines", "actor: a\nother: x\n\n\n", "actor"}, + } { + t.Run(tc.name, func(t *testing.T) { + out, err := updateYamlKey(tc.content, tc.key, "b") + if err != nil { + t.Fatalf("updateYamlKey: %v", err) + } + want := tc.content[len(strings.TrimRight(tc.content, "\n")):] + if got := out[len(strings.TrimRight(out, "\n")):]; got != want { + t.Errorf("trailing newline run = %q, want %q:\nin: %q\nout: %q", got, want, tc.content, out) + } + }) + } + + // The in-place branch replaces the one value and leaves every other byte, + // the terminator included, as it was. + t.Run("key present: only the value changes", func(t *testing.T) { + out, err := updateYamlKey("issue_prefix: vp\ndolt.mode: server\n", "issue_prefix", "zz") + if err != nil { + t.Fatalf("updateYamlKey: %v", err) + } + if want := "issue_prefix: \"zz\"\ndolt.mode: server\n"; out != want { + t.Errorf("updateYamlKey:\ngot: %q\nwant: %q", out, want) + } + }) + + // The appended branch makes an absent key the new last line, which is where + // the old join left it unterminated: "x\n" came back "x\n\nnewkey: \"v\"". + // The set adds that one line and nothing else: a blank line before it only + // when the file does not already end in one, and a terminator after it when + // the file had one. + t.Run("appended key: separator kept, terminator restored", func(t *testing.T) { + for _, tc := range []struct{ content, want string }{ + {"x\n", "x\n\nnewkey: \"v\"\n"}, + // A trailing blank line already separates, so no second one is added, + // and the file's blank lines stay ahead of the new key instead of + // being repeated after it: the file comes back whole, followed by one + // terminated line. Re-attaching the file's own run here would end it + // "x\n\nnewkey: \"v\"\n\n", a blank line nobody asked for. + {"x\n\n", "x\n\nnewkey: \"v\"\n"}, + {"x\n\n\n", "x\n\n\nnewkey: \"v\"\n"}, + // The same append on a CRLF file, which comes back LF throughout. + {"x\r\n\r\n", "x\n\nnewkey: \"v\"\n"}, + } { + out, err := updateYamlKey(tc.content, "newkey", "v") + if err != nil { + t.Fatalf("updateYamlKey(%q): %v", tc.content, err) + } + if out != tc.want { + t.Errorf("updateYamlKey(%q):\ngot: %q\nwant: %q", tc.content, out, tc.want) + } + } + }) + + // A document that genuinely has no trailing newline keeps not having one: + // the rule is "preserve", not "always append". One input per branch. + t.Run("absent trailing newline is not invented", func(t *testing.T) { + for _, content := range []string{"actor: a\nother: x", "other: x"} { + out, err := updateYamlKey(content, "actor", "b") + if err != nil { + t.Fatalf("updateYamlKey(%q): %v", content, err) + } + if strings.HasSuffix(out, "\n") { + t.Errorf("a trailing newline was invented for a document that had none: %q -> %q", content, out) + } + } + }) + + // A set of a single-segment key rewrites a CRLF document to LF throughout, + // as an unset does. That predates the fix and is pinned here, like unset's + // subtest of the same name, so that changing it has to be deliberate. What + // the fix changes is the end: the old join dropped the terminator, so + // "…x\r\n" came back "…x". A set now ends the file the way an unset of the + // same document does, blank lines included: both measure the trailing run + // after normalizing, so each trailing "\r\n" comes back as one "\n". + t.Run("CRLF is normalized to LF, trailing run included", func(t *testing.T) { + for _, tc := range []struct{ content, want string }{ + {"actor: a\r\nother: x\r\n", "actor: \"b\"\nother: x\n"}, + // Measured on the raw bytes, the run stops at the last "\r", so + // re-attaching it ends both of these in a single "\n". For the + // second that loses more than the base did, which ended it "…x\n\n". + {"actor: a\r\nother: x\r\n\r\n", "actor: \"b\"\nother: x\n\n"}, + {"actor: a\r\nother: x\r\n\r\n\r\n", "actor: \"b\"\nother: x\n\n\n"}, + } { + out, err := updateYamlKey(tc.content, "actor", "b") + if err != nil { + t.Fatalf("updateYamlKey(%q): %v", tc.content, err) + } + if out != tc.want { + t.Errorf("updateYamlKey(%q):\ngot: %q\nwant: %q", tc.content, out, tc.want) + } + unset, err := commentOutYamlKey(tc.content, "actor") + if err != nil { + t.Fatalf("commentOutYamlKey(%q): %v", tc.content, err) + } + if got, want := out[len(strings.TrimRight(out, "\n")):], unset[len(strings.TrimRight(unset, "\n")):]; got != want { + t.Errorf("set and unset end %q differently: set %q, unset %q", tc.content, got, want) + } + } + }) +} + +// The same property through the public writer, which is what `bd config set` +// actually calls: the twin of TestUnsetThroughTheFileKeepsTheTrailingNewline. +func TestSetThroughTheFileKeepsTheTrailingNewline(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatal(err) + } + path := filepath.Join(beadsDir, "config.yaml") + // The file ends in a blank line, the shape a single appended "\n" does not + // restore. actor is a single-segment key, so the set takes the flat path. + const body = "issue_prefix: vp\nactor: alice\ndolt:\n mode: server\n\n" + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_DIR", beadsDir) + if err := SetYamlConfig("actor", "bob"); err != nil { + t.Fatalf("SetYamlConfig: %v", err) + } + after, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + // The value must have landed, or an untouched file would pass below. + if !strings.Contains(string(after), "actor: \"bob\"\n") { + t.Fatalf("set did not write the value:\n%q", string(after)) + } + want := body[len(strings.TrimRight(body, "\n")):] + if got := string(after)[len(strings.TrimRight(string(after), "\n")):]; got != want { + t.Errorf("set rewrote the end of the file: trailing run %q, want %q\n%q", got, want, string(after)) + } +} + +// The appended branch through the same writer. The file already ends in a +// blank line, which separates the new key, so the set adds exactly one line: +// the change a hand edit would make, and all someone reviewing it should see. +func TestSetOfAnAbsentKeyThroughTheFileAddsOneLine(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatal(err) + } + path := filepath.Join(beadsDir, "config.yaml") + const body = "issue_prefix: vp\nactor: alice\n\n" + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_DIR", beadsDir) + if err := SetYamlConfig("no-push", "true"); err != nil { + t.Fatalf("SetYamlConfig: %v", err) + } + after, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if want := body + "no-push: true\n"; string(after) != want { + t.Errorf("set of an absent key:\ngot: %q\nwant: %q", string(after), want) + } +} + +// updateYamlKey hands every dotted key to updateNestedYamlKey before it +// normalizes CRLF, and has to keep doing so: replaceFlatKeyLine and +// appendNestedYamlKey carry "\r" through on the lines they leave alone (the +// literal dotted key and the comment-only document below), and normalizing +// above the dispatch would silently change what they write. The other two rows +// are re-marshalled to LF either way. This pins the boundary, not those +// outputs: whatever the nested writer makes of a CRLF document, a set of a +// dotted key returns exactly that. +func TestUpdateYamlKeyLeavesTheNestedPathAlone(t *testing.T) { + for _, tc := range []struct{ name, content string }{ + {"literal dotted key", "issue_prefix: vp\r\ndolt.mode: server\r\n"}, + {"nested block", "issue_prefix: vp\r\ndolt:\r\n mode: server\r\n"}, + {"key absent", "issue_prefix: vp\r\n"}, + {"comment-only document", "# dolt.mode: server\r\n"}, + } { + t.Run(tc.name, func(t *testing.T) { + want, ok, err := updateNestedYamlKey(tc.content, "dolt.mode", "embedded") + if err != nil || !ok { + t.Fatalf("updateNestedYamlKey: ok=%v err=%v; nothing to compare against", ok, err) + } + got, err := updateYamlKey(tc.content, "dolt.mode", "embedded") + if err != nil { + t.Fatalf("updateYamlKey: %v", err) + } + if got != want { + t.Errorf("updateYamlKey and updateNestedYamlKey differ on %q:\nset: %q\nnested: %q", tc.content, got, want) + } + }) + } +}