|
1 | 1 | package git
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
4 | 10 | "testing"
|
5 | 11 | "time"
|
6 | 12 | )
|
@@ -147,3 +153,147 @@ type timedCommandOutput struct {
|
147 | 153 | stderr string
|
148 | 154 | err error
|
149 | 155 | }
|
| 156 | + |
| 157 | +type testGitRepo struct { |
| 158 | + Name string |
| 159 | + Path string |
| 160 | +} |
| 161 | + |
| 162 | +func initializeTestGitRepo(name string) (*testGitRepo, error) { |
| 163 | + repo := &testGitRepo{Name: name} |
| 164 | + dir, err := ioutil.TempDir("", "test-"+repo.Name) |
| 165 | + if err != nil { |
| 166 | + return repo, err |
| 167 | + } |
| 168 | + repo.Path = dir |
| 169 | + tmpfn := filepath.Join(dir, "initial-file") |
| 170 | + if err := ioutil.WriteFile(tmpfn, []byte("test"), 0666); err != nil { |
| 171 | + return repo, fmt.Errorf("unable to create temporary file") |
| 172 | + } |
| 173 | + initCmd := exec.Command("git", "init") |
| 174 | + initCmd.Dir = dir |
| 175 | + if out, err := initCmd.CombinedOutput(); err != nil { |
| 176 | + return repo, fmt.Errorf("unable to initialize repository: %q", out) |
| 177 | + } |
| 178 | + |
| 179 | + configEmailCmd := exec. Command( "git", "config", "user.email", "[email protected]") |
| 180 | + configEmailCmd.Dir = dir |
| 181 | + if out, err := configEmailCmd.CombinedOutput(); err != nil { |
| 182 | + return repo, fmt.Errorf("unable to set git email prefs: %q", out) |
| 183 | + } |
| 184 | + configNameCmd := exec.Command("git", "config", "user.name", "Me Myself") |
| 185 | + configNameCmd.Dir = dir |
| 186 | + if out, err := configNameCmd.CombinedOutput(); err != nil { |
| 187 | + return repo, fmt.Errorf("unable to set git name prefs: %q", out) |
| 188 | + } |
| 189 | + addCmd := exec.Command("git", "add", "initial-file") |
| 190 | + addCmd.Dir = dir |
| 191 | + if out, err := addCmd.CombinedOutput(); err != nil { |
| 192 | + return repo, fmt.Errorf("unable to add file: %q", out) |
| 193 | + } |
| 194 | + commitCmd := exec.Command("git", "commit", "-a", "-m", "repo create") |
| 195 | + commitCmd.Dir = dir |
| 196 | + if out, err := commitCmd.CombinedOutput(); err != nil { |
| 197 | + return repo, fmt.Errorf("problem committing files %q", out) |
| 198 | + } |
| 199 | + return repo, nil |
| 200 | +} |
| 201 | + |
| 202 | +func TestCheckoutWithBranchAndDirWithSameName(t *testing.T) { |
| 203 | + gitClient := NewRepository() |
| 204 | + testRepo, err := initializeTestGitRepo("test") |
| 205 | + defer os.RemoveAll(testRepo.Path) |
| 206 | + if err != nil { |
| 207 | + t.Errorf("problem creating test git repo: %v", err) |
| 208 | + } |
| 209 | + |
| 210 | + // first, create branch "openshift" off of empty repo |
| 211 | + brCmd := exec.Command("git", "branch", "openshift") |
| 212 | + brCmd.Dir = testRepo.Path |
| 213 | + if out, err := brCmd.CombinedOutput(); err != nil { |
| 214 | + t.Errorf("problem creating openshift branch: %q", out) |
| 215 | + } |
| 216 | + |
| 217 | + // second, switch back to master, create openshift dir with file foo, commit |
| 218 | + err = gitClient.Checkout(testRepo.Path, "master") |
| 219 | + if err != nil { |
| 220 | + t.Errorf("problem switching to master: %v", err) |
| 221 | + } |
| 222 | + |
| 223 | + dirname := filepath.Join(testRepo.Path, "openshift") |
| 224 | + err = os.Mkdir(dirname, 0700) |
| 225 | + if err != nil { |
| 226 | + t.Errorf("problem creating openshift dir: %v", err) |
| 227 | + } |
| 228 | + filename := filepath.Join(dirname, "foo") |
| 229 | + _, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) |
| 230 | + if err != nil { |
| 231 | + t.Errorf("problem creating file foo: %v", err) |
| 232 | + } |
| 233 | + |
| 234 | + addCmd := exec.Command("git", "add", "openshift") |
| 235 | + addCmd.Dir = testRepo.Path |
| 236 | + if out, err := addCmd.CombinedOutput(); err != nil { |
| 237 | + t.Errorf("problem adding files: %q", out) |
| 238 | + } |
| 239 | + commitCmd := exec.Command("git", "commit", "-a", "-m", "openshift dir create") |
| 240 | + commitCmd.Dir = testRepo.Path |
| 241 | + if out, err := commitCmd.CombinedOutput(); err != nil { |
| 242 | + t.Errorf("problem committing files %q", out) |
| 243 | + } |
| 244 | + |
| 245 | + // validate that after clone, a checkout of openshift results in a branch checkout |
| 246 | + // and not a dir checkout (since we use "--" on checkout) ...note: we only see this after a clone; on the initial |
| 247 | + // repo creation above, vanilla checkout (whithout "--") would still result in the branch getting checked out |
| 248 | + // vs. the dir |
| 249 | + |
| 250 | + // create new dir, clone test repo |
| 251 | + dir, err := ioutil.TempDir("", "newlocation") |
| 252 | + if err != nil { |
| 253 | + t.Errorf("%v", err) |
| 254 | + } |
| 255 | + defer os.RemoveAll(dir) |
| 256 | + |
| 257 | + cloneCmd := exec.Command("git", "clone", "file://"+testRepo.Path) |
| 258 | + cloneCmd.Dir = dir |
| 259 | + if out, err := cloneCmd.CombinedOutput(); err != nil { |
| 260 | + t.Errorf("problem cloning test repo: %q", out) |
| 261 | + } |
| 262 | + |
| 263 | + newRepoLocation := filepath.Join(dir, filepath.Base(testRepo.Path)) |
| 264 | + // per http://stackoverflow.com/questions/1783405/how-to-check-out-a-remote-git-branch |
| 265 | + // do a git fetch before git checkout, though this has been unnecessary in local testing |
| 266 | + fetchCmd := exec.Command("git", "fetch") |
| 267 | + fetchCmd.Dir = newRepoLocation |
| 268 | + if out, err := fetchCmd.CombinedOutput(); err != nil { |
| 269 | + t.Errorf("problem with fetch %q", out) |
| 270 | + } |
| 271 | + err = gitClient.Checkout(newRepoLocation, "openshift") |
| 272 | + if err != nil { |
| 273 | + vcmd := exec.Command("git", "version") |
| 274 | + vcmd.Dir = newRepoLocation |
| 275 | + version := "" |
| 276 | + if out, err := vcmd.CombinedOutput(); err == nil { |
| 277 | + version = string(out) |
| 278 | + t.Logf("git version is %s", version) |
| 279 | + } |
| 280 | + bcmd := exec.Command("git", "branch", "-r") |
| 281 | + bcmd.Dir = newRepoLocation |
| 282 | + allbranches := "" |
| 283 | + if out, err := bcmd.CombinedOutput(); err == nil { |
| 284 | + t.Logf("branches in new repo %q", out) |
| 285 | + allbranches = string(out) |
| 286 | + } |
| 287 | + t.Errorf("problem switching to openshift using version %s from within %s, where all branches are %s: %v", version, newRepoLocation, allbranches, err) |
| 288 | + } |
| 289 | + |
| 290 | + statusCmd := exec.Command("git", "status") |
| 291 | + statusCmd.Dir = newRepoLocation |
| 292 | + if out, err := statusCmd.CombinedOutput(); err != nil { |
| 293 | + t.Errorf("unable to run git status %q", out) |
| 294 | + } else { |
| 295 | + if !strings.Contains(string(out), "branch openshift") { |
| 296 | + t.Errorf("expected to be on branch openshift, but instead: %q", out) |
| 297 | + } |
| 298 | + } |
| 299 | +} |
0 commit comments