github.com/LGUG2Z/story@v0.4.1/git/branch_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("Branch", 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 38 Describe("Checking out branches", func() { 39 It("Should create a new branch", func() { 40 // Given a repository 41 42 // When I check out a new branch 43 expectedBranch := "test-branch" 44 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 45 Expect(err).NotTo(HaveOccurred()) 46 47 // Then that branch should be created and set as the current branch 48 actualBranch, err := git.GetCurrentBranch(fs, ".") 49 Expect(err).NotTo(HaveOccurred()) 50 Expect(actualBranch).To(Equal(expectedBranch)) 51 }) 52 53 It("Should fail to create a new branch if a branch of the same name already exists", func() { 54 // Given a repository, with a new branch, reverted back to master 55 expectedBranch := "test-branch" 56 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 57 Expect(err).NotTo(HaveOccurred()) 58 _, err = git.CheckoutBranch(git.CheckoutBranchOpts{Branch: "master"}) 59 Expect(err).NotTo(HaveOccurred()) 60 61 // When I create a branch of the same name 62 _, err = git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 63 64 // Then I should receive an error 65 Expect(err).To(HaveOccurred()) 66 }) 67 68 It("Should checkout an existing branch", func() { 69 // Given a repository, with a new branch, reverted back to master 70 expectedBranch := "test-branch" 71 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 72 Expect(err).NotTo(HaveOccurred()) 73 _, err = git.CheckoutBranch(git.CheckoutBranchOpts{Branch: "master"}) 74 Expect(err).NotTo(HaveOccurred()) 75 76 // When I checkout the branch 77 _, err = git.CheckoutBranch(git.CheckoutBranchOpts{Branch: expectedBranch}) 78 Expect(err).NotTo(HaveOccurred()) 79 80 // Then that branch should be created and set as the current branch 81 actualBranch, err := git.GetCurrentBranch(fs, ".") 82 Expect(err).NotTo(HaveOccurred()) 83 Expect(actualBranch).To(Equal(expectedBranch)) 84 }) 85 86 It("Should fail to checkout a branch that doesn't exist", func() { 87 // Given a repository 88 89 // When I check out a branch that hasn't been created 90 expectedBranch := "test-branch" 91 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Branch: expectedBranch}) 92 93 // Then I should receive an error 94 Expect(err).To(HaveOccurred()) 95 96 }) 97 }) 98 99 Describe("Deleting branches", func() { 100 It("Should delete a local branch", func() { 101 // Given a repository, with a new branch, reverted back to master 102 expectedBranch := "test-branch" 103 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 104 Expect(err).NotTo(HaveOccurred()) 105 _, err = git.CheckoutBranch(git.CheckoutBranchOpts{Branch: "master"}) 106 Expect(err).NotTo(HaveOccurred()) 107 108 // When I delete the branch 109 _, err = git.DeleteBranch(git.DeleteBranchOpts{Branch: expectedBranch, Local: true}) 110 Expect(err).NotTo(HaveOccurred()) 111 112 _, err = afero.ReadFile(fs, ".git/refs/heads/test-branch") 113 Expect(err).To(HaveOccurred()) 114 Expect(err.Error()).To(Equal("open .git/refs/heads/test-branch: no such file or directory")) 115 }) 116 117 It("Should delete a remote branch", func() { 118 // Given a repository, with a remote "origin" 119 Expect(fs.MkdirAll("remote", os.FileMode(0700))).To(Succeed()) 120 command := exec.Command("git", "init", "--bare") 121 command.Dir = "remote" 122 _, err := command.CombinedOutput() 123 Expect(err).NotTo(HaveOccurred()) 124 125 command = exec.Command("git", "remote", "add", "origin", "./remote") 126 _, err = command.CombinedOutput() 127 Expect(err).NotTo(HaveOccurred()) 128 129 command = exec.Command("git", "push", "--set-upstream", "origin", "master") 130 _, err = command.CombinedOutput() 131 Expect(err).NotTo(HaveOccurred()) 132 133 // And a new branch and push it 134 expectedBranch := "test-branch" 135 _, err = git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 136 Expect(err).NotTo(HaveOccurred()) 137 138 command = exec.Command("git", "push", "--set-upstream", "origin", "test-branch") 139 _, err = command.CombinedOutput() 140 Expect(err).NotTo(HaveOccurred()) 141 142 // When I delete the branch remotely 143 _, err = git.DeleteBranch(git.DeleteBranchOpts{Branch: expectedBranch, Remote: true}) 144 Expect(err).NotTo(HaveOccurred()) 145 146 // Then the branch should not exist any more on the remote 147 _, err = afero.ReadFile(fs, "remote/refs/heads/test-branch") 148 Expect(err).To(HaveOccurred()) 149 Expect(err.Error()).To(Equal("open remote/refs/heads/test-branch: no such file or directory")) 150 }) 151 }) 152 153 Describe("Determining branches", func() { 154 It("Determine the current branch of a repository", func() { 155 // Given a repository, with a new branch checked out 156 expectedBranch := "test-branch" 157 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 158 Expect(err).NotTo(HaveOccurred()) 159 160 // When I check the branch of the repository 161 actualBranch, err := git.GetCurrentBranch(fs, ".") 162 Expect(err).NotTo(HaveOccurred()) 163 164 // Then it should be the branch checked out 165 Expect(actualBranch).To(Equal(expectedBranch)) 166 }) 167 }) 168 169 Describe("Comparing branch heads", func() { 170 It("Should identify branches that have the same heads", func() { 171 // Given a repository, with a new branch checked out 172 expectedBranch := "test-branch" 173 _, err := git.CheckoutBranch(git.CheckoutBranchOpts{Create: true, Branch: expectedBranch}) 174 Expect(err).NotTo(HaveOccurred()) 175 176 // When I check the equality of the heads 177 areEqual, err := git.HeadsAreEqual(fs, ".", "test-branch", "master") 178 Expect(err).NotTo(HaveOccurred()) 179 180 // Then they should be equal 181 Expect(areEqual).To(BeTrue()) 182 }) 183 }) 184 })