github.com/sdboyer/gps@v0.16.3/lock_test.go (about) 1 package gps 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestLockedProjectSorting(t *testing.T) { 9 // version doesn't matter here 10 lps := []LockedProject{ 11 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), nil), 12 NewLockedProject(mkPI("foo"), NewVersion("nada"), nil), 13 NewLockedProject(mkPI("bar"), NewVersion("zip"), nil), 14 NewLockedProject(mkPI("qux"), NewVersion("zilch"), nil), 15 } 16 lps2 := make([]LockedProject, len(lps)) 17 copy(lps2, lps) 18 19 SortLockedProjects(lps2) 20 21 // only the two should have switched positions 22 lps[0], lps[2] = lps[2], lps[0] 23 if !reflect.DeepEqual(lps, lps2) { 24 t.Errorf("SortLockedProject did not sort as expected:\n\t(GOT) %s\n\t(WNT) %s", lps2, lps) 25 } 26 } 27 28 func TestLockedProjectsEq(t *testing.T) { 29 lps := []LockedProject{ 30 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps"}), 31 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), nil), 32 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps", "flugle"}), 33 NewLockedProject(mkPI("foo"), NewVersion("nada"), []string{"foo"}), 34 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"flugle", "gps"}), 35 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Is("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}), 36 NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.11.0"), []string{"gps"}), 37 NewLockedProject(mkPI("github.com/sdboyer/gps"), Revision("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}), 38 } 39 40 fix := map[string]struct { 41 l1, l2 int 42 shouldeq bool 43 err string 44 }{ 45 "with self": {0, 0, true, "lp does not eq self"}, 46 "with different revision": {0, 5, false, "should not eq with different rev"}, 47 "with different versions": {0, 6, false, "should not eq with different version"}, 48 "with same revsion": {5, 5, true, "should eq with same rev"}, 49 "with empty pkg": {0, 1, false, "should not eq when other pkg list is empty"}, 50 "with long pkg list": {0, 2, false, "should not eq when other pkg list is longer"}, 51 "with different orders": {2, 4, false, "should not eq when pkg lists are out of order"}, 52 "with different lp": {0, 3, false, "should not eq totally different lp"}, 53 "with only rev": {7, 7, true, "should eq with only rev"}, 54 "when only rev matches": {5, 7, false, "should not eq when only rev matches"}, 55 } 56 57 for k, f := range fix { 58 k, f := k, f 59 t.Run(k, func(t *testing.T) { 60 if f.shouldeq { 61 if !lps[f.l1].Eq(lps[f.l2]) { 62 t.Error(f.err) 63 } 64 if !lps[f.l2].Eq(lps[f.l1]) { 65 t.Error(f.err + (" (reversed)")) 66 } 67 } else { 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 } 75 }) 76 } 77 } 78 79 func TestLocksAreEq(t *testing.T) { 80 gpl := NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0").Is("278a227dfc3d595a33a77ff3f841fd8ca1bc8cd0"), []string{"gps"}) 81 svpl := NewLockedProject(mkPI("github.com/Masterminds/semver"), NewVersion("v2.0.0"), []string{"semver"}) 82 bbbt := NewLockedProject(mkPI("github.com/beeblebrox/browntown"), NewBranch("master").Is("63fc17eb7966a6f4cc0b742bf42731c52c4ac740"), []string{"browntown", "smoochies"}) 83 84 l1 := solution{ 85 hd: []byte("foo"), 86 p: []LockedProject{ 87 gpl, 88 bbbt, 89 svpl, 90 }, 91 } 92 93 l2 := solution{ 94 p: []LockedProject{ 95 svpl, 96 gpl, 97 }, 98 } 99 100 if LocksAreEq(l1, l2, true) { 101 t.Fatal("should have failed on hash check") 102 } 103 104 if LocksAreEq(l1, l2, false) { 105 t.Fatal("should have failed on length check") 106 } 107 108 l2.p = append(l2.p, bbbt) 109 110 if !LocksAreEq(l1, l2, false) { 111 t.Fatal("should be eq, must have failed on individual lp check") 112 } 113 114 // ensure original input sort order is maintained 115 if !l1.p[0].Eq(gpl) { 116 t.Error("checking equality resorted l1") 117 } 118 if !l2.p[0].Eq(svpl) { 119 t.Error("checking equality resorted l2") 120 } 121 122 l1.p[0] = NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.11.0"), []string{"gps"}) 123 if LocksAreEq(l1, l2, false) { 124 t.Error("should fail when individual lp were not eq") 125 } 126 }