github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/git/git_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package git_test
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"k8s.io/test-infra/prow/git/localgit"
    27  )
    28  
    29  func TestClone(t *testing.T) {
    30  	lg, c, err := localgit.New()
    31  	if err != nil {
    32  		t.Fatalf("Making local git repo: %v", err)
    33  	}
    34  	defer func() {
    35  		if err := lg.Clean(); err != nil {
    36  			t.Errorf("Error cleaning LocalGit: %v", err)
    37  		}
    38  		if err := c.Clean(); err != nil {
    39  			t.Errorf("Error cleaning Client: %v", err)
    40  		}
    41  	}()
    42  	if err := lg.MakeFakeRepo("foo", "bar"); err != nil {
    43  		t.Fatalf("Making fake repo: %v", err)
    44  	}
    45  	if err := lg.MakeFakeRepo("foo", "baz"); err != nil {
    46  		t.Fatalf("Making fake repo: %v", err)
    47  	}
    48  
    49  	// Fresh clone, will be a cache miss.
    50  	r1, err := c.Clone("foo/bar")
    51  	if err != nil {
    52  		t.Fatalf("Cloning the first time: %v", err)
    53  	}
    54  	defer func() {
    55  		if err := r1.Clean(); err != nil {
    56  			t.Errorf("Cleaning repo: %v", err)
    57  		}
    58  	}()
    59  
    60  	// Clone from the same org.
    61  	r2, err := c.Clone("foo/baz")
    62  	if err != nil {
    63  		t.Fatalf("Cloning another repo in the same org: %v", err)
    64  	}
    65  	defer func() {
    66  		if err := r2.Clean(); err != nil {
    67  			t.Errorf("Cleaning repo: %v", err)
    68  		}
    69  	}()
    70  
    71  	// Make sure it fetches when we clone again.
    72  	if err := lg.AddCommit("foo", "bar", map[string][]byte{"second": {}}); err != nil {
    73  		t.Fatalf("Adding second commit: %v", err)
    74  	}
    75  	r3, err := c.Clone("foo/bar")
    76  	if err != nil {
    77  		t.Fatalf("Cloning a second time: %v", err)
    78  	}
    79  	defer func() {
    80  		if err := r3.Clean(); err != nil {
    81  			t.Errorf("Cleaning repo: %v", err)
    82  		}
    83  	}()
    84  	log := exec.Command("git", "log", "--oneline")
    85  	log.Dir = r3.Dir
    86  	if b, err := log.CombinedOutput(); err != nil {
    87  		t.Fatalf("git log: %v, %s", err, string(b))
    88  	} else {
    89  		t.Logf("git log output: %s", string(b))
    90  		if len(bytes.Split(bytes.TrimSpace(b), []byte("\n"))) != 2 {
    91  			t.Error("Wrong number of commits in git log output. Expected 2")
    92  		}
    93  	}
    94  }
    95  
    96  func TestCheckoutPR(t *testing.T) {
    97  	lg, c, err := localgit.New()
    98  	if err != nil {
    99  		t.Fatalf("Making local git repo: %v", err)
   100  	}
   101  	defer func() {
   102  		if err := lg.Clean(); err != nil {
   103  			t.Errorf("Error cleaning LocalGit: %v", err)
   104  		}
   105  		if err := c.Clean(); err != nil {
   106  			t.Errorf("Error cleaning Client: %v", err)
   107  		}
   108  	}()
   109  	if err := lg.MakeFakeRepo("foo", "bar"); err != nil {
   110  		t.Fatalf("Making fake repo: %v", err)
   111  	}
   112  	r, err := c.Clone("foo/bar")
   113  	if err != nil {
   114  		t.Fatalf("Cloning: %v", err)
   115  	}
   116  	defer func() {
   117  		if err := r.Clean(); err != nil {
   118  			t.Errorf("Cleaning repo: %v", err)
   119  		}
   120  	}()
   121  
   122  	if err := lg.CheckoutNewBranch("foo", "bar", "pull/123/head"); err != nil {
   123  		t.Fatalf("Checkout new branch: %v", err)
   124  	}
   125  	if err := lg.AddCommit("foo", "bar", map[string][]byte{"wow": {}}); err != nil {
   126  		t.Fatalf("Add commit: %v", err)
   127  	}
   128  
   129  	if err := r.CheckoutPullRequest(123); err != nil {
   130  		t.Fatalf("Checking out PR: %v", err)
   131  	}
   132  	if _, err := os.Stat(filepath.Join(r.Dir, "wow")); err != nil {
   133  		t.Errorf("Didn't find file in PR after checking out: %v", err)
   134  	}
   135  }