Skip to content

Commit

Permalink
Removed util package (#29)
Browse files Browse the repository at this point in the history
Util packages are gross.

* Moved relevant code into an ioutil package
* Moved other code from util into respective packages
* Using afero for integration testing of file/dir copy
  • Loading branch information
retr0h committed Nov 18, 2023
1 parent c5be742 commit a6bff51
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 334 deletions.
17 changes: 15 additions & 2 deletions cmd/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@ package cmd
import (
"log/slog"
"os"
"os/user"
"path/filepath"

"github.com/retr0h/go-gilt/internal/util"
"github.com/spf13/cobra"
)

func expandUser(path string) (string, error) {
if len(path) == 0 || path[0] != '~' {
return path, nil
}

usr, err := user.Current()
if err != nil {
return "", err
}

return filepath.Join(usr.HomeDir, path[1:]), nil
}

// getGiltDir create the GiltDir if it doesn't exist.
func getGiltDir() (string, error) {
expandedGiltDir, err := util.ExpandUser(r.GiltDir)
expandedGiltDir, err := expandUser(r.GiltDir)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d
github.com/lmittmann/tint v1.0.3
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/spf13/afero v1.10.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
Expand All @@ -25,7 +26,6 @@ require (
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
Expand Down
48 changes: 36 additions & 12 deletions internal/git/git.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) 2018 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
Expand All @@ -16,35 +18,36 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// Package git package needs reworked into proper Git libraries. However, this
// package will remain using exec as it was easiest to port from go-gilt's
// python counterpart.
package git

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/logrusorgru/aurora/v4"

"github.com/retr0h/go-gilt/internal/repository"
"github.com/retr0h/go-gilt/internal/util"
)

var (
// RunCommand is mocked for tests.
RunCommand = util.RunCmd
RunCommand = runCmd
// FilePathAbs is mocked for tests.
FilePathAbs = filepath.Abs
)

// Git struct for adding methods.
type Git struct {
Debug bool // Debug option set from CLI with debug state.
}

// NewGit factory to create a new Git instance.
func NewGit(debug bool) *Git {
return &Git{
Debug: debug,
debug: debug,
}
}

Expand Down Expand Up @@ -82,14 +85,14 @@ func (g *Git) Clone(repository repository.Repository) error {

func (g *Git) clone(repository repository.Repository) error {
cloneDir := repository.GetCloneDir()
err := RunCommand(g.Debug, "git", "clone", repository.Git, cloneDir)
err := RunCommand(g.debug, "git", "clone", repository.Git, cloneDir)

return err
}

func (g *Git) reset(repository repository.Repository) error {
cloneDir := repository.GetCloneDir()
err := RunCommand(g.Debug, "git", "-C", cloneDir, "reset", "--hard", repository.Version)
err := RunCommand(g.debug, "git", "-C", cloneDir, "reset", "--hard", repository.Version)

return err
}
Expand All @@ -115,8 +118,29 @@ func (g *Git) CheckoutIndex(repository repository.Repository) error {
// Trailing separator needed by git checkout-index.
dstDir + string(os.PathSeparator),
}
if err := RunCommand(g.Debug, "git", cmdArgs...); err != nil {
return err

return RunCommand(g.debug, "git", cmdArgs...)
}

// runCmd execute the provided command with args.
// Yeah, yeah, yeah, I know I cheated by using Exec in this package.
func runCmd(debug bool, name string, args ...string) error {
var stderr bytes.Buffer
cmd := exec.Command(name, args...)

if debug {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

commands := strings.Join(cmd.Args, " ")
msg := fmt.Sprintf("COMMAND: %s", aurora.Colorize(commands, aurora.BlackFg|aurora.RedBg))
fmt.Println(msg)
} else {
cmd.Stderr = &stderr
}

if err := cmd.Run(); err != nil {
return errors.New(stderr.String())
}

return nil
Expand Down
27 changes: 27 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"testing"

capturer "github.com/kami-zh/go-capturer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -109,6 +110,32 @@ func (suite *GitTestSuite) TestReset() {
assert.Equal(suite.T(), want, got)
}

func TestRunCommandReturnsError(t *testing.T) {
err := runCmd(false, "false")

assert.Error(t, err)
}

func TestRunCommandPrintsStreamingStdout(t *testing.T) {
got := capturer.CaptureStdout(func() {
err := runCmd(true, "echo", "-n", "foo")
assert.NoError(t, err)
})
want := "COMMAND: \x1b[30;41mecho -n foo\x1b[0m\nfoo"

assert.Equal(t, want, got)
}

func TestRunCommandPrintsStreamingStderr(t *testing.T) {
got := capturer.CaptureStderr(func() {
err := runCmd(true, "cat", "foo")
assert.Error(t, err)
})
want := "cat: foo: No such file or directory\n"

assert.Equal(t, want, got)
}

// 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 TestGitTestSuite(t *testing.T) {
Expand Down
24 changes: 5 additions & 19 deletions internal/util/util_test.go → internal/git/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018 John Dewey
// Copyright (c) 2023 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
Expand All @@ -18,23 +18,9 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package util
package git

import (
"errors"
"os/user"
"testing"

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

func TestExpandUserReturnsError(t *testing.T) {
originalCurrentUser := currentUser
currentUser = func() (*user.User, error) {
return nil, errors.New("Failed user.Current")
}
defer func() { currentUser = originalCurrentUser }()

_, err := ExpandUser("~/foo/bar")
assert.Error(t, err)
// Git perform Git operations.
type Git struct {
debug bool
}
Loading

0 comments on commit a6bff51

Please sign in to comment.