github.com/mcdonaldseanp/charlie@v0.0.15/githelpers/branch_test.go (about) 1 package githelpers 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/mcdonaldseanp/lookout/localexec" 9 ) 10 11 func initRepo(t *testing.T) { 12 _, _, airr := localexec.ExecReadOutput("git", "init") 13 if airr != nil { 14 t.Fatalf("Failed to create test repo: %s\n", airr) 15 } 16 f, err := os.Create("test.txt") 17 if err != nil { 18 t.Fatalf("Failed to create test repo: %s\n", err) 19 } 20 f.Close() 21 _, _, airr = localexec.ExecReadOutput("git", "add", "--all") 22 if airr != nil { 23 t.Fatalf("Failed to create test repo: %s\n", airr) 24 } 25 _, _, airr = localexec.ExecReadOutput("git", "commit", "-m", "initial", "--no-gpg-sign") 26 if airr != nil { 27 t.Fatalf("Failed to create test repo: %s\n", airr) 28 } 29 _, _, airr = localexec.ExecReadOutput("git", "checkout", "-B", "main") 30 if airr != nil { 31 t.Fatalf("Failed to create test repo: %s\n", airr) 32 } 33 } 34 35 func getBranch(t *testing.T) string { 36 result, _, airr := localexec.ExecReadOutput("git", "rev-parse", "--abbrev-ref", "HEAD") 37 if airr != nil { 38 t.Fatalf("Failed to read branch: %s\n", airr) 39 } 40 return strings.Trim(result, " \n\r\t") 41 } 42 43 func fakeCommit(dir string, t *testing.T) { 44 fakeFile(dir, t) 45 _, _, airr := localexec.ExecReadOutput("git", "add", "--all") 46 if airr != nil { 47 t.Fatalf("Failed to create new commit: %s\n", airr) 48 } 49 _, _, airr = localexec.ExecReadOutput("git", "commit", "-m", "fake", "--no-gpg-sign") 50 if airr != nil { 51 t.Fatalf("Failed to create new commit: %s\n", airr) 52 } 53 } 54 55 func fakeFile(dir string, t *testing.T) { 56 f, err := os.CreateTemp(dir, "") 57 if err != nil { 58 t.Fatalf("Failed to create file: %s\n", err) 59 } 60 f.Close() 61 } 62 63 // Test that checking out the already checked out branch does nothing 64 func TestBranchSame(t *testing.T) { 65 originaldir, _ := os.Getwd() 66 testdir, _ := os.MkdirTemp("", "charlie_testing") 67 defer os.RemoveAll(testdir) 68 defer os.Chdir(originaldir) 69 os.Chdir(testdir) 70 initRepo(t) 71 // Actually run the thing 72 airr := SetBranch("main", false, false) 73 if airr != nil { 74 t.Fatalf("Setting the git branch failed: %s\n", airr) 75 } 76 branch_now := getBranch(t) 77 if branch_now != "main" { 78 t.Fatalf("Branch is not correctly set, should be 'main', is '%s'\n", branch_now) 79 } 80 } 81 82 // Test that checking out an empty branch or one that doesn't exist fails 83 // but does not change the current branch 84 func TestBranchNoExist(t *testing.T) { 85 originaldir, _ := os.Getwd() 86 testdir, _ := os.MkdirTemp("", "charlie_testing") 87 defer os.RemoveAll(testdir) 88 defer os.Chdir(originaldir) 89 os.Chdir(testdir) 90 initRepo(t) 91 // Actually run the thing 92 airr := SetBranch("", false, false) 93 if airr == nil { 94 t.Fatalf("Setting the git branch was supposed to fail!") 95 } 96 // The branch should remain the same 97 branch_now := getBranch(t) 98 if branch_now != "main" { 99 t.Fatalf("Branch is not correctly set, should be 'main', is '%s'\n", branch_now) 100 } 101 // run again with a real name 102 airr = SetBranch("non_existant", false, false) 103 if airr == nil { 104 t.Fatalf("Setting the git branch was supposed to fail!") 105 } 106 // The branch should remain the same 107 branch_now = getBranch(t) 108 if branch_now != "main" { 109 t.Fatalf("Branch is not correctly set, should be 'main', is '%s'\n", branch_now) 110 } 111 } 112 113 // Test that checking out a separate branch works 114 func TestBranchDifferent(t *testing.T) { 115 originaldir, _ := os.Getwd() 116 testdir, _ := os.MkdirTemp("", "charlie_testing") 117 defer os.RemoveAll(testdir) 118 defer os.Chdir(originaldir) 119 os.Chdir(testdir) 120 initRepo(t) 121 _, _, airr := localexec.ExecReadOutput("git", "checkout", "-B", "different") 122 if airr != nil { 123 t.Fatalf("Creating second git branch failed: %s\n", airr) 124 } 125 // Check to make sure we are actually on a new branch 126 branch_now := getBranch(t) 127 if branch_now != "different" { 128 t.Fatalf("Branch is not correctly set, should be 'different', is '%s'\n", branch_now) 129 } 130 // Create a fake commit so the new branch is ahead of main 131 fakeCommit(testdir, t) 132 // Actually run the thing 133 err := SetBranch("main", false, false) 134 if err != nil { 135 t.Fatalf("Setting the git branch failed: %s\n", err) 136 } 137 branch_now = getBranch(t) 138 if branch_now != "main" { 139 t.Fatalf("Branch is not correctly set, should be 'main', is '%s'\n", branch_now) 140 } 141 } 142 143 // Test that checking out a separate branch when the worktree is dirty fails 144 func TestBranchDirty(t *testing.T) { 145 originaldir, _ := os.Getwd() 146 testdir, _ := os.MkdirTemp("", "charlie_testing") 147 defer os.RemoveAll(testdir) 148 defer os.Chdir(originaldir) 149 os.Chdir(testdir) 150 initRepo(t) 151 _, _, airr := localexec.ExecReadOutput("git", "checkout", "-B", "different") 152 if airr != nil { 153 t.Fatalf("Creating second git branch failed: %s\n", airr) 154 } 155 // Check to make sure we are actually on a new branch 156 branch_now := getBranch(t) 157 if branch_now != "different" { 158 t.Fatalf("Branch is not correctly set, should be 'different', is '%s'\n", branch_now) 159 } 160 // Create a fake commit so the new branch is ahead of main 161 fakeCommit(testdir, t) 162 // Create an uncommitted file 163 fakeFile(testdir, t) 164 // Actually run the thing 165 err := SetBranch("main", false, false) 166 if err == nil { 167 t.Fatalf("Setting the git branch was supposed to fail!") 168 } 169 branch_now = getBranch(t) 170 if branch_now != "different" { 171 t.Fatalf("Branch is not correctly set, should be 'different', is '%s'\n", branch_now) 172 } 173 } 174 175 // Test that checking out with --clear set will clean the branch and switch 176 func TestBranchClear(t *testing.T) { 177 originaldir, _ := os.Getwd() 178 testdir, _ := os.MkdirTemp("", "charlie_testing") 179 defer os.RemoveAll(testdir) 180 defer os.Chdir(originaldir) 181 os.Chdir(testdir) 182 initRepo(t) 183 _, _, airr := localexec.ExecReadOutput("git", "checkout", "-B", "different") 184 if airr != nil { 185 t.Fatalf("Creating second git branch failed: %s\n", airr) 186 } 187 // Check to make sure we are actually on a new branch 188 branch_now := getBranch(t) 189 if branch_now != "different" { 190 t.Fatalf("Branch is not correctly set, should be 'different', is '%s'\n", branch_now) 191 } 192 // Create a fake commit so the new branch is ahead of main 193 fakeCommit(testdir, t) 194 // Create an uncommitted file 195 fakeFile(testdir, t) 196 // Actually run the thing 197 err := SetBranch("main", true, false) 198 if err != nil { 199 t.Fatalf("Setting the git branch failed: %s\n", err) 200 } 201 branch_now = getBranch(t) 202 if branch_now != "main" { 203 t.Fatalf("Branch is not correctly set, should be 'different', is '%s'\n", branch_now) 204 } 205 // Check that the work tree is clean again 206 wt, airr := OpenWorktree() 207 if airr != nil { 208 t.Fatalf("Getting the work tree failed: %s\n", airr) 209 } 210 clean, airr := WorkTreeClean(wt) 211 if airr != nil { 212 t.Fatalf("Getting the work tree failed: %s\n", airr) 213 } 214 if !clean { 215 t.Fatalf("Working tree is not clean") 216 } 217 }