github.com/mhristof/go-stacks@v0.1.3/git/branches_test.go (about) 1 package git 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/MakeNowJust/heredoc" 10 "github.com/mhristof/go-stacks/bash" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func mkgit(t *testing.T, commands []string) string { 15 dir, err := ioutil.TempDir("", "git") 16 if err != nil { 17 panic(err) 18 } 19 20 err = os.Chdir(dir) 21 if err != nil { 22 panic(err) 23 } 24 25 for _, command := range commands { 26 _, _, err = bash.Run(command) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 } 32 return dir 33 } 34 35 func TestBranches(t *testing.T) { 36 var cases = []struct { 37 name string 38 branches []string 39 path string 40 }{ 41 { 42 name: "non git folder", 43 path: mkgit(t, []string{}), 44 }, 45 { 46 name: "git folder with main", 47 path: mkgit(t, []string{ 48 "git init", 49 "git commit --allow-empty -m 'empty.commit'", 50 }), 51 branches: []string{ 52 "main", 53 }, 54 }, 55 { 56 name: "git folder with a couple of branches", 57 path: mkgit(t, []string{ 58 "git init", 59 "git commit --allow-empty -m 'empty.commit'", 60 "git checkout -b foobar", 61 "git checkout -b foobar1", 62 }), 63 branches: []string{ 64 "foobar", "foobar1", "main", 65 }, 66 }, 67 } 68 69 for _, test := range cases { 70 assert.Equal(t, test.branches, Branches(test.path), test.name) //nolint 71 defer os.Remove(test.path) 72 } 73 } 74 75 func TestRebase(t *testing.T) { 76 var cases = []struct { 77 name string 78 path string 79 branch string 80 gitLog string 81 }{ 82 { 83 name: "main ahead of feat1", 84 branch: ".*", 85 path: mkgit(t, []string{ 86 "git init", 87 "git commit --allow-empty -m 'empty.commit'", 88 "git checkout -b feat1", 89 "git commit --allow-empty -m 'feat1.commit'", 90 "git checkout main", 91 "git commit --allow-empty -m 'main.commit'", 92 }), 93 gitLog: heredoc.Doc(` 94 * (HEAD -> feat1).'feat1.commit' 95 * (main).'main.commit' 96 * .'empty.commit'`), 97 }, 98 { 99 name: "feat1 ahead of feat1.1", 100 branch: ".*", 101 path: mkgit(t, []string{ 102 "git init", 103 "git commit --allow-empty -m 'empty.commit'", 104 "git commit --allow-empty -m 'empty.commit1'", 105 "git checkout -b feat1", 106 "git commit --allow-empty -m 'feat1.commit'", 107 "git checkout -b feat1.1", 108 "git commit --allow-empty -m 'feat1.1.commit'", 109 "git checkout feat1", 110 "git commit --allow-empty -m 'feat1.commit2'", 111 }), 112 gitLog: heredoc.Doc(` 113 * (HEAD -> feat1.1).'feat1.1.commit' 114 * (feat1).'feat1.commit2' 115 * .'feat1.commit' 116 * .'empty.commit1' 117 * (main).'empty.commit1' 118 * .'empty.commit'`), 119 }, 120 { 121 name: "no changes", 122 branch: ".*", 123 path: mkgit(t, []string{ 124 "git init", 125 "git commit --allow-empty -m 'empty.commit'", 126 "git commit --allow-empty -m 'empty.commit1'", 127 "git checkout -b feat1", 128 "git commit --allow-empty -m 'feat1.commit'", 129 "git checkout -b feat1.1", 130 "git commit --allow-empty -m 'feat1.1.commit'", 131 }), 132 gitLog: heredoc.Doc(` 133 * (HEAD -> feat1.1).'feat1.1.commit' 134 * (feat1).'feat1.commit' 135 * .'empty.commit1' 136 * (main).'empty.commit1' 137 * .'empty.commit'`), 138 }, 139 { 140 name: "limit branch name", 141 branch: "feat1", 142 path: mkgit(t, strings.Split(heredoc.Doc(` 143 git init 144 git commit --allow-empty -m 'empty.commit' 145 git commit --allow-empty -m 'empty.commit1' 146 git checkout -b feat1 147 git commit --allow-empty -m 'feat1.commit' 148 git checkout -b feat1.1 149 git commit --allow-empty -m 'feat1.1.commit' 150 git checkout feat1 151 git commit --allow-empty -m 'feat1.commit1' 152 git checkout main 153 git checkout -b feat2 154 git commit --allow-empty -m 'feat2.commit1' 155 git checkout -b feat2.1 156 git commit --allow-empty -m 'feat2.1.commit1' 157 git checkout feat2 158 git commit --allow-empty -m 'feat2.commit2'`), 159 "\n")), 160 gitLog: heredoc.Doc(` 161 * (HEAD -> feat1.1).'feat1.1.commit' 162 * (feat1).'feat1.commit1' 163 * .'feat1.commit' 164 * .'empty.commit1' 165 | * (feat2).'feat2.commit2' 166 | | * (feat2.1).'feat2.1.commit1' 167 | |/ 168 | * .'feat2.commit1' 169 |/ 170 * (main).'empty.commit1' 171 * .'empty.commit'`), 172 }, 173 } 174 175 for _, test := range cases { 176 err := os.Chdir(test.path) 177 if err != nil { 178 panic(err) 179 } 180 181 commands, err := Rebase(test.path, test.branch) //nolint 182 if err != nil { 183 panic(err) 184 } 185 186 for _, command := range commands { 187 _, _, err := bash.Run(command) 188 if err != nil { 189 t.Fatal(err) 190 } 191 192 } 193 stdout, _, err := bash.Run(`git log --graph --pretty=format:%d.%s --all`) 194 if err != nil { 195 t.Fatal(err) 196 } 197 198 assert.Equal(t, test.gitLog, strings.ReplaceAll(stdout, "/ ", "/"), test.name) 199 defer os.Remove(test.path) 200 } 201 }