Skip to content

Commit

Permalink
fix(tmux): Fix tmux.Server.ListSessions when the server isn't running. (
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffFaer authored Feb 13, 2024
1 parent 55b843e commit 27e0ee4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (cmd *Command) Start() error {
start := time.Now()
logger := slog.Default().With("command", cmd)
logger.Debug("Executing external process. (1/2)")
err := cmd.Start()
err := cmd.cmd.Start()
go func() {
cmd.Wait()
logger.Debug("Executing external process. (2/2)", "elapsed", time.Since(start), "exit_code", cmd.ProcessState.ExitCode())
Expand Down
6 changes: 5 additions & 1 deletion tmux/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ func (srv *Server) Equal(other *Server) bool {
// ListSessions lists the sessions that exist in this tmux server.
// This method will also lookup and cache the given properties.
func (srv *Server) ListSessions() ([]*Session, error) {
stdout, err := srv.command("list-sessions", "-F", string(SessionID)).RunStdout()
stdout, stderr, err := srv.command("list-sessions", "-F", string(SessionID)).RunOutput()
if err != nil {
if strings.Contains(stderr, "no server running") {
return nil, nil
}
fmt.Fprint(os.Stderr, stderr)
return nil, err
}
var res []*Session
Expand Down
8 changes: 7 additions & 1 deletion tmux/tmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ func RunInTTY(t *testing.T, cmd *exec.Command) *os.File {

func TestServer_Sessions(t *testing.T) {
srv := NewServerForTesting(t)

sessions := srv.MustListSessions()
if len(sessions) != 0 {
t.Errorf("New tmux server has %d sessions, expected 0", len(sessions))
}

a := srv.MustNewSession(NewSessionName("a"))
b := srv.MustNewSession(NewSessionName("b"))
c := srv.MustNewSession(NewSessionName("c"))

sessions := srv.MustListSessions()
sessions = srv.MustListSessions()
if diff := cmp.Diff([]TestSession{a, b, c}, sessions, tmuxCmpOpt); diff != "" {
t.Errorf("srv.ListSessions() diff (-want +got)\n%s", diff)
}
Expand Down

0 comments on commit 27e0ee4

Please sign in to comment.