-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpc_test.go
200 lines (189 loc) · 5.29 KB
/
pc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package pkgconfig
import (
"bytes"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestExpand(t *testing.T) {
vars := map[string][]byte{
"dupa": []byte("SPARTA"),
"B": []byte("XD"),
"var": []byte(""),
"XD": []byte(" "),
"ABCDEFGHIJKLM": []byte("WAT"),
"TABLE": []byte("┻━┻"),
"WAT": []byte("ಠ_ಠ"),
}
cases := [...][2][]byte{{
[]byte("THIS IS ${dupa}"),
[]byte("THIS IS SPARTA"),
}, {
[]byte("${A} ${B} ${C} ${D}"),
[]byte("${A} XD ${C} ${D}"),
}, {
[]byte("${var}${var}${*}${var}${*}"),
[]byte("${*}${*}"),
}, {
[]byte("$$$$$$$$$$${XD}$$$$$$$$$$$$$$"),
[]byte("$$$$$$$$$$ $$$$$$$$$$$$$$"),
}, {
[]byte("${ABCDEFGHIJKLM}"),
[]byte("WAT"),
}, {
[]byte("(╯°□°)╯︵ ${TABLE}"),
[]byte("(╯°□°)╯︵ ┻━┻"),
}, {
[]byte("${TABLE} ︵ヽ(`o´)ノ︵ ${TABLE}"),
[]byte("┻━┻ ︵ヽ(`o´)ノ︵ ┻━┻"),
}, {
[]byte("x${WAT}x${TABLE}x${X}x"),
[]byte("xಠ_ಠx┻━┻x${X}x"),
}}
for i, cas := range cases {
if p := expand(cas[0], vars); !bytes.Equal(p, cas[1]) {
t.Errorf("expected p=%q; was %q (i=%d)", cas[1], p, i)
}
}
}
var libgit2pc = []byte(`libdir=testdata/libgit2
includedir=testdata/include/libgit2
Name: libgit2
Description: The git library, take 2
Version: 0.20.0
URL: http://libgit2.github.com/
Libs.private: -lrt
Libs: -L${libdir} -lgit2 -Wl,-rpath -Wl,$ORIGIN
Cflags: -I${includedir}`)
var expected = &PC{
Name: "libgit2",
Desc: "The git library, take 2",
Version: "0.20.0",
URL: "http://libgit2.github.com/",
Libs: []string{
"-Ltestdata/libgit2",
"-lgit2",
"-Wl,-rpath",
"-Wl,$ORIGIN",
},
LibsPrivate: []string{
"-lrt",
},
Cflags: []string{
"-Itestdata/include/libgit2",
},
File: filepath.Join("testdata", "libgit2.pc"),
}
func TestNewPC(t *testing.T) {
pc, err := NewPC(bytes.NewBuffer(libgit2pc))
if err != nil {
t.Fatalf("expected err=nil; was %q", err)
}
// NOTE: pc.File is set by Lookup only
pc.File = expected.File
if !reflect.DeepEqual(pc, expected) {
t.Errorf("expected pc=%+v; was %+v", expected, pc)
}
}
func TestNewPCVars(t *testing.T) {
cases := [...]struct {
raw []byte
vars map[string]string
libs []string
}{{
[]byte("libdir=${libdir}/Library\n\nLibs: -L${libdir} -L${libdir}/Default"),
map[string]string{"libdir": "/Users/rjeczalik"},
[]string{"-L/Users/rjeczalik/Library", "-L/Users/rjeczalik/Library/Default"},
}, {
[]byte("libdir=${GOPATH}/lib/${GOOS}_${GOARCH}/libgit2\n\nLibs: -L${libdir}"),
map[string]string{"GOPATH": "/home/rjeczalik", "GOOS": "linux", "GOARCH": "amd64"},
[]string{"-L/home/rjeczalik/lib/linux_amd64/libgit2"},
}, {
[]byte("A=${X}${X}\nX=${A}${X}\nB=${A}${X}${A}\n\nLibs: -L${A} -L${X} -L${B}"),
map[string]string{"X": "★"},
[]string{"-L★★", "-L★★★", "-L★★★★★★★"},
}}
for i, cas := range cases {
pc, err := NewPCVars(bytes.NewBuffer(cas.raw), cas.vars)
if err != nil {
t.Errorf("expected err=nil; was %q (i=%d)", err, i)
}
if !reflect.DeepEqual(pc.Libs, cas.libs) {
t.Errorf("expected pc.Libs=%v; was %v (i=%d)", cas.libs, pc.Libs, i)
}
}
}
func TestNewPCErr(t *testing.T) {
cases := [...][]byte{
[]byte(""),
[]byte(" "),
[]byte("libdir:\n\n"),
[]byte("libdir=/lib\nincludedir:\n\n"),
[]byte("libdir=/lib\nincludedir=/include\n\nCflags=-I${includedir}"),
[]byte("libdir=/lib\nincludedir=/include\n\nCflags:-I${includedir}\nName="),
}
for i, cas := range cases {
if _, err := NewPC(bytes.NewBuffer(cas)); err == nil {
t.Errorf("expected err!=nil (i=%d)", i)
}
}
}
func TestPCWriteTo(t *testing.T) {
var buf bytes.Buffer
cases := [...]struct {
pc *PC
exp []byte
}{{
&PC{Name: "name", Libs: []string{"-A"}, Cflags: []string{"-B", "-C"}},
[]byte("\nName: name\nLibs: -A\nCflags: -B -C\n"),
}, {
&PC{Name: "A", Desc: "B", Version: "C", URL: "D", Libs: []string{"-E"},
LibsPrivate: []string{"-F"}, Cflags: []string{"-G"}, File: "I"},
[]byte("\nName: A\nDescription: B\nVersion: C\nURL: D\nLibs.private: -F\nLibs: -E\nCflags: -G\n"),
}, {
&PC{Libs: []string{"-A", "", ""}, LibsPrivate: []string{"", "-B", ""},
Cflags: []string{"", "", "-C"}},
[]byte("\nLibs.private: -B\nLibs: -A\nCflags: -C\n"),
}}
for i, cas := range cases {
buf.Reset()
if _, err := cas.pc.WriteTo(&buf); err != nil {
t.Errorf("expected err=nil; was %q (i=%d)", err, i)
continue
}
if !bytes.Equal(buf.Bytes(), cas.exp) {
t.Errorf("expected buf=%q; was %q (i=%d)", cas.exp, buf.Bytes(), i)
}
}
}
func TestPCWriteToErr(t *testing.T) {
var buf bytes.Buffer
cases := [...]*PC{
{},
{Libs: []string{""}},
{Cflags: []string{""}},
{Libs: []string{"-A"}},
{Cflags: []string{"-B"}},
{Libs: []string{""}, Cflags: []string{""}},
{Libs: []string{""}, LibsPrivate: []string{"", ""}, Cflags: []string{"", "", ""}},
}
for i, cas := range cases {
buf.Reset()
if _, err := cas.WriteTo(&buf); err == nil {
t.Errorf("expected err!=nil (i=%d, buf=%q)", i, buf.Bytes())
}
}
}
func TestLookupPC(t *testing.T) {
if err := os.Setenv("PKG_CONFIG_PATH", "testdata"); err != nil {
t.Fatalf("expected err=nil; was %q", err)
}
pc, err := LookupPC("libgit2")
if err != nil {
t.Fatalf("expected err=nil; was %q", err)
}
if !reflect.DeepEqual(pc, expected) {
t.Errorf("expected pc=%+v; was %+v", expected, pc)
}
}