@@ -18,14 +18,7 @@ func TestReadLock(t *testing.T) {
1818 h := test .NewHelper (t )
1919 defer h .Cleanup ()
2020
21- _ , err := readLock (h .GetTestFile ("lock/error.json" ))
22- if err == nil {
23- t .Error ("Reading lock with invalid props should have caused error, but did not" )
24- } else if ! strings .Contains (err .Error (), "both a branch" ) {
25- t .Errorf ("Unexpected error %q; expected multiple version error" , err )
26- }
27-
28- golden := "lock/golden.json"
21+ golden := "lock/golden0.json"
2922 l , err := readLock (h .GetTestFile (golden ))
3023 if err != nil {
3124 t .Fatalf ("Should have read Lock correctly, but got err %q" , err )
@@ -45,15 +38,36 @@ func TestReadLock(t *testing.T) {
4538
4639 if ! reflect .DeepEqual (l , l2 ) {
4740 t .Error ("Valid lock did not parse as expected" )
41+ }
42+
43+ golden = "lock/golden1.json"
44+ l , err = readLock (h .GetTestFile (golden ))
45+ if err != nil {
46+ t .Fatalf ("Should have read Lock correctly, but got err %q" , err )
47+ }
48+
49+ b , _ = hex .DecodeString ("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e" )
50+ l2 = & Lock {
51+ Memo : b ,
52+ P : []gps.LockedProject {
53+ gps .NewLockedProject (
54+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/sdboyer/gps" )},
55+ gps .NewVersion ("0.12.2" ).Is (gps .Revision ("d05d5aca9f895d19e9265839bffeadd74a2d2ecb" )),
56+ []string {"." },
57+ ),
58+ },
59+ }
4860
61+ if ! reflect .DeepEqual (l , l2 ) {
62+ t .Error ("Valid lock did not parse as expected" )
4963 }
5064}
5165
5266func TestWriteLock (t * testing.T ) {
5367 h := test .NewHelper (t )
5468 defer h .Cleanup ()
5569
56- golden := "lock/golden .json"
70+ golden := "lock/golden0 .json"
5771 lg := h .GetTestFileString (golden )
5872 memo , _ := hex .DecodeString ("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e" )
5973 l := & Lock {
@@ -81,4 +95,79 @@ func TestWriteLock(t *testing.T) {
8195 t .Errorf ("Valid lock did not marshal to JSON as expected:\n \t (GOT): %s\n \t (WNT): %s" , lg , string (b ))
8296 }
8397 }
98+
99+ golden = "lock/golden1.json"
100+ lg = h .GetTestFileString (golden )
101+ memo , _ = hex .DecodeString ("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e" )
102+ l = & Lock {
103+ Memo : memo ,
104+ P : []gps.LockedProject {
105+ gps .NewLockedProject (
106+ gps.ProjectIdentifier {ProjectRoot : gps .ProjectRoot ("github.com/sdboyer/gps" )},
107+ gps .NewVersion ("0.12.2" ).Is (gps .Revision ("d05d5aca9f895d19e9265839bffeadd74a2d2ecb" )),
108+ []string {"." },
109+ ),
110+ },
111+ }
112+
113+ b , err = l .MarshalJSON ()
114+ if err != nil {
115+ t .Fatalf ("Error while marshaling valid lock to JSON: %q" , err )
116+ }
117+
118+ if string (b ) != lg {
119+ if * test .UpdateGolden {
120+ if err = h .WriteTestFile (golden , string (b )); err != nil {
121+ t .Fatal (err )
122+ }
123+ } else {
124+ t .Errorf ("Valid lock did not marshal to JSON as expected:\n \t (GOT): %s\n \t (WNT): %s" , lg , string (b ))
125+ }
126+ }
127+ }
128+
129+ func TestReadLockErrors (t * testing.T ) {
130+ h := test .NewHelper (t )
131+ defer h .Cleanup ()
132+ var err error
133+
134+ tests := []struct {
135+ name string
136+ file string
137+ }{
138+ {"specified both" , "lock/error0.json" },
139+ {"invalid hash" , "lock/error1.json" },
140+ {"no branch or version" , "lock/error2.json" },
141+ }
142+
143+ for _ , tst := range tests {
144+ _ , err = readLock (h .GetTestFile (tst .file ))
145+ if err == nil {
146+ t .Errorf ("Reading lock with %s should have caused error, but did not" , tst .name )
147+ } else if ! strings .Contains (err .Error (), tst .name ) {
148+ t .Errorf ("Unexpected error %q; expected %s error" , err , tst .name )
149+ }
150+ }
151+
152+ // _, err = readLock(h.GetTestFile("lock/error0.json"))
153+ // if err == nil {
154+ // t.Error("Reading lock with invalid props should have caused error, but did not")
155+ // } else if !strings.Contains(err.Error(), "both a branch") {
156+ // t.Errorf("Unexpected error %q; expected multiple version error", err)
157+ // }
158+ //
159+ // _, err = readLock(h.GetTestFile("lock/error1.json"))
160+ // if err == nil {
161+ // t.Error("Reading lock with invalid hash should have caused error, but did not")
162+ // } else if !strings.Contains(err.Error(), "invalid hash") {
163+ // t.Errorf("Unexpected error %q; expected invalid hash error", err)
164+ // }
165+ //
166+ // _, err = readLock(h.GetTestFile("lock/error2.json"))
167+ // if err == nil {
168+ // t.Error("Reading lock with invalid props should have caused error, but did not")
169+ // } else if !strings.Contains(err.Error(), "no version") {
170+ // t.Errorf("Unexpected error %q; expected no version error", err)
171+ // }
172+
84173}
0 commit comments