Skip to content

Commit

Permalink
Revert "chore: coverage++"
Browse files Browse the repository at this point in the history
Convert test to use FailFS instead.

This reverts commit a4d9e44.
  • Loading branch information
nisimond authored and retr0h committed Mar 12, 2024
1 parent 52f0dda commit 71cff17
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
13 changes: 1 addition & 12 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ import (
"github.com/retr0h/gilt/v2/internal"
)

// AbsFn function to switch when testing.
// The only time `Abs` will return an error is when `os.Getwd()` returns an
// error. Given this situation is rare, but "it's a weird, wild world out there"
// opted to retain he error handling, but clumsily swap the test function for
// coverage++.
var AbsFn = (*Git).abs

// New factory to create a new Git instance.
func New(
appFs avfs.VFS,
Expand Down Expand Up @@ -76,7 +69,7 @@ func (g *Git) Worktree(
version string,
dstDir string,
) error {
dst, err := AbsFn(g, dstDir)
dst, err := g.appFs.Abs(dstDir)
if err != nil {
return err
}
Expand All @@ -101,7 +94,3 @@ func (g *Git) Worktree(
}
return err
}

func (g *Git) abs(path string) (string, error) {
return g.appFs.Abs(path)
}
23 changes: 15 additions & 8 deletions internal/git/git_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ package git_test

import (
"errors"
"fmt"
"log/slog"
"os"
"testing"

"github.com/avfs/avfs"
"github.com/avfs/avfs/vfs/memfs"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand All @@ -35,13 +35,15 @@ import (
"github.com/retr0h/gilt/v2/internal"
"github.com/retr0h/gilt/v2/internal/git"
"github.com/retr0h/gilt/v2/internal/mocks/exec"
failfs "github.com/retr0h/gilt/v2/internal/mocks/vfs"
)

type GitManagerPublicTestSuite struct {
suite.Suite

ctrl *gomock.Controller
mockExec *exec.MockExecManager
appFs avfs.VFS

gitURL string
gitVersion string
Expand All @@ -53,7 +55,7 @@ type GitManagerPublicTestSuite struct {

func (suite *GitManagerPublicTestSuite) NewTestGitManager() internal.GitManager {
return git.New(
memfs.New(),
suite.appFs,
suite.mockExec,
slog.New(slog.NewTextHandler(os.Stdout, nil)),
)
Expand All @@ -62,6 +64,7 @@ func (suite *GitManagerPublicTestSuite) NewTestGitManager() internal.GitManager
func (suite *GitManagerPublicTestSuite) SetupTest() {
suite.ctrl = gomock.NewController(suite.T())
suite.mockExec = exec.NewMockExecManager(suite.ctrl)
suite.appFs = memfs.New()

suite.gitURL = "https://example.com/user/repo.git"
suite.gitVersion = "abc123"
Expand Down Expand Up @@ -113,14 +116,18 @@ func (suite *GitManagerPublicTestSuite) TestWorktreeError() {
}

func (suite *GitManagerPublicTestSuite) TestWorktreeErrorWhenAbsErrors() {
originalAbsFn := git.AbsFn
git.AbsFn = func(g *git.Git, _ string) (string, error) {
return "", fmt.Errorf("failed to get abs path")
}
defer func() { git.AbsFn = originalAbsFn }()
// Make Abs() calls fail
suite.appFs = failfs.New(
suite.appFs,
map[string]interface{}{
"Abs": func(string) (string, error) { return "", errors.New("FailFS!") },
},
)
gm := suite.NewTestGitManager()

err := suite.gm.Worktree(suite.cloneDir, suite.gitVersion, suite.dstDir)
err := gm.Worktree(suite.cloneDir, suite.gitVersion, suite.dstDir)
assert.Error(suite.T(), err)
assert.Equal(suite.T(), "FailFS!", err.Error())
}

func (suite *GitManagerPublicTestSuite) TestUpdateOk() {
Expand Down
10 changes: 10 additions & 0 deletions internal/mocks/vfs/failfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ func (vfs *FailFS) SystemDirs(basePath string) []avfs.DirInfo {
}

func (vfs *FailFS) Abs(path string) (string, error) {
if failFn, ok := vfs.failFn["Abs"]; ok {
results := vfs.callFailFn(failFn, path)
var abs string
var err error
abs, _ = results[0].Interface().(string)
if !results[1].IsNil() {
err, _ = results[1].Interface().(error)
}
return abs, err
}
return vfs.baseFS.Abs(path)
}

Expand Down

0 comments on commit 71cff17

Please sign in to comment.