github.com/LGUG2Z/story@v0.4.1/git/add_test.go (about) 1 package git_test 2 3 import ( 4 "os" 5 "os/exec" 6 "strings" 7 8 "github.com/LGUG2Z/story/git" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/spf13/afero" 12 ) 13 14 var _ = Describe("Add", func() { 15 BeforeEach(func() { 16 if err := fs.MkdirAll("test", os.FileMode(0700)); err != nil { 17 Fail(err.Error()) 18 } 19 20 if err := os.Chdir("test"); err != nil { 21 Fail(err.Error()) 22 } 23 24 if err := initialiseRepository("."); err != nil { 25 Fail(err.Error()) 26 } 27 }) 28 29 AfterEach(func() { 30 if err := os.Chdir(".."); err != nil { 31 Fail(err.Error()) 32 } 33 34 if err := fs.RemoveAll("test"); err != nil { 35 Fail(err.Error()) 36 } 37 }) 38 39 Describe("Adding files to be committed", func() { 40 It("Should add the files to the staging area for the next commit", func() { 41 // Given a repository with an untracked file in a subfolder 42 Expect(fs.MkdirAll("sub", os.FileMode(0700))).To(Succeed()) 43 Expect(afero.WriteFile(fs, "sub/newfile", []byte{}, os.FileMode(0666))).To(Succeed()) 44 45 // When I add it to the staging area 46 _, err := git.Add(git.AddOpts{Project: "sub", Files: []string{"newfile"}}) 47 Expect(err).NotTo(HaveOccurred()) 48 49 // Then the files appear in the list of files staged for commit 50 command := exec.Command("git", "diff", "--cached", "--name-only") 51 52 combinedOutput, err := command.CombinedOutput() 53 Expect(err).NotTo(HaveOccurred()) 54 55 actual := strings.TrimSpace(string(combinedOutput)) 56 Expect(actual).To(Equal("sub/newfile")) 57 }) 58 }) 59 })