-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the status of the Git worktree for the repository. This is necessary for determining the state of all the files. Testing required an alternative approach as stubbing the status method was difficult. This prevents some error branches from being tested.
- Loading branch information
1 parent
5b71585
commit 9251ba2
Showing
5 changed files
with
176 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package repository | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-git/go-git/v5" | ||
) | ||
|
||
type Worktree struct { | ||
Status git.Status | ||
} | ||
|
||
func (r *Repository) Worktree() (Worktree, error) { | ||
var wt Worktree | ||
|
||
w, err := r.Worktreer.Worktree() | ||
if err != nil { | ||
return Worktree{}, fmt.Errorf("unable to get worktree: %w", err) | ||
} | ||
|
||
s, err := w.Status() | ||
if err != nil { | ||
return Worktree{}, fmt.Errorf("unable to get status of worktree: %w", err) | ||
} | ||
wt.Status = s | ||
|
||
return wt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package repository_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/go-git/go-billy/v5/memfs" | ||
fixtures "github.com/go-git/go-git-fixtures/v4" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing/cache" | ||
"github.com/go-git/go-git/v5/storage/filesystem" | ||
"github.com/mikelorant/committed/internal/repository" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockRepositoryWorktree struct { | ||
fixture *fixtures.Fixture | ||
err error | ||
} | ||
|
||
func (m MockRepositoryWorktree) Worktree() (*git.Worktree, error) { | ||
dotgit := m.fixture.DotGit() | ||
st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault()) | ||
wt := memfs.New() | ||
|
||
repo, err := git.Open(st, wt) | ||
if err != nil { | ||
return &git.Worktree{}, err | ||
} | ||
|
||
if m.err != nil { | ||
return &git.Worktree{}, errMockWorktree | ||
} | ||
|
||
return repo.Worktree() | ||
} | ||
|
||
var errMockWorktree = errors.New("error") | ||
|
||
func TestWorktree(t *testing.T) { | ||
type args struct { | ||
fixture *fixtures.Fixture | ||
err error | ||
} | ||
|
||
type want struct { | ||
count int | ||
err error | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want want | ||
}{ | ||
{ | ||
name: "dirty", | ||
args: args{ | ||
fixture: fixtures.Basic().One(), | ||
}, | ||
want: want{ | ||
count: 9, | ||
}, | ||
}, | ||
{ | ||
name: "empty", | ||
args: args{ | ||
fixture: fixtures.ByTag("empty")[0], | ||
}, | ||
}, | ||
{ | ||
name: "error", | ||
args: args{ | ||
fixture: fixtures.Basic().One(), | ||
err: errMockWorktree, | ||
}, | ||
want: want{ | ||
err: errMockWorktree, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var r repository.Repository | ||
|
||
r.Worktreer = MockRepositoryWorktree{ | ||
fixture: tt.args.fixture, | ||
err: tt.args.err, | ||
} | ||
|
||
wt, err := r.Worktree() | ||
if tt.want.err != nil { | ||
assert.Error(t, err) | ||
assert.ErrorIs(t, err, errMockWorktree) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.Len(t, wt.Status, tt.want.count) | ||
}) | ||
} | ||
} |