* test(cmd/bd): pin workspace discovery refusal text and exit codes
Add before-parity pins for what an ordinary bd prints, and with which
exit code, when workspace discovery cannot select a usable workspace:
- no .beads, an empty .beads, or a .beads with config.yaml but no
metadata.json and no database ("no beads database found", and bd
where's text and --json refusals);
- a corrupt metadata.json, for list, create, where (all refuse) and
version (warns, exits 0);
- an exported BEADS_DIR naming a missing path, an empty directory, a
regular file or a corrupt workspace, run from a corrupt workspace so a
fallback to cwd discovery would show;
- .beads/.env: BEADS_DIR routing, an exported BEADS_DIR winning, BEADS_DB
routing bd where but not a data command, BD_JSON applying only in the
selected workspace, and a .env BD_BACKEND not being refused;
- .beads/redirect: missing and non-workspace targets (warning, then the
source), a corrupt target, a chain, and a redirect behind BEADS_DIR;
- --db paths and names, and BEADS_DB / BD_DB, including the two
inconsistencies main has today (a data command ignores BD_DB, and lets
a discoverable cwd workspace shadow BEADS_DB, while bd where honors
both);
- BD_BACKEND / BD_DATABASE_BACKEND being refused before any of the
above, for store-less commands too.
The cases use workspaces with a corrupt metadata.json as sentinels: the
refusal names the metadata.json it failed to parse, which shows the
directory discovery selected without opening a store, so the store-less
pins need neither Dolt nor cgo. One embedded-Dolt pin covers the
store-backed .env case: BD_READONLY=true in .beads/.env refuses a write
with the read-only text and exit 1, reads still run, and an exported
BD_READONLY wins over the file.
These pin today's origin/main, not an ideal. A change that reorders
admission, discovery or checkBlockedEnvVars in the root PersistentPreRunE
now has to change an expectation here and is reviewed as a test diff.
Built from the merge base with the graph fork (46aa59cc65), bd passes
every store-less pin; the fork tip fails two (a BEADS_DIR naming a
regular file exits 5 with graph_not_initialized instead of exit 1, also
when BD_BACKEND is set).
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* test(cmd/bd): keep the discovery pins independent of PATH and cgo
The version case under TestDiscoveryRefusalPinsCorruptMetadata pinned
bd version's full stderr. bd version also appends a "Warning: multiple
'bd' binaries found in PATH" block when the caller's PATH holds more
than one distinct bd binary (a release install plus a dev build, or
Homebrew plus go install), and the fixture passes the caller's PATH
through. Such a machine failed the pin spuriously. normalize now drops
that block from stderr, as it already drops the sandbox auto-detection
notice, so the rest of the case is still pinned exactly.
The file also said the store-less pins need no cgo. They need a cgo bd.
A CGO_ENABLED=0 bd is server-only, and its no-database hints add "run
'bd doctor' to diagnose", so a CGO_ENABLED=0 go test of the package
failed 16 cases on the hint wording alone. The file now builds only
with cgo, and its header says why. CI runs none of these in its
CGO_ENABLED=0 lane, whose -run subset is fixed by PURE_CMD_BD_TESTS.
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* test(cmd/bd): pin a working-directory .beads that is a regular file
The ExplicitBeadsDir pins cover a BEADS_DIR that names a regular file.
This adds the same case reached by the working-directory walk instead:
a project whose .beads is a regular file. bd list and bd where both exit
1 with "Error: loading config: reading config: open
$ROOT/p/.beads/metadata.json: not a directory".
bd built from the fork merge base 46aa59cc65 passes both cases. The
fork tip b9eebc31f8 fails both: it exits 5 with "graph_not_initialized:
open $ROOT/p/.beads/graph-preview-format: not a directory", the same
reorder the BEADS_DIR pin already catches.
A .beads that is an unreadable directory (chmod 000) splits main and
the fork the same way, but it is not pinned: a test run as root can
still read the directory.
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* test(cmd/bd): close the embedded discovery pin's environment
Maintainer fixup from the adoption review of #7677.
TestEmbeddedDiscoveryPinsEnvFileReadonly compares bd's output exactly,
but its run helper started from bdEnv, which drops only BEADS_* and
BD_ROUTING_MODE. Every other BD_* the caller exported reached bd: with
BD_OTEL_METRICS_URL/BD_OTEL_LOGS_URL set (as on agent hosts) the
deprecation warning lands in front of the read-only refusal, with
BD_JSON_ENVELOPE=1 the empty list is printed as an envelope, and in a
sandbox the "Sandbox detected" notice precedes the refusal; each failed
the pin. The run helper now uses the store-less pins' closed environment,
factored out of discoveryPinFixture.baseEnv as discoveryPinEnv (HOME stays
the directory bd init used), and drops the sandbox notice from stderr
with the same discoveryPinSandboxNotice pattern normalize now uses.
normalize collapsed every consecutive repeat of a stderr line, which also
hid a doubled "Error:" line. It now folds only repeated
"Warning: redirect" lines, the ones discovery prints once per re-resolve.
blocked-backend-not-checked used a corrupt-metadata sentinel, whose
bind-time refusal comes before the legacy guard, so a blocked-variable
check added after .env loading would not have changed its output. It now
uses a config.yaml-only workspace and pins discovery's no-database
refusal.
Measured with the PR head's bd and test binaries under env -i:
- the embedded pin passes on a clean environment and failed with
BD_OTEL_*, BD_JSON_ENVELOPE=1, or kill(2) returning EPERM (strace
inject); with this change it passes in all of them, plus an exported
XDG_CONFIG_HOME, an existing BD_MIGRATION_FREEZE_FILE and the agent
host's BD_*/BEADS_* set;
- it still fails when .beads/.env overrides the caller (gotenv.OverLoad)
or is not loaded;
- the store-less suite passes 54/54 subtests; a bd that prints the
legacy-guard error twice now fails 12 of them, and a bd that checks
the blocked variables again after the legacy guard now fails
blocked-backend-not-checked; both passed the suite before.
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com>
86 lines
3.3 KiB
Go
86 lines
3.3 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// TestEmbeddedDiscoveryPinsEnvFileReadonly is the store-backed member of the
|
|
// before-parity discovery pins (discovery_refusal_pins_test.go holds the
|
|
// store-less ones). It pins how an ordinary workspace applies a BD_READONLY
|
|
// supplied by its own .beads/.env: a write is refused with the read-only text
|
|
// and exit 1, also under --json, while reads still run; and a BD_READONLY the
|
|
// caller exports wins over the file.
|
|
func TestEmbeddedDiscoveryPinsEnvFileReadonly(t *testing.T) {
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
|
|
}
|
|
t.Parallel()
|
|
|
|
bd := buildEmbeddedBD(t)
|
|
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "pin")
|
|
if err := os.WriteFile(filepath.Join(beadsDir, ".env"), []byte("BD_READONLY=true\n"), 0o600); err != nil {
|
|
t.Fatalf("write .beads/.env: %v", err)
|
|
}
|
|
|
|
run := func(extraEnv []string, args ...string) (exit int, stdout, stderr string) {
|
|
t.Helper()
|
|
cmd := exec.Command(bd, args...)
|
|
cmd.Dir = dir
|
|
// The store-less pins' closed environment: no BD_*/BEADS_* the caller
|
|
// exports (BD_READONLY, BD_JSON_ENVELOPE, BD_OTEL_*, ...) reaches bd.
|
|
// HOME stays dir, where bd init ran.
|
|
cmd.Env = append(discoveryPinEnv(dir, filepath.Join(dir, "no-such-freeze-marker")),
|
|
"BEADS_DOLT_AUTO_START=0", "BEADS_NO_DAEMON=1")
|
|
cmd.Env = append(cmd.Env, extraEnv...)
|
|
var out, errOut bytes.Buffer
|
|
cmd.Stdout, cmd.Stderr = &out, &errOut
|
|
if err := cmd.Run(); err != nil {
|
|
var exitErr *exec.ExitError
|
|
if !errors.As(err, &exitErr) {
|
|
t.Fatalf("run bd %v: %v", args, err)
|
|
}
|
|
exit = exitErr.ExitCode()
|
|
}
|
|
return exit, out.String(), discoveryPinSandboxNotice.ReplaceAllLiteralString(errOut.String(), "")
|
|
}
|
|
|
|
exit, stdout, stderr := run(nil, "create", "refused by .env", "--json")
|
|
if exit != 1 || stdout != "" || stderr != "Error: operation 'create' is not allowed in read-only mode\n" {
|
|
t.Fatalf("create under .env BD_READONLY=true: exit=%d stdout=%q stderr=%q; want exit=1, no stdout, the read-only refusal", exit, stdout, stderr)
|
|
}
|
|
|
|
exit, stdout, stderr = run(nil, "list", "--json")
|
|
if exit != 0 || stdout != "[]\n" {
|
|
t.Fatalf("list under .env BD_READONLY=true: exit=%d stdout=%q stderr=%q; want exit=0 and an empty list", exit, stdout, stderr)
|
|
}
|
|
|
|
exit, stdout, stderr = run([]string{"BD_READONLY=false"}, "create", "allowed by the caller", "--json")
|
|
if exit != 0 {
|
|
t.Fatalf("create with exported BD_READONLY=false: exit=%d stdout=%q stderr=%q; want the caller's setting to win", exit, stdout, stderr)
|
|
}
|
|
var created struct {
|
|
Title string `json:"title"`
|
|
}
|
|
if err := json.Unmarshal([]byte(stdout), &created); err != nil || created.Title != "allowed by the caller" {
|
|
t.Fatalf("create with exported BD_READONLY=false printed %q (decode error %v)", stdout, err)
|
|
}
|
|
|
|
exit, stdout, stderr = run(nil, "list", "--json")
|
|
var listed []struct {
|
|
Title string `json:"title"`
|
|
}
|
|
if exit != 0 {
|
|
t.Fatalf("list after the allowed create: exit=%d stderr=%q", exit, stderr)
|
|
}
|
|
if err := json.Unmarshal([]byte(stdout), &listed); err != nil || len(listed) != 1 || listed[0].Title != "allowed by the caller" {
|
|
t.Fatalf("list after the allowed create = %q (decode error %v); want only the allowed issue, the refused create wrote nothing", stdout, err)
|
|
}
|
|
}
|