From 71cff17aae69ecce7d7eac021f95cfb790332f56 Mon Sep 17 00:00:00 2001 From: Nicolas Simonds Date: Mon, 11 Mar 2024 18:52:03 -0700 Subject: [PATCH] Revert "chore: coverage++" Convert test to use FailFS instead. This reverts commit a4d9e44c61c970d7b61612e4b9eb0865be91b473. --- internal/git/git.go | 13 +------------ internal/git/git_public_test.go | 23 +++++++++++++++-------- internal/mocks/vfs/failfs.go | 10 ++++++++++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index 1d08687..1bdb062 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -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, @@ -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 } @@ -101,7 +94,3 @@ func (g *Git) Worktree( } return err } - -func (g *Git) abs(path string) (string, error) { - return g.appFs.Abs(path) -} diff --git a/internal/git/git_public_test.go b/internal/git/git_public_test.go index 77a6583..05b67a1 100644 --- a/internal/git/git_public_test.go +++ b/internal/git/git_public_test.go @@ -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" @@ -35,6 +35,7 @@ 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 { @@ -42,6 +43,7 @@ type GitManagerPublicTestSuite struct { ctrl *gomock.Controller mockExec *exec.MockExecManager + appFs avfs.VFS gitURL string gitVersion string @@ -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)), ) @@ -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" @@ -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() { diff --git a/internal/mocks/vfs/failfs.go b/internal/mocks/vfs/failfs.go index f1fcc04..855cf6e 100644 --- a/internal/mocks/vfs/failfs.go +++ b/internal/mocks/vfs/failfs.go @@ -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) }