github.com/the-gigi/multi-git@v1.0.0/pkg/repo_manager/repo_manager_test.go (about) 1 package repo_manager 2 3 import ( 4 . "github.com/onsi/ginkgo" 5 . "github.com/onsi/gomega" 6 "path/filepath" 7 8 . "github.com/the-gigi/multi-git/pkg/helpers" 9 "os" 10 "path" 11 "strings" 12 ) 13 14 var baseDir string 15 16 var repoList = []string{} 17 18 var _ = Describe("Repo manager tests", func() { 19 var err error 20 21 BeforeSuite(func() { 22 Ω(ConfigureGit()).Should(Succeed()) 23 }) 24 removeAll := func() { 25 err = os.RemoveAll(baseDir) 26 Ω(err).Should(BeNil()) 27 } 28 29 BeforeEach(func() { 30 removeAll() 31 err = CreateDir(baseDir, "dir-1", true) 32 Ω(err).Should(BeNil()) 33 repoList = []string{"dir-1"} 34 }) 35 AfterEach(removeAll) 36 37 Context("Tests for failure cases", func() { 38 It("Should fail with invalid base dir", func() { 39 _, err := NewRepoManager("/no-such-dir", repoList, true) 40 Ω(err).ShouldNot(BeNil()) 41 }) 42 43 It("Should fail with empty repo list", func() { 44 _, err := NewRepoManager(baseDir, []string{}, true) 45 Ω(err).ShouldNot(BeNil()) 46 }) 47 }) 48 49 Context("Tests for success cases", func() { 50 It("Should get repo list successfully", func() { 51 rm, err := NewRepoManager(baseDir, repoList, true) 52 Ω(err).Should(BeNil()) 53 54 repos := rm.GetRepos() 55 Ω(repos).Should(HaveLen(1)) 56 actual := path.Join(baseDir, repoList[0]) 57 expected := repos[0] 58 Ω(actual).Should(Equal(expected)) 59 }) 60 61 It("Should get repo list successfully with non-git directories", func() { 62 repoList = append(repoList, "dir-2") 63 CreateDir(baseDir, repoList[1], true) 64 CreateDir(baseDir, "not-a-git-repo", false) 65 rm, err := NewRepoManager(baseDir, repoList, true) 66 Ω(err).Should(BeNil()) 67 68 repos := rm.GetRepos() 69 Ω(repos).Should(HaveLen(2)) 70 Ω(repos[0] == path.Join(baseDir, repoList[0])).Should(BeTrue()) 71 Ω(repos[1] == path.Join(baseDir, repoList[1])).Should(BeTrue()) 72 }) 73 74 It("Should get repo list successfully with non-git directories", func() { 75 repoList = append(repoList, "dir-2") 76 CreateDir(baseDir, repoList[1], true) 77 CreateDir(baseDir, "not-a-git-repo", false) 78 rm, err := NewRepoManager(baseDir, repoList, true) 79 Ω(err).Should(BeNil()) 80 81 repos := rm.GetRepos() 82 Ω(repos).Should(HaveLen(2)) 83 Ω(repos[0] == path.Join(baseDir, repoList[0])).Should(BeTrue()) 84 Ω(repos[1] == path.Join(baseDir, repoList[1])).Should(BeTrue()) 85 }) 86 87 It("Should create branches successfully", func() { 88 repoList = append(repoList, "dir-2") 89 CreateDir(baseDir, repoList[1], true) 90 rm, err := NewRepoManager(baseDir, repoList, true) 91 Ω(err).Should(BeNil()) 92 93 output, err := rm.Exec("checkout -b test-branch") 94 Ω(err).Should(BeNil()) 95 96 for _, out := range output { 97 Ω(out).Should(Equal("Switched to a new branch 'test-branch'\n")) 98 } 99 }) 100 101 It("Should commit files successfully", func() { 102 rm, err := NewRepoManager(baseDir, repoList, true) 103 Ω(err).Should(BeNil()) 104 105 output, err := rm.Exec("checkout -b test-branch") 106 Ω(err).Should(BeNil()) 107 108 for _, out := range output { 109 Ω(out).Should(Equal("Switched to a new branch 'test-branch'\n")) 110 } 111 112 err = AddFiles(baseDir, repoList[0], true, "file_1.txt", "file_2.txt") 113 Ω(err).Should(BeNil()) 114 115 // Restore working directory after executing the command 116 wd, _ := os.Getwd() 117 defer os.Chdir(wd) 118 119 dir := path.Join(baseDir, repoList[0]) 120 err = os.Chdir(dir) 121 Ω(err).Should(BeNil()) 122 123 output, err = rm.Exec("log --oneline") 124 Ω(err).Should(BeNil()) 125 126 ok := strings.HasSuffix(output[dir], "added some files...\n") 127 Ω(ok).Should(BeTrue()) 128 }) 129 }) 130 }) 131 132 func init() { 133 baseDir, _ = filepath.Abs("tmp/test-multi-git") 134 }