Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit c902383

Browse files
committed
Add tests for writing locks
1 parent 166b616 commit c902383

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

lock_test.go

+55-21
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,46 @@
55
package main
66

77
import (
8+
"bytes"
89
"encoding/hex"
10+
"encoding/json"
911
"reflect"
1012
"strings"
1113
"testing"
1214

1315
"github.com/sdboyer/gps"
1416
)
1517

16-
func TestReadLock(t *testing.T) {
17-
const le = `{
18-
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
19-
"projects": [
20-
{
21-
"name": "github.com/sdboyer/gps",
22-
"branch": "master",
18+
const le = `{
19+
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
20+
"projects": [
21+
{
22+
"name": "github.com/sdboyer/gps",
23+
"branch": "master",
2324
"version": "v0.12.0",
24-
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
25-
"packages": ["."]
26-
}
27-
]
25+
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
26+
"packages": [
27+
"."
28+
]
29+
}
30+
]
2831
}`
29-
const lg = `{
30-
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
31-
"projects": [
32-
{
33-
"name": "github.com/sdboyer/gps",
34-
"branch": "master",
35-
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
36-
"packages": ["."]
37-
}
38-
]
32+
33+
const lg = `{
34+
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
35+
"projects": [
36+
{
37+
"name": "github.com/sdboyer/gps",
38+
"branch": "master",
39+
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
40+
"packages": [
41+
"."
42+
]
43+
}
44+
]
3945
}`
4046

47+
func TestReadLock(t *testing.T) {
4148
_, err := readLock(strings.NewReader(le))
4249
if err == nil {
4350
t.Error("Reading lock with invalid props should have caused error, but did not")
@@ -66,3 +73,30 @@ func TestReadLock(t *testing.T) {
6673
t.Error("Valid lock did not parse as expected")
6774
}
6875
}
76+
77+
func TestWriteLock(t *testing.T) {
78+
memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
79+
l := &lock{
80+
Memo: memo,
81+
P: []gps.LockedProject{
82+
gps.NewLockedProject(
83+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/gps")},
84+
gps.NewBranch("master").Is(gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb")),
85+
[]string{"."},
86+
),
87+
},
88+
}
89+
90+
b, err := json.Marshal(l)
91+
if err != nil {
92+
t.Fatalf("Error while marshaling valid lock to JSON: %q", err)
93+
}
94+
95+
var out bytes.Buffer
96+
json.Indent(&out, b, "", "\t")
97+
98+
s := out.String()
99+
if s != lg {
100+
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", s, lg)
101+
}
102+
}

0 commit comments

Comments
 (0)