Skip to content

Commit

Permalink
[#200] Simplify git function names now that they are in a separate pa…
Browse files Browse the repository at this point in the history
…ckage
  • Loading branch information
mengdaming committed Jan 3, 2023
1 parent ab68a11 commit 52fe73a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/checker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
func initTestCheckEnv(params params.Params) {
initCheckEnv(params)
// We replace git implementation with a fake so that we bypass real git access
checkEnv.git, checkEnv.gitErr = git.NewGitFake(git.GitFakeSettings{})
checkEnv.git, checkEnv.gitErr = git.NewFake(git.FakeSettings{})
}

func assertStatus(t *testing.T, expected CheckStatus, checker func(params params.Params) (cr *CheckResults), params params.Params) {
Expand Down
44 changes: 22 additions & 22 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ func Test_tcr_operation_end_state(t *testing.T) {
{
"commit with git commit failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.CommitCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.Commands{git.CommitCommand}, nil)
tcr.commit(events.TcrEvent{})
},
status.GitError,
},
{
"commit with git push failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.PushCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.Commands{git.PushCommand}, nil)
tcr.commit(events.TcrEvent{})
},
status.GitError,
Expand All @@ -205,15 +205,15 @@ func Test_tcr_operation_end_state(t *testing.T) {
{
"revert with git diff failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.DiffCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.Commands{git.DiffCommand}, nil)
tcr.revert(events.TcrEvent{})
},
status.GitError,
},
{
"revert with git restore failure",
func() {
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.RestoreCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.Commands{git.RestoreCommand}, nil)
tcr.revert(events.TcrEvent{})
},
status.GitError,
Expand All @@ -232,7 +232,7 @@ func Test_tcr_operation_end_state(t *testing.T) {
func Test_tcr_revert_end_state_with_commit_on_fail_enabled(t *testing.T) {
testFlags := []struct {
desc string
gitFailures git.GitCommands
gitFailures git.Commands
expectedStatus status.Status
}{
{
Expand All @@ -242,27 +242,27 @@ func Test_tcr_revert_end_state_with_commit_on_fail_enabled(t *testing.T) {
},
{
"git stash failure",
git.GitCommands{git.StashCommand},
git.Commands{git.StashCommand},
status.GitError,
},
{
"git un-stash failure",
git.GitCommands{git.UnStashCommand},
git.Commands{git.UnStashCommand},
status.GitError,
},
{
"git add failure",
git.GitCommands{git.AddCommand},
git.Commands{git.AddCommand},
status.GitError,
},
{
"git commit failure",
git.GitCommands{git.CommitCommand},
git.Commands{git.CommitCommand},
status.GitError,
},
{
"git revert failure",
git.GitCommands{git.RevertCommand},
git.Commands{git.RevertCommand},
status.GitError,
},
}
Expand All @@ -282,7 +282,7 @@ func Test_tcr_cycle_end_state(t *testing.T) {
testFlags := []struct {
desc string
toolchainFailures toolchain.Operations
gitFailures git.GitCommands
gitFailures git.Commands
expectedStatus status.Status
}{
{
Expand All @@ -302,27 +302,27 @@ func Test_tcr_cycle_end_state(t *testing.T) {
},
{
"with git add failure",
nil, git.GitCommands{git.AddCommand},
nil, git.Commands{git.AddCommand},
status.GitError,
},
{
"with git commit failure",
nil, git.GitCommands{git.CommitCommand},
nil, git.Commands{git.CommitCommand},
status.GitError,
},
{
"with git push failure",
nil, git.GitCommands{git.PushCommand},
nil, git.Commands{git.PushCommand},
status.GitError,
},
{
"with test and git diff failure",
toolchain.Operations{toolchain.TestOperation}, git.GitCommands{git.DiffCommand},
toolchain.Operations{toolchain.TestOperation}, git.Commands{git.DiffCommand},
status.GitError,
},
{
"with test and git restore failure",
toolchain.Operations{toolchain.TestOperation}, git.GitCommands{git.RestoreCommand},
toolchain.Operations{toolchain.TestOperation}, git.Commands{git.RestoreCommand},
status.GitError,
},
}
Expand All @@ -337,7 +337,7 @@ func Test_tcr_cycle_end_state(t *testing.T) {
}
}

func initTcrEngineWithFakes(p *params.Params, toolchainFailures toolchain.Operations, gitFailures git.GitCommands, gitLogItems vcs.GitLogItems) (TcrInterface, *git.GitFake) {
func initTcrEngineWithFakes(p *params.Params, toolchainFailures toolchain.Operations, gitFailures git.Commands, gitLogItems vcs.GitLogItems) (TcrInterface, *git.Fake) {
tchn := registerFakeToolchain(toolchainFailures)
lang := registerFakeLanguage(tchn)

Expand Down Expand Up @@ -385,13 +385,13 @@ func registerFakeLanguage(toolchainName string) string {
return fake.GetName()
}

func replaceGitImplWithFake(tcr TcrInterface, failures git.GitCommands, gitLogItems vcs.GitLogItems) *git.GitFake {
fakeSettings := git.GitFakeSettings{
func replaceGitImplWithFake(tcr TcrInterface, failures git.Commands, gitLogItems vcs.GitLogItems) *git.Fake {
fakeSettings := git.FakeSettings{
FailingCommands: failures,
ChangedFiles: vcs.FileDiffs{vcs.NewFileDiff("fake-src", 1, 1)},
Logs: gitLogItems,
}
fake, _ := git.NewGitFake(fakeSettings)
fake, _ := git.NewFake(fakeSettings)
tcr.setVcs(fake)
return fake
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func Test_git_pull_highlights_errors(t *testing.T) {
return msg.Type.Severity == report.Error && msg.Text == "git pull command failed!"
},
)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.PullCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.Commands{git.PullCommand}, nil)
tcr.GitPull()
time.Sleep(1 * time.Millisecond)
sniffer.Stop()
Expand All @@ -476,7 +476,7 @@ func Test_git_push_highlights_errors(t *testing.T) {
return msg.Type.Severity == report.Error && msg.Text == "git push command failed!"
},
)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.GitCommands{git.PushCommand}, nil)
tcr, _ := initTcrEngineWithFakes(nil, nil, git.Commands{git.PushCommand}, nil)
tcr.GitPush()
time.Sleep(1 * time.Millisecond)
sniffer.Stop()
Expand Down
28 changes: 13 additions & 15 deletions src/vcs/git/git_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,27 @@ package git
import (
"bufio"
"bytes"
"github.com/codeskyblue/go-sh"
"github.com/murex/tcr/report"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/cmd"
"strings"
)

func newGitCommand() *cmd.ShellCommand {
return cmd.New("git")
}

// IsGitCommandAvailable indicates if git command is available on local machine
func IsGitCommandAvailable() bool {
return vcs.IsCommandAvailable("git")
return newGitCommand().IsInPath()
}

// GetGitCommandPath returns the path to git command on this machine
func GetGitCommandPath() string {
return vcs.GetCommandPath("git")
return newGitCommand().GetFullPath()
}

// GetGitCommandVersion returns the version of git command on this machine
func GetGitCommandVersion() string {
gitOutput, err := runGitCommand([]string{"version"})
gitOutput, err := runGitCommand("version")
if err != nil {
return "unknown"
}
Expand All @@ -58,7 +60,7 @@ func GetGitUserName() string {
}

func getGitConfigValue(variable string) string {
gitOutput, err := runGitCommand([]string{"config", variable})
gitOutput, err := runGitCommand("config", variable)
if err != nil || gitOutput == nil || len(gitOutput) == 0 {
return "not set"
}
Expand All @@ -68,15 +70,11 @@ func getGitConfigValue(variable string) string {
}

// traceGitCommand calls git command and reports its output traces
func traceGitCommand(params []string) error {
output, err := runGitCommand(params)
if len(output) > 0 {
report.PostText(string(output))
}
return err
func traceGitCommand(params ...string) error {
return newGitCommand().Trace(params...)
}

// runGitCommand calls git command in a separate process and returns its output traces
func runGitCommand(params []string) (output []byte, err error) {
return sh.Command("git", params).CombinedOutput()
func runGitCommand(params ...string) (output []byte, err error) {
return newGitCommand().Run(params...)
}
8 changes: 4 additions & 4 deletions src/vcs/git/git_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type gitImpl struct {
workingBranch string
workingBranchExistsOnRemote bool
pushEnabled bool
runGitFunction func(params []string) (output []byte, err error)
traceGitFunction func(params []string) (err error)
runGitFunction func(params ...string) (output []byte, err error)
traceGitFunction func(params ...string) (err error)
}

// New initializes the git implementation based on the provided directory from local clone
Expand Down Expand Up @@ -372,11 +372,11 @@ func (g *gitImpl) CheckRemoteAccess() bool {
// traceGit runs a git command and traces its output.
// The command is launched from the git root directory
func (g *gitImpl) traceGit(args ...string) error {
return g.traceGitFunction(append([]string{"-C", g.GetRootDir()}, args...))
return g.traceGitFunction(append([]string{"-C", g.GetRootDir()}, args...)...)
}

// runGit calls git command in a separate process and returns its output traces
// The command is launched from the git root directory
func (g *gitImpl) runGit(args ...string) (output []byte, err error) {
return g.runGitFunction(append([]string{"-C", g.GetRootDir()}, args...))
return g.runGitFunction(append([]string{"-C", g.GetRootDir()}, args...)...)
}
32 changes: 16 additions & 16 deletions src/vcs/git/git_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func Test_git_diff_command(t *testing.T) {
false,
nil,
vcs.FileDiffs{
{filepath.Join("/", "some-file.txt"), 1, 1},
{Path: filepath.Join("/", "some-file.txt"), AddedLines: 1, RemovedLines: 1},
},
},
{"2 files changed",
Expand All @@ -128,8 +128,8 @@ func Test_git_diff_command(t *testing.T) {
false,
nil,
vcs.FileDiffs{
{filepath.Join("/", "file1.txt"), 1, 1},
{filepath.Join("/", "file2.txt"), 1, 1},
{Path: filepath.Join("/", "file1.txt"), AddedLines: 1, RemovedLines: 1},
{Path: filepath.Join("/", "file2.txt"), AddedLines: 1, RemovedLines: 1},
},
},
{"file changed in sub-directory",
Expand All @@ -138,7 +138,7 @@ func Test_git_diff_command(t *testing.T) {
false,
nil,
vcs.FileDiffs{
{filepath.Join("/", "some-dir", "some-file.txt"), 1, 1},
{Path: filepath.Join("/", "some-dir", "some-file.txt"), AddedLines: 1, RemovedLines: 1},
},
},
{"1 file changed with added lines only",
Expand All @@ -147,7 +147,7 @@ func Test_git_diff_command(t *testing.T) {
false,
nil,
vcs.FileDiffs{
{filepath.Join("/", "some-file.txt"), 15, 0},
{Path: filepath.Join("/", "some-file.txt"), AddedLines: 15, RemovedLines: 0},
},
},
{"1 file changed with removed lines only",
Expand All @@ -156,7 +156,7 @@ func Test_git_diff_command(t *testing.T) {
false,
nil,
vcs.FileDiffs{
{filepath.Join("/", "some-file.txt"), 0, 7},
{Path: filepath.Join("/", "some-file.txt"), AddedLines: 0, RemovedLines: 7},
},
},
{"noise in output trace",
Expand All @@ -167,15 +167,15 @@ func Test_git_diff_command(t *testing.T) {
false,
nil,
vcs.FileDiffs{
{filepath.Join("/", "some-file.txt"), 1, 1},
{Path: filepath.Join("/", "some-file.txt"), AddedLines: 1, RemovedLines: 1},
},
},
}
for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
g, _ := newGitImpl(inMemoryRepoInit, "")
g.runGitFunction = func(args []string) (output []byte, err error) {
g.runGitFunction = func(args ...string) (output []byte, err error) {
actualArgs = args[2:]
return []byte(tt.gitDiffOutput), tt.gitDiffError
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func Test_git_push_command(t *testing.T) {
for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(_ []string) (err error) {
g.traceGitFunction = func(_ ...string) (err error) {
return tt.gitError
}
g.pushEnabled = tt.pushEnabled
Expand Down Expand Up @@ -286,7 +286,7 @@ func Test_git_pull_command(t *testing.T) {
for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(_ []string) (err error) {
g.traceGitFunction = func(_ ...string) (err error) {
return tt.gitError
}
g.remoteEnabled = true
Expand Down Expand Up @@ -344,7 +344,7 @@ func Test_git_add_command(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(args []string) (err error) {
g.traceGitFunction = func(args ...string) (err error) {
actualArgs = args[2:]
return tt.gitError
}
Expand Down Expand Up @@ -409,7 +409,7 @@ func Test_git_commit_command(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(args []string) error {
g.traceGitFunction = func(args ...string) error {
actualArgs = args[2:]
return tt.gitError
}
Expand Down Expand Up @@ -444,7 +444,7 @@ func Test_git_restore_command(t *testing.T) {
for _, tt := range testFlags {
t.Run(tt.desc, func(t *testing.T) {
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(_ []string) (err error) {
g.traceGitFunction = func(_ ...string) (err error) {
return tt.gitError
}

Expand Down Expand Up @@ -482,7 +482,7 @@ func Test_git_revert_command(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(args []string) (err error) {
g.traceGitFunction = func(args ...string) (err error) {
actualArgs = args[2:]
return tt.gitError
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func Test_git_stash_command(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(args []string) (err error) {
g.traceGitFunction = func(args ...string) (err error) {
actualArgs = args[2:]
return tt.gitError
}
Expand Down Expand Up @@ -582,7 +582,7 @@ func Test_git_unstash_command(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
var actualArgs []string
g, _ := newGitImpl(inMemoryRepoInit, "")
g.traceGitFunction = func(args []string) (err error) {
g.traceGitFunction = func(args ...string) (err error) {
actualArgs = args[2:]
return tt.gitError
}
Expand Down
Loading

0 comments on commit 52fe73a

Please sign in to comment.