github.com/golang/dep@v0.5.4/gps/vcs_repo_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gps
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/Masterminds/vcs"
    16  )
    17  
    18  // original implementation of these test files come from
    19  // https://github.com/Masterminds/vcs test files
    20  
    21  const gitRemoteTestRepo = "https://github.com/Masterminds/VCSTestRepo"
    22  
    23  func TestErrs(t *testing.T) {
    24  	err := newVcsLocalErrorOr(context.Canceled, nil, "", "")
    25  	if err != context.Canceled {
    26  		t.Errorf("context errors should always pass through, got %s", err)
    27  	}
    28  	err = newVcsRemoteErrorOr(context.Canceled, nil, "", "")
    29  	if err != context.Canceled {
    30  		t.Errorf("context errors should always pass through, got %s", err)
    31  	}
    32  	err = newVcsLocalErrorOr(context.DeadlineExceeded, nil, "", "")
    33  	if err != context.DeadlineExceeded {
    34  		t.Errorf("context errors should always pass through, got %s", err)
    35  	}
    36  	err = newVcsRemoteErrorOr(context.DeadlineExceeded, nil, "", "")
    37  	if err != context.DeadlineExceeded {
    38  		t.Errorf("context errors should always pass through, got %s", err)
    39  	}
    40  
    41  	err = newVcsLocalErrorOr(errors.New("bar"), nil, "foo", "baz")
    42  	if _, is := err.(*vcs.LocalError); !is {
    43  		t.Errorf("should have gotten local error, got %T %v", err, err)
    44  	}
    45  	err = newVcsRemoteErrorOr(errors.New("bar"), nil, "foo", "baz")
    46  	if _, is := err.(*vcs.RemoteError); !is {
    47  		t.Errorf("should have gotten remote error, got %T %v", err, err)
    48  	}
    49  }
    50  
    51  func testSvnRepo(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	if testing.Short() {
    55  		t.Skip("Skipping slow test in short mode")
    56  	}
    57  
    58  	ctx := context.Background()
    59  	tempDir, err := ioutil.TempDir("", "go-vcs-svn-tests")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer func() {
    64  		err = os.RemoveAll(tempDir)
    65  		if err != nil {
    66  			t.Error(err)
    67  		}
    68  	}()
    69  
    70  	rep, err := vcs.NewSvnRepo("https://github.com/Masterminds/VCSTestRepo/trunk", tempDir+string(os.PathSeparator)+"VCSTestRepo")
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	repo := &svnRepo{rep}
    75  
    76  	// Do an initial checkout.
    77  	err = repo.get(ctx)
    78  	if err != nil {
    79  		t.Fatalf("Unable to checkout SVN repo. Err was %s", err)
    80  	}
    81  
    82  	// Verify SVN repo is a SVN repo
    83  	if !repo.CheckLocal() {
    84  		t.Fatal("Problem checking out repo or SVN CheckLocal is not working")
    85  	}
    86  
    87  	// Update the version to a previous version.
    88  	err = repo.updateVersion(ctx, "r2")
    89  	if err != nil {
    90  		t.Fatalf("Unable to update SVN repo version. Err was %s", err)
    91  	}
    92  
    93  	// Use Version to verify we are on the right version.
    94  	v, err := repo.Version()
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	if v != "2" {
    99  		t.Fatal("Error checking checked SVN out version")
   100  	}
   101  
   102  	// Perform an update which should take up back to the latest version.
   103  	err = repo.fetch(ctx)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// Make sure we are on a newer version because of the update.
   109  	v, err = repo.Version()
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	if v == "2" {
   114  		t.Fatal("Error with version. Still on old version. Update failed")
   115  	}
   116  
   117  	ci, err := repo.CommitInfo("2")
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	if ci.Commit != "2" {
   122  		t.Error("Svn.CommitInfo wrong commit id")
   123  	}
   124  	if ci.Author != "matt.farina" {
   125  		t.Error("Svn.CommitInfo wrong author")
   126  	}
   127  	if ci.Message != "Update README.md" {
   128  		t.Error("Svn.CommitInfo wrong message")
   129  	}
   130  	ti, err := time.Parse(time.RFC3339Nano, "2015-07-29T13:46:20.000000Z")
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	if !ti.Equal(ci.Date) {
   135  		t.Error("Svn.CommitInfo wrong date")
   136  	}
   137  
   138  	_, err = repo.CommitInfo("555555555")
   139  	if err != vcs.ErrRevisionUnavailable {
   140  		t.Error("Svn didn't return expected ErrRevisionUnavailable")
   141  	}
   142  }
   143  
   144  func testHgRepo(t *testing.T) {
   145  	t.Parallel()
   146  
   147  	if testing.Short() {
   148  		t.Skip("Skipping slow test in short mode")
   149  	}
   150  
   151  	ctx := context.Background()
   152  	tempDir, err := ioutil.TempDir("", "go-vcs-hg-tests")
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  
   157  	defer func() {
   158  		err = os.RemoveAll(tempDir)
   159  		if err != nil {
   160  			t.Error(err)
   161  		}
   162  	}()
   163  
   164  	rep, err := vcs.NewHgRepo("https://bitbucket.org/mattfarina/testhgrepo", tempDir+"/testhgrepo")
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  
   169  	repo := &hgRepo{rep}
   170  
   171  	// Do an initial clone.
   172  	err = repo.get(ctx)
   173  	if err != nil {
   174  		t.Fatalf("Unable to clone Hg repo. Err was %s", err)
   175  	}
   176  
   177  	// Verify Hg repo is a Hg repo
   178  	if !repo.CheckLocal() {
   179  		t.Fatal("Problem checking out repo or Hg CheckLocal is not working")
   180  	}
   181  
   182  	// Set the version using the short hash.
   183  	err = repo.updateVersion(ctx, "a5494ba2177f")
   184  	if err != nil {
   185  		t.Fatalf("Unable to update Hg repo version. Err was %s", err)
   186  	}
   187  
   188  	// Use Version to verify we are on the right version.
   189  	v, err := repo.Version()
   190  	if err != nil {
   191  		t.Fatal(err)
   192  	}
   193  	if v != "a5494ba2177ff9ef26feb3c155dfecc350b1a8ef" {
   194  		t.Fatalf("Error checking checked out Hg version: %s", v)
   195  	}
   196  
   197  	// Perform an update.
   198  	err = repo.fetch(ctx)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  }
   203  
   204  func testGitRepo(t *testing.T) {
   205  	t.Parallel()
   206  
   207  	if testing.Short() {
   208  		t.Skip("Skipping slow test in short mode")
   209  	}
   210  
   211  	ctx := context.Background()
   212  	tempDir, err := ioutil.TempDir("", "go-vcs-git-tests")
   213  	if err != nil {
   214  		t.Fatal(err)
   215  	}
   216  
   217  	defer func() {
   218  		err = os.RemoveAll(tempDir)
   219  		if err != nil {
   220  			t.Error(err)
   221  		}
   222  	}()
   223  
   224  	rep, err := vcs.NewGitRepo(gitRemoteTestRepo, tempDir+"/VCSTestRepo")
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  
   229  	repo := &gitRepo{rep}
   230  
   231  	// Do an initial clone.
   232  	err = repo.get(ctx)
   233  	if err != nil {
   234  		t.Fatalf("Unable to clone Git repo. Err was %s", err)
   235  	}
   236  
   237  	// Verify Git repo is a Git repo
   238  	if !repo.CheckLocal() {
   239  		t.Fatal("Problem checking out repo or Git CheckLocal is not working")
   240  	}
   241  
   242  	// Perform an update.
   243  	err = repo.fetch(ctx)
   244  	if err != nil {
   245  		t.Fatal(err)
   246  	}
   247  
   248  	v, err := repo.Current()
   249  	if err != nil {
   250  		t.Fatalf("Error trying Git Current: %s", err)
   251  	}
   252  	if v != "master" {
   253  		t.Fatalf("Current failed to detect Git on tip of master. Got version: %s", v)
   254  	}
   255  
   256  	// Set the version using the short hash.
   257  	err = repo.updateVersion(ctx, "806b07b")
   258  	if err != nil {
   259  		t.Fatalf("Unable to update Git repo version. Err was %s", err)
   260  	}
   261  
   262  	// Once a ref has been checked out the repo is in a detached head state.
   263  	// Trying to pull in an update in this state will cause an error. Update
   264  	// should cleanly handle this. Pulling on a branch (tested elsewhere) and
   265  	// skipping that here.
   266  	err = repo.fetch(ctx)
   267  	if err != nil {
   268  		t.Fatal(err)
   269  	}
   270  
   271  	// Use Version to verify we are on the right version.
   272  	v, err = repo.Version()
   273  	if err != nil {
   274  		t.Fatal(err)
   275  	}
   276  	if v != "806b07b08faa21cfbdae93027904f80174679402" {
   277  		t.Fatal("Error checking checked out Git version")
   278  	}
   279  }
   280  
   281  func testBzrRepo(t *testing.T) {
   282  	t.Parallel()
   283  
   284  	if testing.Short() {
   285  		t.Skip("Skipping slow test in short mode")
   286  	}
   287  
   288  	ctx := context.Background()
   289  	tempDir, err := ioutil.TempDir("", "go-vcs-bzr-tests")
   290  	if err != nil {
   291  		t.Fatal(err)
   292  	}
   293  
   294  	defer func() {
   295  		err = os.RemoveAll(tempDir)
   296  		if err != nil {
   297  			t.Error(err)
   298  		}
   299  	}()
   300  
   301  	rep, err := vcs.NewBzrRepo("https://launchpad.net/govcstestbzrrepo", tempDir+"/govcstestbzrrepo")
   302  	if err != nil {
   303  		t.Fatal(err)
   304  	}
   305  
   306  	repo := &bzrRepo{rep}
   307  
   308  	// Do an initial clone.
   309  	err = repo.get(ctx)
   310  	if err != nil {
   311  		t.Fatalf("Unable to clone Bzr repo. Err was %s", err)
   312  	}
   313  
   314  	// Verify Bzr repo is a Bzr repo
   315  	if !repo.CheckLocal() {
   316  		t.Fatal("Problem checking out repo or Bzr CheckLocal is not working")
   317  	}
   318  
   319  	v, err := repo.Current()
   320  	if err != nil {
   321  		t.Fatalf("Error trying Bzr Current: %s", err)
   322  	}
   323  	if v != "-1" {
   324  		t.Fatalf("Current failed to detect Bzr on tip of branch. Got version: %s", v)
   325  	}
   326  
   327  	err = repo.updateVersion(ctx, "2")
   328  	if err != nil {
   329  		t.Fatalf("Unable to update Bzr repo version. Err was %s", err)
   330  	}
   331  
   332  	// Use Version to verify we are on the right version.
   333  	v, err = repo.Version()
   334  	if err != nil {
   335  		t.Fatal(err)
   336  	}
   337  	if v != "2" {
   338  		t.Fatal("Error checking checked out Bzr version")
   339  	}
   340  
   341  	v, err = repo.Current()
   342  	if err != nil {
   343  		t.Fatalf("Error trying Bzr Current: %s", err)
   344  	}
   345  	if v != "2" {
   346  		t.Fatalf("Current failed to detect Bzr on rev 2 of branch. Got version: %s", v)
   347  	}
   348  }