Skip to content

Commit

Permalink
util: move determineSourceRemote to util file
Browse files Browse the repository at this point in the history
determineSourceRemote() function is being used in more then one place and isn't
specific to mr_create, where it was initialy declared. This patch moves it to
util.go file.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Jan 25, 2021
1 parent ea0c681 commit 7183bba
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 92 deletions.
21 changes: 0 additions & 21 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitconfig "github.com/tcnksm/go-gitconfig"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
Expand Down Expand Up @@ -266,26 +265,6 @@ func runMRCreate(cmd *cobra.Command, args []string) {
fmt.Println(mrURL + "/diffs")
}

func determineSourceRemote(branch string) string {
// There is a precendence of options that should be considered here:
// branch.<name>.pushRemote > remote.pushDefault > branch.<name>.remote
// This rule is placed in git-config(1) manpage
r, err := gitconfig.Local("branch." + branch + ".pushRemote")
if err == nil {
return r
}
r, err = gitconfig.Local("remote.pushDefault")
if err == nil {
return r
}
r, err = gitconfig.Local("branch." + branch + ".remote")
if err == nil {
return r
}

return forkRemote
}

func mrText(base, head, sourceRemote, targetRemote string, coverLetterFormat bool) (string, error) {
var (
commitMsg string
Expand Down
71 changes: 0 additions & 71 deletions cmd/mr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/acarl005/stripansi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -508,72 +506,3 @@ func Test_mrCmd_noArgs(t *testing.T) {
lab mr [flags]
lab mr [command]`)
}

func Test_determineSourceRemote(t *testing.T) {
tests := []struct {
desc string
branch string
expected string
}{
{
desc: "branch.<name>.remote",
branch: "mrtest",
expected: "lab-testing",
},
{
desc: "branch.<name>.pushRemote",
branch: "mrtest-pushRemote",
expected: "lab-testing",
},
{
desc: "pushDefault without pushRemote set",
branch: "mrtest",
expected: "garbageurl",
},
{
desc: "pushDefault with pushRemote set",
branch: "mrtest-pushRemote",
expected: "lab-testing",
},
}

// The function being tested here depends on being in the test
// directory, where 'git config --local' can retrieve the correct
// info from
repo := copyTestRepo(t)
oldWd, err := os.Getwd()
if err != nil {
t.Log(err)
}
os.Chdir(repo)

var remoteModified bool
for _, test := range tests {
test := test
if strings.Contains(test.desc, "pushDefault") && !remoteModified {
git := exec.Command("git", "config", "--local", "remote.pushDefault", "garbageurl")
git.Dir = repo
b, err := git.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
remoteModified = true
}

t.Run(test.desc, func(t *testing.T) {
sourceRemote := determineSourceRemote(test.branch)
assert.Equal(t, test.expected, sourceRemote)
})
}
// Remove the added option to avoid messing with other tests
git := exec.Command("git", "config", "--local", "--unset", "remote.pushDefault")
git.Dir = repo
b, err := git.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
// And move back to the workdir we were before the test
os.Chdir(oldWd)
}
21 changes: 21 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
gitconfig "github.com/tcnksm/go-gitconfig"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/config"
git "github.com/zaquestion/lab/internal/git"
Expand Down Expand Up @@ -352,3 +353,23 @@ func labURLToRepo(project *gitlab.Project) string {
}
return urlToRepo
}

func determineSourceRemote(branch string) string {
// There is a precendence of options that should be considered here:
// branch.<name>.pushRemote > remote.pushDefault > branch.<name>.remote
// This rule is placed in git-config(1) manpage
r, err := gitconfig.Local("branch." + branch + ".pushRemote")
if err == nil {
return r
}
r, err = gitconfig.Local("remote.pushDefault")
if err == nil {
return r
}
r, err = gitconfig.Local("branch." + branch + ".remote")
if err == nil {
return r
}

return forkRemote
}
70 changes: 70 additions & 0 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -290,3 +291,72 @@ func Test_labURLToRepo(t *testing.T) {
urlToRepo = labURLToRepo(&project)
assert.Equal(t, urlToRepo, HTTPURL)
}

func Test_determineSourceRemote(t *testing.T) {
tests := []struct {
desc string
branch string
expected string
}{
{
desc: "branch.<name>.remote",
branch: "mrtest",
expected: "lab-testing",
},
{
desc: "branch.<name>.pushRemote",
branch: "mrtest-pushRemote",
expected: "lab-testing",
},
{
desc: "pushDefault without pushRemote set",
branch: "mrtest",
expected: "garbageurl",
},
{
desc: "pushDefault with pushRemote set",
branch: "mrtest-pushRemote",
expected: "lab-testing",
},
}

// The function being tested here depends on being in the test
// directory, where 'git config --local' can retrieve the correct
// info from
repo := copyTestRepo(t)
oldWd, err := os.Getwd()
if err != nil {
t.Log(err)
}
os.Chdir(repo)

var remoteModified bool
for _, test := range tests {
test := test
if strings.Contains(test.desc, "pushDefault") && !remoteModified {
git := exec.Command("git", "config", "--local", "remote.pushDefault", "garbageurl")
git.Dir = repo
b, err := git.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
remoteModified = true
}

t.Run(test.desc, func(t *testing.T) {
sourceRemote := determineSourceRemote(test.branch)
assert.Equal(t, test.expected, sourceRemote)
})
}
// Remove the added option to avoid messing with other tests
git := exec.Command("git", "config", "--local", "--unset", "remote.pushDefault")
git.Dir = repo
b, err := git.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
// And move back to the workdir we were before the test
os.Chdir(oldWd)
}

0 comments on commit 7183bba

Please sign in to comment.