github.com/golang/dep@v0.5.4/gps/lock_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  	"reflect"
     9  	"sort"
    10  	"testing"
    11  )
    12  
    13  func TestLockedProjectSorting(t *testing.T) {
    14  	// version doesn't matter here
    15  	lps := []LockedProject{
    16  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), nil),
    17  		NewLockedProject(mkPI("foo"), NewVersion("nada"), nil),
    18  		NewLockedProject(mkPI("bar"), NewVersion("zip"), nil),
    19  		NewLockedProject(mkPI("qux"), NewVersion("zilch"), nil),
    20  	}
    21  	lps2 := make([]LockedProject, len(lps))
    22  	copy(lps2, lps)
    23  
    24  	sort.SliceStable(lps2, func(i, j int) bool {
    25  		return lps2[i].Ident().Less(lps2[j].Ident())
    26  	})
    27  
    28  	// only the two should have switched positions
    29  	lps[0], lps[2] = lps[2], lps[0]
    30  	if !reflect.DeepEqual(lps, lps2) {
    31  		t.Errorf("SortLockedProject did not sort as expected:\n\t(GOT) %s\n\t(WNT) %s", lps2, lps)
    32  	}
    33  }
    34  
    35  func TestLockedProjectsEq(t *testing.T) {
    36  	lps := []LockedProject{
    37  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), []string{"gps"}),
    38  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), nil),
    39  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), []string{"gps", "flugle"}),
    40  		NewLockedProject(mkPI("foo"), NewVersion("nada").Pair("OTHERREV"), []string{"foo"}),
    41  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV"), []string{"flugle", "gps"}),
    42  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("REV2"), []string{"gps"}),
    43  		NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.11.0").Pair("REV"), []string{"gps"}),
    44  		NewLockedProject(mkPI("github.com/sdboyer/gps"), Revision("REV2"), []string{"gps"}),
    45  	}
    46  
    47  	fix := map[string]struct {
    48  		l1, l2   int
    49  		shouldeq bool
    50  		err      string
    51  	}{
    52  		"with self":               {0, 0, true, "lp does not eq self"},
    53  		"with different revision": {0, 5, false, "should not eq with different rev"},
    54  		"with different versions": {0, 6, false, "should not eq with different version"},
    55  		"with same revsion":       {5, 5, true, "should eq with same rev"},
    56  		"with empty pkg":          {0, 1, false, "should not eq when other pkg list is empty"},
    57  		"with long pkg list":      {0, 2, false, "should not eq when other pkg list is longer"},
    58  		"with different orders":   {2, 4, false, "should not eq when pkg lists are out of order"},
    59  		"with different lp":       {0, 3, false, "should not eq totally different lp"},
    60  		"with only rev":           {7, 7, true, "should eq with only rev"},
    61  		"when only rev matches":   {5, 7, false, "should not eq when only rev matches"},
    62  	}
    63  
    64  	for k, f := range fix {
    65  		k, f := k, f
    66  		t.Run(k, func(t *testing.T) {
    67  			if f.shouldeq {
    68  				if !lps[f.l1].Eq(lps[f.l2]) {
    69  					t.Error(f.err)
    70  				}
    71  				if !lps[f.l2].Eq(lps[f.l1]) {
    72  					t.Error(f.err + (" (reversed)"))
    73  				}
    74  			} else {
    75  				if lps[f.l1].Eq(lps[f.l2]) {
    76  					t.Error(f.err)
    77  				}
    78  				if lps[f.l2].Eq(lps[f.l1]) {
    79  					t.Error(f.err + (" (reversed)"))
    80  				}
    81  			}
    82  		})
    83  	}
    84  }
    85  
    86  func TestLockedProjectsString(t *testing.T) {
    87  	tt := []struct {
    88  		name string
    89  		lp   LockedProject
    90  		want string
    91  	}{
    92  		{
    93  			name: "full info",
    94  			lp:   NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps"}),
    95  			want: "github.com/sdboyer/gps@v0.10.0 with packages: [gps]",
    96  		},
    97  		{
    98  			name: "empty package list",
    99  			lp:   NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{}),
   100  			want: "github.com/sdboyer/gps@v0.10.0 with packages: []",
   101  		},
   102  		{
   103  			name: "nil package",
   104  			lp:   NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), nil),
   105  			want: "github.com/sdboyer/gps@v0.10.0 with packages: []",
   106  		},
   107  		{
   108  			name: "with source",
   109  			lp: NewLockedProject(
   110  				ProjectIdentifier{ProjectRoot: "github.com/sdboyer/gps", Source: "github.com/another/repo"},
   111  				NewVersion("v0.10.0"), []string{"."}),
   112  			want: "github.com/sdboyer/gps (from github.com/another/repo)@v0.10.0 with packages: [.]",
   113  		},
   114  		{
   115  			name: "version pair",
   116  			lp:   NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Pair("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}),
   117  			want: "github.com/sdboyer/gps@v0.10.0 with packages: [gps]",
   118  		},
   119  		{
   120  			name: "revision only",
   121  			lp:   NewLockedProject(mkPI("github.com/sdboyer/gps"), Revision("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}),
   122  			want: "github.com/sdboyer/gps@278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0 with packages: [gps]",
   123  		},
   124  	}
   125  
   126  	for _, tc := range tt {
   127  		t.Run(tc.name, func(t *testing.T) {
   128  			s := tc.lp.String()
   129  			if tc.want != s {
   130  				t.Fatalf("want %s, got %s", tc.want, s)
   131  			}
   132  		})
   133  	}
   134  
   135  }