github.com/lgug2z/story@v0.4.1/git/push_test.go (about)

     1  package git_test
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  
     7  	"github.com/LGUG2Z/story/git"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/spf13/afero"
    11  )
    12  
    13  var _ = Describe("Push", func() {
    14  	BeforeEach(func() {
    15  		if err := fs.MkdirAll("test", os.FileMode(0700)); err != nil {
    16  			Fail(err.Error())
    17  		}
    18  
    19  		if err := os.Chdir("test"); err != nil {
    20  			Fail(err.Error())
    21  		}
    22  
    23  		if err := initialiseRepository("."); err != nil {
    24  			Fail(err.Error())
    25  		}
    26  	})
    27  
    28  	AfterEach(func() {
    29  		if err := os.Chdir(".."); err != nil {
    30  			Fail(err.Error())
    31  		}
    32  
    33  		if err := fs.RemoveAll("test"); err != nil {
    34  			Fail(err.Error())
    35  		}
    36  	})
    37  	Describe("Pushing commits", func() {
    38  		It("Should push unpushed commits in a project", func() {
    39  			// Given a repository, with a remote "origin"
    40  			Expect(fs.MkdirAll("remote", os.FileMode(0700))).To(Succeed())
    41  			command := exec.Command("git", "init", "--bare")
    42  			command.Dir = "remote"
    43  			_, err := command.CombinedOutput()
    44  			Expect(err).NotTo(HaveOccurred())
    45  
    46  			command = exec.Command("git", "remote", "add", "origin", "./remote")
    47  			_, err = command.CombinedOutput()
    48  			Expect(err).NotTo(HaveOccurred())
    49  
    50  			command = exec.Command("git", "push", "--set-upstream", "origin", "master")
    51  			_, err = command.CombinedOutput()
    52  			Expect(err).NotTo(HaveOccurred())
    53  
    54  			// And a new branch which is pushed
    55  			expectedBranch := "test-branch"
    56  			_, err = git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch})
    57  			Expect(err).NotTo(HaveOccurred())
    58  
    59  			command = exec.Command("git", "push", "--set-upstream", "origin", "test-branch")
    60  			_, err = command.CombinedOutput()
    61  			Expect(err).NotTo(HaveOccurred())
    62  
    63  			// And a change committed but not yet pushed
    64  			Expect(afero.WriteFile(fs, "bla", []byte{}, os.FileMode(0666))).To(Succeed())
    65  			_, err = git.Add(git.AddOpts{Files: []string{"bla"}})
    66  			Expect(err).NotTo(HaveOccurred())
    67  			_, err = git.Commit(git.CommitOpts{Messages: []string{"commit bla"}})
    68  			Expect(err).NotTo(HaveOccurred())
    69  
    70  			// When I push
    71  			combinedOutput, err := git.Push(git.PushOpts{Branch: "test-branch", Remote: "origin"})
    72  
    73  			// The push should be completed successfully
    74  			Expect(err).NotTo(HaveOccurred())
    75  			Expect(combinedOutput).To(ContainSubstring("test-branch -> test-branch"))
    76  		})
    77  
    78  		It("Should not take action if there are no unpushed commits", func() {
    79  			// Given a repository, with a remote "origin"
    80  			Expect(fs.MkdirAll("remote", os.FileMode(0700))).To(Succeed())
    81  			command := exec.Command("git", "init", "--bare")
    82  			command.Dir = "remote"
    83  			_, err := command.CombinedOutput()
    84  			Expect(err).NotTo(HaveOccurred())
    85  
    86  			command = exec.Command("git", "remote", "add", "origin", "./remote")
    87  			_, err = command.CombinedOutput()
    88  			Expect(err).NotTo(HaveOccurred())
    89  
    90  			command = exec.Command("git", "push", "--set-upstream", "origin", "master")
    91  			_, err = command.CombinedOutput()
    92  			Expect(err).NotTo(HaveOccurred())
    93  
    94  			// And a new branch which is pushed
    95  			expectedBranch := "test-branch"
    96  			_, err = git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch})
    97  			Expect(err).NotTo(HaveOccurred())
    98  
    99  			command = exec.Command("git", "push", "--set-upstream", "origin", "test-branch")
   100  			_, err = command.CombinedOutput()
   101  			Expect(err).NotTo(HaveOccurred())
   102  
   103  			// And a change committed and pushed
   104  			Expect(afero.WriteFile(fs, "bla", []byte{}, os.FileMode(0666))).To(Succeed())
   105  			_, err = git.Add(git.AddOpts{Files: []string{"bla"}})
   106  			Expect(err).NotTo(HaveOccurred())
   107  			_, err = git.Commit(git.CommitOpts{Messages: []string{"commit bla"}})
   108  			Expect(err).NotTo(HaveOccurred())
   109  			_, err = git.Push(git.PushOpts{Branch: "test-branch", Remote: "origin"})
   110  			Expect(err).NotTo(HaveOccurred())
   111  
   112  			// When I push
   113  			combinedOutput, err := git.Push(git.PushOpts{Branch: "test-branch", Remote: "origin"})
   114  
   115  			// Then I receive a message telling me there are no unpushed commits
   116  			Expect(err).NotTo(HaveOccurred())
   117  			Expect(combinedOutput).To(ContainSubstring("no unpushed commits"))
   118  
   119  		})
   120  	})
   121  })