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

     1  package gps
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/sdboyer/gps/pkgtree"
    11  )
    12  
    13  // Executed in parallel by TestSlowVcs
    14  func testSourceGateway(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	if testing.Short() {
    18  		t.Skip("Skipping gateway testing in short mode")
    19  	}
    20  	requiresBins(t, "git")
    21  
    22  	cachedir, err := ioutil.TempDir("", "smcache")
    23  	if err != nil {
    24  		t.Fatalf("failed to create temp dir: %s", err)
    25  	}
    26  	bgc := context.Background()
    27  	ctx, cancelFunc := context.WithCancel(bgc)
    28  	defer func() {
    29  		removeAll(cachedir)
    30  		cancelFunc()
    31  	}()
    32  
    33  	do := func(wantstate sourceState) func(t *testing.T) {
    34  		return func(t *testing.T) {
    35  			superv := newSupervisor(ctx)
    36  			sc := newSourceCoordinator(superv, newDeductionCoordinator(superv), cachedir)
    37  
    38  			id := mkPI("github.com/sdboyer/deptest")
    39  			sg, err := sc.getSourceGatewayFor(ctx, id)
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  
    44  			if _, ok := sg.src.(*gitSource); !ok {
    45  				t.Fatalf("Expected a gitSource, got a %T", sg.src)
    46  			}
    47  
    48  			if sg.srcState != wantstate {
    49  				t.Fatalf("expected state on initial create to be %v, got %v", wantstate, sg.srcState)
    50  			}
    51  
    52  			if err := sg.syncLocal(ctx); err != nil {
    53  				t.Fatalf("error on cloning git repo: %s", err)
    54  			}
    55  
    56  			cvlist := sg.cache.getAllVersions()
    57  			if len(cvlist) != 4 {
    58  				t.Fatalf("repo setup should've cached four versions, got %v: %s", len(cvlist), cvlist)
    59  			}
    60  
    61  			wanturl := "https://" + id.normalizedSource()
    62  			goturl, err := sg.sourceURL(ctx)
    63  			if err != nil {
    64  				t.Fatalf("got err from sourceURL: %s", err)
    65  			}
    66  			if wanturl != goturl {
    67  				t.Fatalf("Expected %s as source URL, got %s", wanturl, goturl)
    68  			}
    69  
    70  			vlist, err := sg.listVersions(ctx)
    71  			if err != nil {
    72  				t.Fatalf("Unexpected error getting version pairs from git repo: %s", err)
    73  			}
    74  
    75  			if len(vlist) != 4 {
    76  				t.Fatalf("git test repo should've produced four versions, got %v: vlist was %s", len(vlist), vlist)
    77  			} else {
    78  				SortPairedForUpgrade(vlist)
    79  				evl := []PairedVersion{
    80  					NewVersion("v1.0.0").Is(Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf")),
    81  					NewVersion("v0.8.1").Is(Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f")),
    82  					NewVersion("v0.8.0").Is(Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf")),
    83  					newDefaultBranch("master").Is(Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f")),
    84  				}
    85  				if !reflect.DeepEqual(vlist, evl) {
    86  					t.Fatalf("Version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
    87  				}
    88  			}
    89  
    90  			rev := Revision("c575196502940c07bf89fd6d95e83b999162e051")
    91  			// check that an expected rev is not in cache
    92  			_, has := sg.cache.getVersionsFor(rev)
    93  			if has {
    94  				t.Fatal("shouldn't have bare revs in cache without specifically requesting them")
    95  			}
    96  
    97  			is, err := sg.revisionPresentIn(ctx, Revision("c575196502940c07bf89fd6d95e83b999162e051"))
    98  			if err != nil {
    99  				t.Fatalf("unexpected error while checking revision presence: %s", err)
   100  			} else if !is {
   101  				t.Fatalf("revision that should exist was not present")
   102  			}
   103  
   104  			// check that an expected rev is not in cache
   105  			_, has = sg.cache.getVersionsFor(rev)
   106  			if !has {
   107  				t.Fatal("bare rev should be in cache after specific request for it")
   108  			}
   109  
   110  			// Ensure that a bad rev doesn't work on any method that takes
   111  			// versions
   112  			badver := NewVersion("notexist")
   113  			wanterr := fmt.Errorf("version %q does not exist in source", badver)
   114  
   115  			_, _, err = sg.getManifestAndLock(ctx, ProjectRoot("github.com/sdboyer/deptest"), badver, naiveAnalyzer{})
   116  			if err == nil {
   117  				t.Fatal("wanted err on nonexistent version")
   118  			} else if err.Error() != wanterr.Error() {
   119  				t.Fatalf("wanted nonexistent err when passing bad version, got: %s", err)
   120  			}
   121  
   122  			_, err = sg.listPackages(ctx, ProjectRoot("github.com/sdboyer/deptest"), badver)
   123  			if err == nil {
   124  				t.Fatal("wanted err on nonexistent version")
   125  			} else if err.Error() != wanterr.Error() {
   126  				t.Fatalf("wanted nonexistent err when passing bad version, got: %s", err)
   127  			}
   128  
   129  			err = sg.exportVersionTo(ctx, badver, cachedir)
   130  			if err == nil {
   131  				t.Fatal("wanted err on nonexistent version")
   132  			} else if err.Error() != wanterr.Error() {
   133  				t.Fatalf("wanted nonexistent err when passing bad version, got: %s", err)
   134  			}
   135  
   136  			wantptree := pkgtree.PackageTree{
   137  				ImportRoot: "github.com/sdboyer/deptest",
   138  				Packages: map[string]pkgtree.PackageOrErr{
   139  					"github.com/sdboyer/deptest": pkgtree.PackageOrErr{
   140  						P: pkgtree.Package{
   141  							ImportPath: "github.com/sdboyer/deptest",
   142  							Name:       "deptest",
   143  							Imports:    []string{},
   144  						},
   145  					},
   146  				},
   147  			}
   148  
   149  			ptree, err := sg.listPackages(ctx, ProjectRoot("github.com/sdboyer/deptest"), Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"))
   150  			if err != nil {
   151  				t.Fatalf("unexpected err when getting package tree with known rev: %s", err)
   152  			}
   153  			if !reflect.DeepEqual(wantptree, ptree) {
   154  				t.Fatalf("got incorrect PackageTree:\n\t(GOT): %#v\n\t(WNT): %#v", wantptree, ptree)
   155  			}
   156  
   157  			ptree, err = sg.listPackages(ctx, ProjectRoot("github.com/sdboyer/deptest"), NewVersion("v1.0.0"))
   158  			if err != nil {
   159  				t.Fatalf("unexpected err when getting package tree with unpaired good version: %s", err)
   160  			}
   161  			if !reflect.DeepEqual(wantptree, ptree) {
   162  				t.Fatalf("got incorrect PackageTree:\n\t(GOT): %#v\n\t(WNT): %#v", wantptree, ptree)
   163  			}
   164  		}
   165  	}
   166  
   167  	// Run test twice so that we cover both the existing and non-existing case;
   168  	// only difference in results is the initial setup state.
   169  	t.Run("empty", do(sourceIsSetUp|sourceExistsUpstream|sourceHasLatestVersionList))
   170  	t.Run("exists", do(sourceIsSetUp|sourceExistsLocally|sourceExistsUpstream|sourceHasLatestVersionList))
   171  }