Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple misc updates. Mostly around repository implementation testing. #31

Merged
merged 3 commits into from
Apr 4, 2024
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
77 changes: 1 addition & 76 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,14 @@ var (

// Register registers a VCS for use by tmux-vcs-sync.
func Register(vcs VersionControlSystem) {
registered = append(registered, TracingVersionControlSystem(vcs))
registered = append(registered, &tracingVersionControlSystem{vcs})
}

// Registered is all of the VersionControlSystems added via Register.
func Registered() VersionControlSystems {
return slices.Clone(registered)
}

// TracingVersionControlSystem wraps the provided VersionControlSystem so that
// it automatically creates trace regions.
func TracingVersionControlSystem(vcs VersionControlSystem) VersionControlSystem {
return &tracingVersionControlSystem{vcs}
}

// CurrentRepository returns a Repository for the current working directory, or an error if one cannot be found.
func (all VersionControlSystems) CurrentRepository(ctx context.Context) (Repository, error) {
repo, err := all.MaybeCurrentRepository(ctx)
Expand Down Expand Up @@ -165,72 +159,3 @@ func MaybeFindRepository[T any](ctx context.Context, elems []T, fn func(T) (Repo
return nil, fmt.Errorf("multiple Repositories match: %s", strings.Join(s, ", "))
}
}

type tracingVersionControlSystem struct {
vcs VersionControlSystem
}

func (vcs *tracingVersionControlSystem) Name() string { return vcs.vcs.Name() }
func (vcs *tracingVersionControlSystem) WorkUnitName() string { return vcs.vcs.WorkUnitName() }

func (vcs *tracingVersionControlSystem) Repository(ctx context.Context, name string) (Repository, error) {
defer trace.StartRegion(ctx, "VCS:"+vcs.Name()).End()
repo, err := vcs.vcs.Repository(ctx, name)
if err != nil {
return nil, err
}
if repo == nil {
return nil, nil
}
return &tracingRepository{repo}, nil
}

type tracingRepository struct {
repo Repository
}

func (repo *tracingRepository) VCS() VersionControlSystem { return repo.repo.VCS() }
func (repo *tracingRepository) Name() string { return repo.repo.Name() }
func (repo *tracingRepository) RootDir() string { return repo.repo.RootDir() }

func (repo *tracingRepository) startRegions(ctx context.Context) func() {
r1 := trace.StartRegion(ctx, "VCS:"+repo.VCS().Name())
r2 := trace.StartRegion(ctx, "Repo:"+repo.Name())
return func() {
r2.End()
r1.End()
}
}

func (repo *tracingRepository) Current(ctx context.Context) (string, error) {
defer repo.startRegions(ctx)()
return repo.repo.Current(ctx)
}
func (repo *tracingRepository) List(ctx context.Context, prefix string) ([]string, error) {
defer repo.startRegions(ctx)()
return repo.repo.List(ctx, prefix)
}
func (repo *tracingRepository) Sort(ctx context.Context, workUnits []string) error {
defer repo.startRegions(ctx)()
return repo.repo.Sort(ctx, workUnits)
}
func (repo *tracingRepository) New(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.New(ctx, workUnitName)
}
func (repo *tracingRepository) Commit(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.Commit(ctx, workUnitName)
}
func (repo *tracingRepository) Rename(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.Rename(ctx, workUnitName)
}
func (repo *tracingRepository) Exists(ctx context.Context, workUnitName string) (bool, error) {
defer repo.startRegions(ctx)()
return repo.repo.Exists(ctx, workUnitName)
}
func (repo *tracingRepository) Update(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.Update(ctx, workUnitName)
}
15 changes: 14 additions & 1 deletion api/repotest/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type Options struct {
ExtraListWorkUnitPrefixes []ListWorkUnitTestCase

Parallel bool

// The deadline to use for test cases.
// Defaults to 5s.
Deadline time.Duration
}

type ListWorkUnitTestCase struct {
Expand All @@ -37,6 +41,10 @@ type ListWorkUnitTestCase struct {
type repoCtor func(*testing.T) api.Repository

func RepoTests(t *testing.T, ctor func(context.Context, *testing.T, string) (api.Repository, error), opts Options) {
if opts.Deadline == 0 {
opts.Deadline = 5 * time.Second
}

for n, tc := range map[string]func(context.Context, *testing.T, repoCtor, Options){
"EmptyRepository": testEmptyRepository,
"New": testNew,
Expand All @@ -50,7 +58,7 @@ func RepoTests(t *testing.T, ctor func(context.Context, *testing.T, string) (api
if opts.Parallel {
t.Parallel()
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), opts.Deadline)
defer cancel()
ctor := func(t *testing.T) api.Repository {
repo, err := ctor(ctx, t, t.Name())
Expand Down Expand Up @@ -350,6 +358,11 @@ func testSort(ctx context.Context, t *testing.T, ctor repoCtor, opts Options) {
workUnits: []string{"wxyz"},
wantErr: true,
},
{
name: "Empty",

workUnits: []string{},
},
} {
t.Run(tc.name, func(t *testing.T) {
want := slices.Clone(tc.workUnits)
Expand Down
77 changes: 77 additions & 0 deletions api/tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package api

import (
"context"
"runtime/trace"
)

// tracingVersionControlSystem wraps the provided VersionControlSystem so that
// it automatically creates trace regions.
type tracingVersionControlSystem struct {
vcs VersionControlSystem
}

func (vcs *tracingVersionControlSystem) Name() string { return vcs.vcs.Name() }
func (vcs *tracingVersionControlSystem) WorkUnitName() string { return vcs.vcs.WorkUnitName() }

func (vcs *tracingVersionControlSystem) Repository(ctx context.Context, name string) (Repository, error) {
defer trace.StartRegion(ctx, "VCS:"+vcs.Name()).End()
repo, err := vcs.vcs.Repository(ctx, name)
if err != nil {
return nil, err
}
if repo == nil {
return nil, nil
}
return &tracingRepository{repo}, nil
}

type tracingRepository struct {
repo Repository
}

func (repo *tracingRepository) VCS() VersionControlSystem { return repo.repo.VCS() }
func (repo *tracingRepository) Name() string { return repo.repo.Name() }
func (repo *tracingRepository) RootDir() string { return repo.repo.RootDir() }

func (repo *tracingRepository) startRegions(ctx context.Context) func() {
r1 := trace.StartRegion(ctx, "VCS:"+repo.VCS().Name())
r2 := trace.StartRegion(ctx, "Repo:"+repo.Name())
return func() {
r2.End()
r1.End()
}
}

func (repo *tracingRepository) Current(ctx context.Context) (string, error) {
defer repo.startRegions(ctx)()
return repo.repo.Current(ctx)
}
func (repo *tracingRepository) List(ctx context.Context, prefix string) ([]string, error) {
defer repo.startRegions(ctx)()
return repo.repo.List(ctx, prefix)
}
func (repo *tracingRepository) Sort(ctx context.Context, workUnits []string) error {
defer repo.startRegions(ctx)()
return repo.repo.Sort(ctx, workUnits)
}
func (repo *tracingRepository) New(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.New(ctx, workUnitName)
}
func (repo *tracingRepository) Commit(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.Commit(ctx, workUnitName)
}
func (repo *tracingRepository) Rename(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.Rename(ctx, workUnitName)
}
func (repo *tracingRepository) Exists(ctx context.Context, workUnitName string) (bool, error) {
defer repo.startRegions(ctx)()
return repo.repo.Exists(ctx, workUnitName)
}
func (repo *tracingRepository) Update(ctx context.Context, workUnitName string) error {
defer repo.startRegions(ctx)()
return repo.repo.Update(ctx, workUnitName)
}
Loading