session: expand ~ and env vars in SetCwd

This commit is contained in:
Levi Neely 2026-08-17 14:29:02 +02:00
parent 8a1efe0907
commit 9147fd790a
2 changed files with 45 additions and 1 deletions

View File

@ -100,9 +100,10 @@ func (s *Session) Cwd() string {
}
// SetCwd sets the session working directory.
// Tilde and environment variables in cwd are expanded before storing.
func (s *Session) SetCwd(cwd string) {
s.mu.Lock()
s.cwd = cwd
s.cwd = util.ExpandHome(os.ExpandEnv(cwd))
s.mu.Unlock()
go s.saveSession()
}

View File

@ -0,0 +1,43 @@
package session
import (
"os"
"testing"
)
func TestSetCwd_ExpandsTilde(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil || home == "" {
t.Skip("cannot determine home directory")
}
s := &Session{}
s.SetCwd("~/projects")
want := home + "/projects"
if got := s.Cwd(); got != want {
t.Errorf("SetCwd(~/projects): got %q, want %q", got, want)
}
}
func TestSetCwd_ExpandsEnvVars(t *testing.T) {
t.Setenv("OLLIE_TEST_DIR", "/tmp/testdir")
s := &Session{}
s.SetCwd("$OLLIE_TEST_DIR/sub")
want := "/tmp/testdir/sub"
if got := s.Cwd(); got != want {
t.Errorf("SetCwd($OLLIE_TEST_DIR/sub): got %q, want %q", got, want)
}
}
func TestSetCwd_AbsolutePathUnchanged(t *testing.T) {
s := &Session{}
s.SetCwd("/usr/local/bin")
want := "/usr/local/bin"
if got := s.Cwd(); got != want {
t.Errorf("SetCwd(/usr/local/bin): got %q, want %q", got, want)
}
}