diff --git a/rex/rex.go b/rex/rex.go index 24f5456..d60644e 100644 --- a/rex/rex.go +++ b/rex/rex.go @@ -32,12 +32,15 @@ type Client struct { // New returns a client from REX_SESSION and REX_BLOCK, or false outside Rex. func New(env []string) (*Client, bool) { - session := proc.Getenv(env, "REX_SESSION") + block := proc.Getenv(env, "REX_BLOCK") + // Since Rex 0.1.0-preview.23 a pane gets REX_BLOCK but no REX_SESSION. + // Every rex command takes a block ID for -s and uses the session that holds it. + session := cmp.Or(proc.Getenv(env, "REX_SESSION"), block) if session == "" { return nil, false } c := &Client{ - block: proc.Getenv(env, "REX_BLOCK"), + block: block, session: session, bin: binPath(env), env: env, diff --git a/rex/rex_test.go b/rex/rex_test.go index 8b9bddf..987ed00 100644 --- a/rex/rex_test.go +++ b/rex/rex_test.go @@ -218,6 +218,30 @@ func TestNew(t *testing.T) { } } +// Rex 0.1.0-preview.23 and later set REX_BLOCK but not REX_SESSION, and rex +// takes the block ID for -s. +func TestNewWithoutSession(t *testing.T) { + t.Parallel() + dir := t.TempDir() + testgit.Script(t, dir, "rex", `printf "%s\n" "$*" >> "${0%/*}/calls" +echo '{"blocks":[]}'`) + env := []string{"PATH=/usr/bin:/bin", "REX_BLOCK=block:self", "REX_BIN_DIR=" + dir} + c, ok := rex.New(env) + if !ok || c.Block() != "block:self" { + t.Fatalf("New(%q) = %+v, %t, want a client for block:self", env, c, ok) + } + if _, err := c.Agents(t.Context()); err != nil { + t.Fatalf("Agents: %v", err) + } + calls, err := os.ReadFile(filepath.Join(dir, "calls")) + if err != nil { + t.Fatal(err) + } + if want := "api call -s block:self session.list_blocks {}\n"; !strings.Contains(string(calls), want) { + t.Errorf("rex calls = %q, want %q among them", calls, want) + } +} + func TestConfigPath(t *testing.T) { t.Parallel() tests := []struct {