Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions rex/rex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions rex/rex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down