github.com/sdboyer/gps@v0.16.3/vcs_repo_test.go (about)

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