Skip to content

Commit

Permalink
mr_test: Generalize prepare and close test functions
Browse files Browse the repository at this point in the history
This issue was noticed while implementing the --source option in 'mr
create'.

The prepare function in the merge request test code has an error that is
commented out.  This is because the 'mr -d' option is no longer valid and
always throws an error.  The prepare function attempts to clean up any
existing test merge requests prior to creating new ones.  It would be
best to clean this up, avoid duplication in the code, and generalize
the call to a single function.

In addition to that I noticed that the delete function is repetitive
throughout the test code.  The delete functions should be renamed to close,
and it also can be generalized.

Fix and generalize the prepare and close test functions.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Mar 22, 2021
1 parent 63c2db6 commit 375f452
Showing 1 changed file with 61 additions and 147 deletions.
208 changes: 61 additions & 147 deletions cmd/mr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,58 @@ import (
"io/ioutil"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"

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

func closeMR(t *testing.T, targetRepo string, cmdDir string, mrID string) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "close", targetRepo, mrID)
cmd.Dir = cmdDir

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
}

func cleanupMR(t *testing.T, targetRepo string, cmdDir string, MRtitle string) {
openMRcmd := exec.Command("sh", "-c", fmt.Sprintf("%s mr list %s | grep -m1 \"%s\" | cut -c2- | awk '{print $1}'", labBinaryPath, targetRepo, MRtitle))
openMRcmd.Dir = cmdDir
openMRstr, err := openMRcmd.CombinedOutput()
if err != nil {
t.Log(string(openMRstr))
t.Fatal(err)
}

openMR, err := strconv.Atoi(strings.TrimSpace(string(openMRstr)))
if err != nil {
t.Log(string(openMRstr))
return
}

if openMR <= 0 {
// no open MRs
return
}

// close the existing MR
closeMR(t, targetRepo, cmdDir, string(openMRstr))
}

func Test_mrCmd(t *testing.T) {
repo := copyTestRepo(t)
var mrID string
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list lab-testing | grep -m1 'mr title' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr lab-testing -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "lab-testing", repo, "mr title")
})
t.Run("create", func(t *testing.T) {
git := exec.Command("git", "checkout", "mrtest")
Expand Down Expand Up @@ -74,19 +107,8 @@ func Test_mrCmd(t *testing.T) {
require.Contains(t, outStripped, "mr description")
require.Contains(t, out, fmt.Sprintf("WebURL: https://gitlab.com/lab-testing/test/-/merge_requests/%s", mrID))
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "lab-testing", "-d", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "lab-testing", repo, mrID)
})
}

Expand All @@ -97,14 +119,7 @@ func Test_mrCmd_MR_description_and_options(t *testing.T) {
commentID string
)
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list lab-testing | grep -m1 'Fancy Description' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr lab-testing -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "lab-testing", repo, "Fancy Description")
})
t.Run("create MR from file", func(t *testing.T) {
git := exec.Command("git", "checkout", "mrtest")
Expand Down Expand Up @@ -190,34 +205,16 @@ func Test_mrCmd_MR_description_and_options(t *testing.T) {
_commentID := "#" + commentID + ": lab-testing"
require.Contains(t, out, _commentID)
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create -F likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "lab-testing", "-d", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "lab-testing", repo, mrID)
})
}

func Test_mrCmd_DifferingUpstreamBranchName(t *testing.T) {
repo := copyTestRepo(t)
var mrID string
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list lab-testing | grep -m1 'mr title' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr lab-testing -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "lab-testing", repo, "mr title")
})
t.Run("create", func(t *testing.T) {
git := exec.Command("git", "checkout", "-b", "local/mrtest", "origin/mrtest")
Expand All @@ -244,34 +241,16 @@ func Test_mrCmd_DifferingUpstreamBranchName(t *testing.T) {
mrID = strings.TrimPrefix(out[:i], "https://gitlab.com/lab-testing/test/-/merge_requests/")
t.Log(mrID)
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "close", "lab-testing", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "lab-testing", repo, mrID)
})
}

func Test_mrCmd_Draft(t *testing.T) {
repo := copyTestRepo(t)
var mrID string
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list lab-testing | grep -m1 'Test draft' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr lab-testing -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "lab-testing", repo, "Test draft")
})
t.Run("create", func(t *testing.T) {
git := exec.Command("git", "checkout", "mrtest")
Expand Down Expand Up @@ -326,34 +305,16 @@ func Test_mrCmd_Draft(t *testing.T) {
t.Log(out)
require.NotContains(t, out, "Test draft merge request")
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "close", "lab-testing", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "lab-testing", repo, mrID)
})
}

func Test_mrCmd_Milestone(t *testing.T) {
repo := copyTestRepo(t)
var mrID string
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list origin | grep -m1 'Test draft' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr origin -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "origin", repo, "Test draft")
})
t.Run("create", func(t *testing.T) {
git := exec.Command("git", "checkout", "mrtest")
Expand Down Expand Up @@ -408,34 +369,16 @@ func Test_mrCmd_Milestone(t *testing.T) {
t.Log(out)
require.NotContains(t, out, "MR for 1.0")
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "close", "origin", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "origin", repo, mrID)
})
}

func Test_mrCmd_ByBranch(t *testing.T) {
repo := copyTestRepo(t)
var mrID string
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list lab-testing | grep -m1 'mr by branch' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr lab-testing -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "lab-testing", repo, "mr by branch")
})
t.Run("create", func(t *testing.T) {
git := exec.Command("git", "checkout", "mrtest")
Expand Down Expand Up @@ -476,34 +419,16 @@ func Test_mrCmd_ByBranch(t *testing.T) {
out := string(b)
require.Contains(t, out, fmt.Sprintf("WebURL: https://gitlab.com/lab-testing/test/-/merge_requests/%s", mrID))
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "close", "lab-testing", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "lab-testing", repo, mrID)
})
}

func Test_mrCmd_source(t *testing.T) {
repo := copyTestRepo(t)
var mrID string
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` mr list lab-testing | grep -m1 'mr title' | cut -c2- | awk '{print $1}' | xargs `+labBinaryPath+` mr lab-testing -d`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
cleanupMR(t, "lab-testing", repo, "mr title")
})
t.Run("create_invalid", func(t *testing.T) {
git := exec.Command("git", "checkout", "mrtest")
Expand Down Expand Up @@ -553,19 +478,8 @@ func Test_mrCmd_source(t *testing.T) {
mrID = strings.TrimPrefix(out[:i], "https://gitlab.com/lab-testing/test/-/merge_requests/")
t.Log(mrID)
})
t.Run("delete", func(t *testing.T) {
if mrID == "" {
t.Skip("mrID is empty, create likely failed")
}
cmd := exec.Command(labBinaryPath, "mr", "close", "lab-testing", mrID)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}
require.Contains(t, string(b), fmt.Sprintf("Merge Request !%s closed", mrID))
t.Run("close", func(t *testing.T) {
closeMR(t, "lab-testing", repo, mrID)
})
}

Expand Down

0 comments on commit 375f452

Please sign in to comment.