* fix(config): make a dotted key round-trip through SetYamlConfigInDir (bd-zj95)
SetYamlConfigInDir writes dotted keys; GetStringFromDir reads them by splitting
on the dot and walking nested mappings; UnsetYamlConfig removes them. Those are
three directions of ONE round-trip property, and set/get was broken in two ways,
both reachable from ordinary files:
- An empty or comment-only config.yaml has no mapping node, so the nested
writer declined and the caller appended a literal 'dolt.host: ...' line — a
key whose NAME contains a dot. Nothing that splits on the dot can ever find
it.
- A file that already carried such a flat key had it updated in place, which
preserved the unreadable shape forever.
So a caller could write a value and immediately be unable to read it back. Two
consumers found it the hard way: bd's ownership handoff, whose commit write-set
refused itself on dolt.auto-start, and a Gas City managed city, whose
.beads/config.yaml is entirely flat dotted keys.
Six shipped call sites write a dotted key through this path, and every one of
them is a write into a .beads/config.yaml that may be empty or comment-only —
exactly the shape that produced the unreadable spelling:
cmd/bd/bootstrap.go:1108 sync.remote
cmd/bd/dolt.go:481 sync.remote
cmd/bd/init.go:3386, :3394 sync.remote
cmd/bd/migrate_dolt_mode.go:810 dolt.shared-server
cmd/bd/migrate_dolt_mode.go:1168 dolt.shared-server
An empty document now gets a mapping made for it, and a flat key of the name
being written is migrated to the nested form rather than updated in place. Only
the key being written is touched: a dotted key this call does not own belongs to
someone else and is left exactly as they wrote it.
Table test over nine file shapes — absent, empty, comment-only, flat, flat
commented, existing section, unrelated nested, unrelated flat, and mixed — each
asserting both that every key reads back and that no unreadable flat spelling
survives.
(cherry picked from commit cbe9bd6ca03416a76441e560bdec9fa67add99c5)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(config): unset a dotted key in the shape the writer produces
Writing a dotted key nested and then being unable to unset it is the same
round-trip break in the other direction. Set, get and unset are three directions
of ONE property, and all three have to agree on the shape: UnsetYamlConfig
comments out the line matching the key, and its pattern only ever matched a FLAT
'sync.remote:' line. Once the writer nests, an unset silently does nothing and
the value stays live — for sync.remote that means bd keeps a remote the operator
asked it to forget.
commentOutYamlKey now walks the nesting, tracking indentation per segment so a
key is commented only when it sits under its OWN parents rather than under some
other section sharing a leaf name. The flat spelling is still handled, because
files written by older bds carry it.
This is what closes the loop for the shipped unset paths — cmd/bd/config.go:578
('bd config unset'), cmd/bd/dolt.go:1761 and
cmd/bd/dolt_remote_proxied_server.go:34 — against the six writers the previous
commit nests: sync.remote at cmd/bd/bootstrap.go:1108, cmd/bd/dolt.go:481 and
cmd/bd/init.go:3386/:3394, and dolt.shared-server at
cmd/bd/migrate_dolt_mode.go:810/:1168.
TestFinalizeSyncedBootstrapWritesConfigFiles moves with it. It required a
literal 'sync.remote: ' line — a key whose name contains a dot, the one shape
GetStringFromDir and viper can never find, because both split on the dot and
walk nested mappings. That assertion passed for exactly as long as the value was
unreadable and failed the moment it became readable, so it now asserts what
bootstrap actually owes its caller: a remote that reads back.
(cherry picked from the internal/config half of commit
7f1ec4799a3e8af0a89b8f97c28ec30082fedbb7)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* test(cmd/bd): assert the sync.remote round trip, not a YAML spelling
Six cmd/bd test sites baked the flat spelling. They passed BECAUSE the bug was
present, and that is the part worth recording.
Viper finds a literal dotted key as well as a nested one; GetStringFromDir does
not, because it splits on the dot and walks mappings. So these tests exercised
config.yaml through ONE reader while the value was invisible to the other. They
were green for exactly as long as the value was unreadable, and
TestFinalizeSyncedBootstrapWritesConfigFiles failed on macOS and Linux CI the
moment it became readable. A test that only passes while a bug is live does not
merely fail to catch it — it defends it.
bootstrap_guard_test.go:84 seed
bootstrap_test.go:358, :410 seed
bootstrap_test.go:1330 assert (accepted 'sync.remote: ' OR
'sync-remote: ', neither readable)
dolt_worktree_remote_test.go:55 assert
init_remote_test.go:128 seed
All six now assert the round trip through GetStringFromDir, or the caller's own
observable, rather than any YAML text — flat or nested. Asserting the nested
spelling instead would have been the same mistake facing the other way.
The three seeds go through config.SetYamlConfigInDir, the real writer, via a
shared seedSyncRemote helper that asserts the read-back. A fixture can no longer
hand bd a config.yaml that bd itself would never have written.
These are the tests standing over the shipped writers this series fixes:
sync.remote at cmd/bd/bootstrap.go:1108, cmd/bd/dolt.go:481 and
cmd/bd/init.go:3386/:3394, and dolt.shared-server at
cmd/bd/migrate_dolt_mode.go:810/:1168.
(cherry picked from commit b0df00b59d2d68ee5fee313c05dc2300ad66fcef)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(config): close the dotted-key round trip the rest of the way
Review of the first three commits found the round trip still open in two
places, both reachable from ordinary files.
An empty or comment-only config.yaml parses to no YAML nodes at all, and
yaml.v3 keeps no trace of its text either — not even the comments. The
writer fabricated a mapping for that case and marshalled it, so the file
it wrote back was the new key and nothing else. `bd init` writes a
template that is comments and nothing else and then persists sync.remote
into it, so every remote-connected init shipped a config.yaml with its
whole documentation deleted. config.yaml is git-tracked: the operator
sees that as a committed diff they did not make. Append the rendered key
to the existing text instead, so nothing already in the file is parsed,
re-emitted or reformatted, and it survives byte for byte.
An unset comments the leaf out and leaves the section holding nothing —
`sync:` and no more. A null is not a mapping, so the next set refused it,
fell through to the flat writer, and put a literal `sync.remote:` back
into the file: the unreadable shape, re-created one command after being
fixed, with bd reporting success while GetStringFromDir saw nothing. A
section with no value under it has nothing to lose, so treat it as the
empty mapping it looks like.
Two shapes that are genuinely unwritable now say so instead of writing
the flat key and claiming success: a parent that already holds a value
(`sync: enabled`), and a file whose top level is not a mapping. Both
returned nil and produced a value no reader could see.
Unset also matched lines inside literal block scalars, so unsetting
sync.remote rewrote text the operator had written under a `notes: |`.
Skip a block scalar's body: it is data, not structure.
Finally, drop normalizeYamlKey and its empty keyAliases map. It has been
an identity function whose comment claimed it renamed keys.
* docs(changelog): record the dotted config key fix under Unreleased
It is the most user-visible fix on this branch and it was not in the
changelog, so it would not have reached the release notes or
`bd upgrade review`.
* fix(config): preserve existing flat dotted keys (GH#6594)
Keep literal dotted keys in their existing shape, align the direct reader with Viper precedence, and warn when both flat and nested spellings coexist.
Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Rongjun GENG
* fix(config): anchor the dotted unset walk to the key it was asked about
Review fixes for PR #6574 (maintainer iteration 1).
The new nested matcher in `commentOutYamlKey` anchored neither end of the
walk, so `bd config unset <dotted key>` commented out keys it does not own
and could leave the named target live while reporting success — the silent
divergence this PR exists to end. Two independent defects in one loop:
segment 0 matched at any indent, so a `dolt:` section under an unrelated
top-level key seeded the walk; and a key line merely deeper than the matched
parent was stepped over rather than ending the match, so an interposed
section was skipped instead of blocking descent. On
sync:
sub:
remote: keep
remote: target
unsetting `sync.remote` cut `sync.sub.remote` and left `sync.remote` live.
The walk now requires segment 0 at top level and every later segment to be a
direct child, treating any other key line as an opaque subtree to step over.
All four reviewer seeds are pinned in the unset table, which is generalized
to carry the key under test and the keys that must survive — the old table
could not express an over-match, since every case put the leaf one level
under a top-level parent.
Also, from the same review:
- Unset refuses, instead of degrading silently, on the two shapes a line
matcher cannot edit: a key inside a flow-style mapping (it no-opped with a
success exit, leaving the value live) and a key whose value is a block
scalar (the key line was commented and the body stayed behind, re-parsing
as a value of the parent). A key that is not set is still a no-op.
- Updating an existing flat dotted key rewrites that key's line instead of
marshaling the whole document back, which dropped blank separators,
re-indented nested blocks from two to four spaces and collapsed comment
alignment — a whole-file diff in a git-tracked config.yaml from a one-key
set. The line is swapped only when it is the entire entry, keeping the
key's own text and quoting; anything else falls back to the marshal, so a
quoted flat key still reads back.
- The two negative `init` guards assert a read-back rather than the absence
of the pre-PR spelling, which the writer no longer emits and which a real
regression would therefore have sailed past.
Refs: bd-zj95, GH#6594
Maintainer iteration 2 (both findings minor, non-gating; taken because each one
falsified something this commit itself asserts):
- The one-line rewrite kept every other line byte for byte but dropped the
trailing comment on the line it touched — a regression against the PR head,
whose node marshal carried LineComment through, in the branch whose whole
point is leaving a shared config.yaml alone. The note is re-appended from the
parsed comment matched back from the right of the line, so a `#` inside a
quoted value is not mistaken for its start. yaml.v3 hangs that comment on the
value node, not the key node as the review prescribed, except when the value
is empty; both are consulted and both are pinned. The existing pin seeded its
note on a bystander key and so could not see the loss; it is now on the key
under test.
- The unset refusal walked only the nested spelling, so a literal
`dolt.host: |` still had its key line commented out and its body orphaned at
the top level, leaving the whole file unparseable with a success exit — while
the changelog sentence this commit added says that case reports an error. One
lookup of the literal key closes the gap.
The gate's doc comment no longer claims to cover the class; it is two known
shapes, and it names the members still outside it.
---------
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Rongjun GENG <rj.geng@gmail.com>
241 lines
8 KiB
Go
241 lines
8 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
)
|
|
|
|
func TestInitCommandRegistersRemoteFlag(t *testing.T) {
|
|
if initCmd.Flags().Lookup("remote") == nil {
|
|
t.Fatal("init command does not register --remote")
|
|
}
|
|
}
|
|
|
|
func TestInitExplicitRemoteDrivesCloneAndPersistence(t *testing.T) {
|
|
const remote = "git+ssh://git@example.com/right/repo.git"
|
|
resolveConfiguredRemote := func() string {
|
|
return "git+ssh://git@example.com/wrong/repo.git"
|
|
}
|
|
|
|
syncURL, source := resolveInitConfiguredSyncRemote(remote, true, resolveConfiguredRemote)
|
|
if syncURL != remote {
|
|
t.Fatalf("syncURL = %q, want explicit remote %q", syncURL, remote)
|
|
}
|
|
if source != initSyncRemoteExplicit {
|
|
t.Fatalf("source = %v, want explicit", source)
|
|
}
|
|
syncFromRemote := syncURL != ""
|
|
syncURLFromConfig := syncURL != "" && source != initSyncRemoteNone
|
|
if !syncFromRemote {
|
|
t.Fatal("explicit remote did not select clone-from-remote path")
|
|
}
|
|
if !syncURLFromConfig {
|
|
t.Fatal("explicit remote was not treated as user-configured sync URL")
|
|
}
|
|
|
|
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("# Beads Config\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := persistInitSyncRemote(beadsDir, remote, syncURL, syncFromRemote, syncURLFromConfig, false); err != nil {
|
|
t.Fatalf("persistInitSyncRemote failed: %v", err)
|
|
}
|
|
configBytes, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(configBytes), remote) {
|
|
t.Fatalf("config.yaml does not contain explicit remote %q:\n%s", remote, configBytes)
|
|
}
|
|
}
|
|
|
|
func TestInitServerExternalRemoteUsesServerCloneMode(t *testing.T) {
|
|
if got := initRemoteCloneMode(true, true); got != remoteCloneExternalServer {
|
|
t.Fatalf("initRemoteCloneMode(server=true, external=true) = %v, want external server clone mode", got)
|
|
}
|
|
}
|
|
|
|
func TestInitDoltServerTLSFromEnv(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
env string
|
|
want bool
|
|
}{
|
|
{name: "unset", want: false},
|
|
{name: "zero", env: "0", want: false},
|
|
{name: "false", env: "false", want: false},
|
|
{name: "one", env: "1", want: true},
|
|
{name: "true", env: "true", want: true},
|
|
{name: "mixed case true", env: "TrUe", want: true},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if tc.env == "" {
|
|
t.Setenv("BEADS_DOLT_SERVER_TLS", "")
|
|
} else {
|
|
t.Setenv("BEADS_DOLT_SERVER_TLS", tc.env)
|
|
}
|
|
if got := initDoltServerTLSFromEnv(); got != tc.want {
|
|
t.Fatalf("initDoltServerTLSFromEnv() = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInitExplicitEmptyRemoteSkipsAmbientConfig(t *testing.T) {
|
|
called := false
|
|
resolveConfiguredRemote := func() string {
|
|
called = true
|
|
return "git+ssh://git@example.com/ambient/repo.git"
|
|
}
|
|
|
|
syncURL, source := resolveInitConfiguredSyncRemote("", true, resolveConfiguredRemote)
|
|
if called {
|
|
t.Fatal("explicit empty --remote consulted ambient sync config")
|
|
}
|
|
if syncURL != "" {
|
|
t.Fatalf("syncURL = %q, want empty explicit remote", syncURL)
|
|
}
|
|
if source != initSyncRemoteExplicit {
|
|
t.Fatalf("source = %v, want explicit", source)
|
|
}
|
|
if syncURL != "" {
|
|
t.Fatal("explicit empty --remote would trigger early remote safety")
|
|
}
|
|
}
|
|
|
|
func TestPersistInitSyncRemoteExplicitRemoteWritesTargetDir(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
callerDir := filepath.Join(tmpDir, "caller")
|
|
targetBeadsDir := filepath.Join(tmpDir, "target", ".beads")
|
|
callerBeadsDir := filepath.Join(callerDir, ".beads")
|
|
if err := os.MkdirAll(callerBeadsDir, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(targetBeadsDir, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
callerConfig := filepath.Join(callerBeadsDir, "config.yaml")
|
|
const callerRemote = "git+ssh://git@example.com/wrong/repo.git"
|
|
seedSyncRemote(t, callerBeadsDir, callerRemote)
|
|
targetConfig := filepath.Join(targetBeadsDir, "config.yaml")
|
|
if err := os.WriteFile(targetConfig, []byte("# Beads Config\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(callerDir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const remote = "git+ssh://git@example.com/right/repo.git"
|
|
if err := persistInitSyncRemote(targetBeadsDir, remote, remote, false, true, false); err != nil {
|
|
t.Fatalf("persistInitSyncRemote failed: %v", err)
|
|
}
|
|
|
|
targetBytes, err := os.ReadFile(targetConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(targetBytes), remote) {
|
|
t.Fatalf("target config.yaml does not contain explicit remote %q:\n%s", remote, targetBytes)
|
|
}
|
|
|
|
callerBytes, err := os.ReadFile(callerConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(callerBytes), remote) {
|
|
t.Fatalf("caller config.yaml was modified instead of target:\n%s", callerBytes)
|
|
}
|
|
// Stronger than the absence above: the caller still resolves to what it had.
|
|
// A write that landed somewhere unreadable would satisfy "does not contain
|
|
// the new remote" while having quietly destroyed the old one.
|
|
if got := config.GetStringFromDir(callerBeadsDir, "sync.remote"); got != callerRemote {
|
|
t.Fatalf("caller sync.remote = %q, want it untouched at %q:\n%s", got, callerRemote, callerBytes)
|
|
}
|
|
}
|
|
|
|
// `bd init --remote` runs createConfigYaml and then immediately writes
|
|
// sync.remote into what it just created. That template is comments and nothing
|
|
// else, which is the one document shape yaml.v3 parses to no nodes at all — so a
|
|
// writer that round-trips a node tree has nothing to write back but the key it
|
|
// added, and the operator's first commit after init carries a config.yaml with
|
|
// its entire documentation deleted. It is git-tracked, so they see it.
|
|
//
|
|
// This runs the real pair in the real order rather than asserting on a replica
|
|
// of the template, so it stays true if either side moves.
|
|
func TestInitPersistingSyncRemoteKeepsTheConfigTemplate(t *testing.T) {
|
|
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := createConfigYaml(beadsDir, false, ""); err != nil {
|
|
t.Fatalf("createConfigYaml: %v", err)
|
|
}
|
|
template, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
const remote = "git+ssh://git@example.com/right/repo.git"
|
|
if err := persistInitSyncRemote(beadsDir, remote, remote, false, true, false); err != nil {
|
|
t.Fatalf("persistInitSyncRemote: %v", err)
|
|
}
|
|
|
|
body, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, line := range strings.Split(string(template), "\n") {
|
|
if strings.TrimSpace(line) == "" {
|
|
continue
|
|
}
|
|
if !strings.Contains(string(body), line) {
|
|
t.Errorf("init dropped a line of its own config template: %q\ngot:\n%s", line, body)
|
|
}
|
|
}
|
|
if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != remote {
|
|
t.Errorf("sync.remote = %q, want %q\n%s", got, remote, body)
|
|
}
|
|
}
|
|
|
|
func TestInitTimeCloneConfigExternalDefaultsAreSelfContained(t *testing.T) {
|
|
t.Setenv("BEADS_DOLT_SERVER_HOST", "")
|
|
t.Setenv("BEADS_DOLT_SERVER_PORT", "")
|
|
t.Setenv("BEADS_DOLT_PORT", "")
|
|
t.Setenv("BEADS_DOLT_SERVER_USER", "")
|
|
t.Setenv("BEADS_DOLT_SERVER_DATABASE", "")
|
|
|
|
cfg := initTimeCloneConfig(true, "", 3312, "", "", "beads_proj")
|
|
|
|
if cfg.GetDoltMode() != configfile.DoltModeServer {
|
|
t.Fatalf("dolt mode = %q, want server", cfg.GetDoltMode())
|
|
}
|
|
if cfg.GetDoltServerHost() != configfile.DefaultDoltServerHost {
|
|
t.Fatalf("host = %q, want default %q", cfg.GetDoltServerHost(), configfile.DefaultDoltServerHost)
|
|
}
|
|
if cfg.GetDoltServerUser() != configfile.DefaultDoltServerUser {
|
|
t.Fatalf("user = %q, want default %q", cfg.GetDoltServerUser(), configfile.DefaultDoltServerUser)
|
|
}
|
|
if cfg.GetDoltServerPort() != 3312 {
|
|
t.Fatalf("port = %d, want 3312", cfg.GetDoltServerPort())
|
|
}
|
|
if cfg.GetDoltDatabase() != "beads_proj" {
|
|
t.Fatalf("database = %q, want beads_proj", cfg.GetDoltDatabase())
|
|
}
|
|
}
|