Skip to content

Commit

Permalink
Moved path package to public test pattern (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Nov 24, 2023
1 parent 82f0618 commit 9fa6ad4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
5 changes: 3 additions & 2 deletions internal/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
"path/filepath"
)

var currentUser = user.Current
// CurrentUser function to switch when testing
var CurrentUser = user.Current

// ExpandUser on Unix and Windows, return the argument with an initial
// component of ~ or ~user replaced by that user’s home directory.
Expand All @@ -36,7 +37,7 @@ func ExpandUser(
return path, nil
}

usr, err := currentUser()
usr, err := CurrentUser()
if err != nil {
return "", err
}
Expand Down
31 changes: 16 additions & 15 deletions internal/path/path_test.go → internal/path/path_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// TODO(retr0h): move to public testing pattern
package path
package path_test

import (
"fmt"
Expand All @@ -28,39 +27,41 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/retr0h/go-gilt/internal/path"
)

type PathTestSuite struct {
type PathPublicTestSuite struct {
suite.Suite
}

func (suite *PathTestSuite) TestexpandUserOk() {
originalCurrentUser := currentUser
currentUser = func() (*user.User, error) {
func (suite *PathPublicTestSuite) TestexpandUserOk() {
originalCurrentUser := path.CurrentUser
path.CurrentUser = func() (*user.User, error) {
return &user.User{
HomeDir: "/testUser",
}, nil
}
defer func() { currentUser = originalCurrentUser }()
defer func() { path.CurrentUser = originalCurrentUser }()

got, err := ExpandUser("~/foo/bar")
got, err := path.ExpandUser("~/foo/bar")
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), got, "/testUser/foo/bar")
}

func (suite *PathTestSuite) TestexpandUserReturnsError() {
originalCurrentUser := currentUser
currentUser = func() (*user.User, error) {
func (suite *PathPublicTestSuite) TestexpandUserReturnsError() {
originalCurrentUser := path.CurrentUser
path.CurrentUser = func() (*user.User, error) {
return nil, fmt.Errorf("failed to get current user")
}
defer func() { currentUser = originalCurrentUser }()
defer func() { path.CurrentUser = originalCurrentUser }()

_, err := ExpandUser("~/foo/bar")
_, err := path.ExpandUser("~/foo/bar")
assert.Error(suite.T(), err)
}

// In order for `go test` to run this suite, we need to create
// a normal test function and pass our suite to suite.Run.
func TestPathTestSuite(t *testing.T) {
suite.Run(t, new(PathTestSuite))
func TestPathPublicTestSuite(t *testing.T) {
suite.Run(t, new(PathPublicTestSuite))
}

0 comments on commit 9fa6ad4

Please sign in to comment.