summaryrefslogtreecommitdiff
path: root/internal/tmux
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-09-08 12:02:40 +0300
committerPaul Buetow <paul@buetow.org>2025-09-08 12:02:40 +0300
commit75cf6abd55bfb60324fc47cf91eac08dbb8b87b4 (patch)
tree6ef90d8014fe4d9a757d3f7e95bf736b70e4c685 /internal/tmux
parent0dcf347c3fbc6e4ffb7e46294f5dd92dbbcd98ef (diff)
docs: move tmux documentation to its own file
Diffstat (limited to 'internal/tmux')
-rw-r--r--internal/tmux/tmux.go18
-rw-r--r--internal/tmux/tmux_test.go135
2 files changed, 79 insertions, 74 deletions
diff --git a/internal/tmux/tmux.go b/internal/tmux/tmux.go
index 63b5660..6d75a44 100644
--- a/internal/tmux/tmux.go
+++ b/internal/tmux/tmux.go
@@ -1,18 +1,20 @@
package tmux
import (
- "os"
- "os/exec"
- "strconv"
- "strings"
+ "os"
+ "os/exec"
+ "strconv"
+ "strings"
)
// Available reports whether tmux is available and we appear to be in a tmux session.
func Available() bool { return HasBinary() && InSession() }
// HasBinary reports whether the tmux binary is on PATH.
-var lookPath = exec.LookPath
-var command = exec.Command
+var (
+ lookPath = exec.LookPath
+ command = exec.Command
+)
func HasBinary() bool { _, err := lookPath("tmux"); return err == nil }
@@ -47,8 +49,8 @@ func SplitRun(opts SplitOpts, argv []string) error {
// tmux takes a single command string. Use a conservative shell join.
cmdStr := shellJoin(argv)
args = append(args, cmdStr)
- c := command("tmux", args...)
- return c.Run()
+ c := command("tmux", args...)
+ return c.Run()
}
// shellJoin quotes argv elements for safe use in a single shell command string.
diff --git a/internal/tmux/tmux_test.go b/internal/tmux/tmux_test.go
index b18c8a3..4db2e4a 100644
--- a/internal/tmux/tmux_test.go
+++ b/internal/tmux/tmux_test.go
@@ -1,82 +1,85 @@
package tmux
import (
- "errors"
- "os"
- "os/exec"
- "testing"
+ "errors"
+ "os"
+ "os/exec"
+ "testing"
)
func TestInSession(t *testing.T) {
- t.Setenv("TMUX", "/tmp/tmux-123,123,0")
- if !InSession() {
- t.Fatal("expected InSession true when TMUX is set")
- }
- t.Setenv("TMUX", "")
- if InSession() {
- t.Fatal("expected InSession false when TMUX is empty")
- }
+ t.Setenv("TMUX", "/tmp/tmux-123,123,0")
+ if !InSession() {
+ t.Fatal("expected InSession true when TMUX is set")
+ }
+ t.Setenv("TMUX", "")
+ if InSession() {
+ t.Fatal("expected InSession false when TMUX is empty")
+ }
}
func TestHasBinary_UsesLookPath(t *testing.T) {
- old := lookPath
- t.Cleanup(func() { lookPath = old })
- lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
- if !HasBinary() {
- t.Fatal("expected HasBinary true when lookPath succeeds")
- }
- lookPath = func(file string) (string, error) { return "", errors.New("nope") }
- if HasBinary() {
- t.Fatal("expected HasBinary false when lookPath fails")
- }
+ old := lookPath
+ t.Cleanup(func() { lookPath = old })
+ lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
+ if !HasBinary() {
+ t.Fatal("expected HasBinary true when lookPath succeeds")
+ }
+ lookPath = func(file string) (string, error) { return "", errors.New("nope") }
+ if HasBinary() {
+ t.Fatal("expected HasBinary false when lookPath fails")
+ }
}
func TestSplitRun_AssemblesArgs(t *testing.T) {
- captured := struct{ name string; args []string }{}
- oldCmd := command
- t.Cleanup(func() { command = oldCmd })
- command = func(name string, args ...string) *exec.Cmd {
- captured.name = name
- captured.args = append([]string(nil), args...)
- // Use a benign command that exits 0
- return exec.Command("true")
- }
- opts := SplitOpts{Target: ":.", Vertical: true, Percent: 40}
- argv := []string{"/path/to/bin", "-flag", "value with spaces", "and'quote"}
- if err := SplitRun(opts, argv); err != nil {
- t.Fatalf("SplitRun error: %v", err)
- }
- if captured.name != "tmux" {
- t.Fatalf("expected tmux, got %q", captured.name)
- }
- wantFlags := map[string]bool{"split-window": true, "-v": true, "-p": true, "40": true, "-t": true, ":.": true}
- for _, a := range captured.args[:len(captured.args)-1] {
- if wantFlags[a] {
- delete(wantFlags, a)
- }
- }
- if len(wantFlags) != 0 {
- t.Fatalf("missing expected flags: %v", wantFlags)
- }
- last := captured.args[len(captured.args)-1]
- if last == "" || last == argv[0] {
- t.Fatalf("expected last arg to be joined command string, got %q", last)
- }
- _ = os.Unsetenv("TMUX")
+ captured := struct {
+ name string
+ args []string
+ }{}
+ oldCmd := command
+ t.Cleanup(func() { command = oldCmd })
+ command = func(name string, args ...string) *exec.Cmd {
+ captured.name = name
+ captured.args = append([]string(nil), args...)
+ // Use a benign command that exits 0
+ return exec.Command("true")
+ }
+ opts := SplitOpts{Target: ":.", Vertical: true, Percent: 40}
+ argv := []string{"/path/to/bin", "-flag", "value with spaces", "and'quote"}
+ if err := SplitRun(opts, argv); err != nil {
+ t.Fatalf("SplitRun error: %v", err)
+ }
+ if captured.name != "tmux" {
+ t.Fatalf("expected tmux, got %q", captured.name)
+ }
+ wantFlags := map[string]bool{"split-window": true, "-v": true, "-p": true, "40": true, "-t": true, ":.": true}
+ for _, a := range captured.args[:len(captured.args)-1] {
+ if wantFlags[a] {
+ delete(wantFlags, a)
+ }
+ }
+ if len(wantFlags) != 0 {
+ t.Fatalf("missing expected flags: %v", wantFlags)
+ }
+ last := captured.args[len(captured.args)-1]
+ if last == "" || last == argv[0] {
+ t.Fatalf("expected last arg to be joined command string, got %q", last)
+ }
+ _ = os.Unsetenv("TMUX")
}
func TestAvailable(t *testing.T) {
- oldLook := lookPath
- t.Cleanup(func() { lookPath = oldLook })
- // Present binary + TMUX set -> available
- lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
- t.Setenv("TMUX", "/tmp/tmux-1,1,1")
- if !Available() {
- t.Fatal("expected Available true with TMUX + binary")
- }
- // No binary -> not available
- lookPath = func(file string) (string, error) { return "", errors.New("nope") }
- if Available() {
- t.Fatal("expected Available false without binary")
- }
+ oldLook := lookPath
+ t.Cleanup(func() { lookPath = oldLook })
+ // Present binary + TMUX set -> available
+ lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
+ t.Setenv("TMUX", "/tmp/tmux-1,1,1")
+ if !Available() {
+ t.Fatal("expected Available true with TMUX + binary")
+ }
+ // No binary -> not available
+ lookPath = func(file string) (string, error) { return "", errors.New("nope") }
+ if Available() {
+ t.Fatal("expected Available false without binary")
+ }
}