Skip to content

Commit

Permalink
test(state): Add test coverage for ~half of tmux/state.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffFaer committed Mar 14, 2024
1 parent 3e3be34 commit 53812f4
Show file tree
Hide file tree
Showing 9 changed files with 831 additions and 49 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ $ go work use . api git
### TODOs

- Tests.
- tmux/state PruneSessions
- tmux/state MaybeFindRepository
- cmd new.go
- cmd rename.go
- cmd update.go no args
- cmd update.go with args
- cmd update.go completion suggestions
- Update should accept repo-qualified work unit names.
- tmux display-menu with better work unit ordering.
- tmux hooks to automatically update session names when a session closes.
23 changes: 17 additions & 6 deletions api/repotest/repotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ import (
"github.com/JeffFaer/tmux-vcs-sync/api"
)

type fakeVCS struct{}
// NewVCS creates a new, fake VersionControlSystem that requires all
// repositories to be in the given directory.
func NewVCS(dir string) api.VersionControlSystem {
return fakeVCS{dir}
}

var VCS = fakeVCS{}
type fakeVCS struct {
dir string
}

func (fakeVCS) Name() string { return "fake" }
func (vcs fakeVCS) Name() string { return fmt.Sprintf("fake(%s)", vcs.dir) }
func (fakeVCS) WorkUnitName() string { return "work unit" }
func (fakeVCS) Repository(dir string) (api.Repository, error) {
func (vcs fakeVCS) Repository(dir string) (api.Repository, error) {
if !strings.HasPrefix(dir, vcs.dir) {
return nil, nil
}
return &fakeRepo{
vcs: vcs,
name: filepath.Base(dir),
dir: dir,
cur: "root",
Expand All @@ -25,14 +35,15 @@ func (fakeVCS) Repository(dir string) (api.Repository, error) {
}

type fakeRepo struct {
vcs api.VersionControlSystem
name, dir string

cur string
workUnits map[string]string
}

func (*fakeRepo) VCS() api.VersionControlSystem {
return VCS
func (repo *fakeRepo) VCS() api.VersionControlSystem {
return repo.vcs
}

func (repo *fakeRepo) Name() string {
Expand Down
18 changes: 16 additions & 2 deletions api/repotest/repotest_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package repotest

import "testing"
import (
"path/filepath"
"strings"
"testing"

"github.com/JeffFaer/tmux-vcs-sync/api"
)

func TestFakeRepo(t *testing.T) {
RepoTests(t, VCS.Repository, Options{})
pre := "testing/"
vcs := NewVCS(pre)
newRepo := func(dir string) (api.Repository, error) {
if !strings.HasPrefix(dir, pre) {
dir = filepath.Join("testing", dir)
}
return vcs.Repository(dir)
}
RepoTests(t, newRepo, Options{})
}
8 changes: 2 additions & 6 deletions tmux/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,9 @@ func (srv *server) ListClients() ([]Client, error) {
return res, nil
}

func (srv *server) NewSession(opts ...NewSessionOption) (Session, error) {
opt := &newSessionOptions{}
for _, o := range opts {
o(opt)
}
func (srv *server) NewSession(opts NewSessionOptions) (Session, error) {
args := []string{"new-session", "-d", "-P", "-F", string(SessionID)}
args = append(args, opt.args()...)
args = append(args, opts.args()...)
newSession := srv.command(args...)
newSession.Stdin = os.Stdin // tmux wants a tty.
stdout, err := newSession.RunStdout()
Expand Down
8 changes: 6 additions & 2 deletions tmux/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type State struct {
}

func New(srv tmux.Server) (*State, error) {
return newState(srv, api.Registered)
}

func newState(srv tmux.Server, vcs api.VersionControlSystems) (*State, error) {
sessions, err := srv.ListSessions()
if err != nil {
return nil, err
Expand Down Expand Up @@ -52,7 +56,7 @@ func New(srv tmux.Server) (*State, error) {
repo, ok := reposByDir[path]
if !ok {
var err error
repo, err = api.Registered.MaybeFindRepository(path)
repo, err = vcs.MaybeFindRepository(path)
if err != nil {
logger.Warn("Error while checking for repository in tmux session.", "error", err)
continue
Expand Down Expand Up @@ -113,7 +117,7 @@ func (st *State) NewSession(repo api.Repository, workUnitName string) (tmux.Sess

n := st.sessionNameString(name)
slog.Info("Creating tmux session.", "name", name, "session_name", n)
sesh, err := st.srv.NewSession(tmux.NewSessionName(n), tmux.NewSessionStartDirectory(repo.RootDir()))
sesh, err := st.srv.NewSession(tmux.NewSessionOptions{Name: n, StartDir: repo.RootDir()})
if err != nil {
return nil, fmt.Errorf("failed to create tmux session %q: %w", n, err)
}
Expand Down
Loading

0 comments on commit 53812f4

Please sign in to comment.