github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/git/step_git_merge_test.go (about)

     1  // +build unit
     2  
     3  package git
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
    14  
    15  	"github.com/jenkins-x/jx-logging/pkg/log"
    16  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    17  	"github.com/jenkins-x/jx/v2/pkg/gits/testhelpers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  func TestStepGitMerge(t *testing.T) {
    23  	RegisterFailHandler(Fail)
    24  	RunSpecs(t, "Step Git Merge Suite")
    25  }
    26  
    27  var _ = Describe("step git merge", func() {
    28  	var (
    29  		masterSha  string
    30  		branchBSha string
    31  		branchCSha string
    32  		repoDir    string
    33  		err        error
    34  		testReader *os.File
    35  		testWriter *os.File
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		By("capturing log output")
    40  		testReader, testWriter, _ = os.Pipe()
    41  		log.SetOutput(testWriter)
    42  		_ = log.SetLevel("info")
    43  	})
    44  
    45  	BeforeEach(func() {
    46  		repoDir, err = ioutil.TempDir("", "jenkins-x-git-test-repo-")
    47  		By(fmt.Sprintf("creating a test repository in '%s'", repoDir))
    48  		Expect(err).NotTo(HaveOccurred())
    49  		testhelpers.GitCmd(Fail, repoDir, "init")
    50  
    51  		By("adding single commit on master branch")
    52  		testhelpers.WriteFile(Fail, repoDir, "a.txt", "a")
    53  		testhelpers.Add(Fail, repoDir)
    54  		masterSha = testhelpers.Commit(Fail, repoDir, "a commit")
    55  
    56  		By("creating branch 'b' and adding a commit")
    57  		testhelpers.Branch(Fail, repoDir, "b")
    58  		testhelpers.WriteFile(Fail, repoDir, "b.txt", "b")
    59  		testhelpers.Add(Fail, repoDir)
    60  		branchBSha = testhelpers.Commit(Fail, repoDir, "b commit")
    61  
    62  		By("creating branch 'c' and adding a commit")
    63  		testhelpers.Checkout(Fail, repoDir, "master")
    64  		testhelpers.Branch(Fail, repoDir, "c")
    65  		testhelpers.WriteFile(Fail, repoDir, "c.txt", "c")
    66  		testhelpers.Add(Fail, repoDir)
    67  		branchCSha = testhelpers.Commit(Fail, repoDir, "c commit")
    68  
    69  		By("checking out master")
    70  		testhelpers.Checkout(Fail, repoDir, "master")
    71  	})
    72  
    73  	AfterEach(func() {
    74  		By("closing test stdout")
    75  		_ = testWriter.Close()
    76  	})
    77  
    78  	AfterEach(func() {
    79  		By("deleting temp repo")
    80  		_ = os.RemoveAll(repoDir)
    81  	})
    82  
    83  	Context("with command line options", func() {
    84  		It("succeeds", func() {
    85  			currentHeadSha := testhelpers.HeadSha(Fail, repoDir)
    86  			Expect(currentHeadSha).Should(Equal(masterSha))
    87  
    88  			options := StepGitMergeOptions{
    89  				StepOptions: step.StepOptions{
    90  					CommonOptions: &opts.CommonOptions{},
    91  				},
    92  				SHAs:       []string{branchBSha},
    93  				Dir:        repoDir,
    94  				BaseBranch: "master",
    95  				BaseSHA:    masterSha,
    96  			}
    97  
    98  			err := options.Run()
    99  			Expect(err).NotTo(HaveOccurred())
   100  
   101  			currentHeadSha = testhelpers.HeadSha(Fail, repoDir)
   102  			Expect(currentHeadSha).Should(Equal(branchBSha))
   103  		})
   104  	})
   105  
   106  	Context("with PULL_REFS", func() {
   107  		BeforeEach(func() {
   108  			err := os.Setenv("PULL_REFS", fmt.Sprintf("master:%s,b:%s", masterSha, branchBSha))
   109  			Expect(err).NotTo(HaveOccurred())
   110  		})
   111  
   112  		AfterEach(func() {
   113  			err := os.Unsetenv("PULL_REFS")
   114  			Expect(err).NotTo(HaveOccurred())
   115  		})
   116  
   117  		It("succeeds", func() {
   118  			currentHeadSha := testhelpers.HeadSha(Fail, repoDir)
   119  			Expect(currentHeadSha).Should(Equal(masterSha))
   120  
   121  			options := StepGitMergeOptions{
   122  				StepOptions: step.StepOptions{
   123  					CommonOptions: &opts.CommonOptions{},
   124  				},
   125  				Dir: repoDir,
   126  			}
   127  
   128  			err := options.Run()
   129  			Expect(err).NotTo(HaveOccurred())
   130  
   131  			currentHeadSha = testhelpers.HeadSha(Fail, repoDir)
   132  			Expect(currentHeadSha).Should(Equal(branchBSha))
   133  		})
   134  	})
   135  
   136  	Context("with multiple merge SHAs in PULL_REFS", func() {
   137  		BeforeEach(func() {
   138  			err := os.Setenv("PULL_REFS", fmt.Sprintf("master:%s,c:%s,b:%s", masterSha, branchCSha, branchBSha))
   139  			Expect(err).NotTo(HaveOccurred())
   140  
   141  		})
   142  
   143  		AfterEach(func() {
   144  			err := os.Unsetenv("PULL_REFS")
   145  			Expect(err).NotTo(HaveOccurred())
   146  		})
   147  
   148  		It("merges all shas and creates a merge commit", func() {
   149  			currentHeadSha := testhelpers.HeadSha(Fail, repoDir)
   150  			Expect(currentHeadSha).Should(Equal(masterSha))
   151  
   152  			options := StepGitMergeOptions{
   153  				StepOptions: step.StepOptions{
   154  					CommonOptions: &opts.CommonOptions{
   155  						Verbose: true,
   156  					},
   157  				},
   158  				Dir: repoDir,
   159  			}
   160  
   161  			err := options.Run()
   162  			Expect(err).NotTo(HaveOccurred())
   163  
   164  			out, err := read(testReader, testWriter)
   165  			Expect(err).NotTo(HaveOccurred())
   166  
   167  			logLines := strings.Split(out, "\n")
   168  			logLines = deleteEmpty(logLines)
   169  			Expect(len(logLines)).Should(Equal(4))
   170  			indices := []int{1, 2, 3}
   171  			for _, i := range indices {
   172  				Expect(strings.TrimSpace(logLines[i])).Should(MatchRegexp("Merged SHA .* with commit message .* into base branch master"))
   173  			}
   174  		})
   175  	})
   176  
   177  	Context("with no options and no PULL_REFS", func() {
   178  		It("logs warning", func() {
   179  			options := StepGitMergeOptions{
   180  				StepOptions: step.StepOptions{
   181  					CommonOptions: &opts.CommonOptions{},
   182  				},
   183  				Dir: repoDir,
   184  			}
   185  
   186  			out := log.CaptureOutput(func() {
   187  				err := options.Run()
   188  				Expect(err).NotTo(HaveOccurred())
   189  
   190  				currentHeadSha := testhelpers.HeadSha(Fail, repoDir)
   191  				Expect(currentHeadSha).Should(Equal(masterSha))
   192  			})
   193  
   194  			Expect(out).Should(ContainSubstring("no SHAs to merge"))
   195  		})
   196  	})
   197  })
   198  
   199  func read(r io.Reader, w io.Closer) (string, error) {
   200  	err := w.Close()
   201  	if err != nil {
   202  		return "", err
   203  	}
   204  	data, err := ioutil.ReadAll(r)
   205  	if err != nil {
   206  		return "", err
   207  	}
   208  	return string(data), nil
   209  }
   210  
   211  func deleteEmpty(s []string) []string {
   212  	var clean []string
   213  	for _, str := range s {
   214  		if str != "" {
   215  			clean = append(clean, str)
   216  		}
   217  	}
   218  	return clean
   219  }