forked from kenael/go-dpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
211 lines (188 loc) · 6.21 KB
/
parser_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
201
202
203
204
205
206
207
208
209
210
211
package dpkg
import (
"bytes"
"testing"
)
func assertEqual(t *testing.T, expected string, current string) {
if expected != current {
t.Errorf("Invalid value. Expected '%v', got '%v'", expected, current)
}
}
func TestMapToPackage(t *testing.T) {
parser := NewParser(nil)
m := map[string]string{
"Package": "package",
"Version": "version",
"Section": "section",
"Installed-Size": "123",
"Maintainer": "tadas",
"Status": "status",
"Source": "source",
"Architecture": "amd64",
"Multi-Arch": "same",
"Depends": "depends",
"Pre-Depends": "predepends",
"Description": "desc",
"Homepage": "home",
"Priority": "priority",
}
pkg, err := parser.mapToPackage(m)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
assertEqual(t, "package", pkg.Package)
assertEqual(t, "version", pkg.Version)
assertEqual(t, "section", pkg.Section)
assertEqual(t, "tadas", pkg.Maintainer)
assertEqual(t, "status", pkg.Status)
assertEqual(t, "source", pkg.Source)
assertEqual(t, "amd64", pkg.Architecture)
assertEqual(t, "same", pkg.MultiArch)
assertEqual(t, "depends", pkg.Depends)
assertEqual(t, "predepends", pkg.PreDepends)
assertEqual(t, "desc", pkg.Description)
assertEqual(t, "home", pkg.Homepage)
assertEqual(t, "priority", pkg.Priority)
if pkg.InstalledSize != 123 {
t.Errorf("Invalid size: %v", pkg.InstalledSize)
}
}
func TestParseLineHandlesEmptyString(t *testing.T) {
parser := NewParser(nil)
key, value := parser.parseLine("")
assertEqual(t, "", key)
assertEqual(t, "", value)
}
func TestParseLineHandlesNewLine(t *testing.T) {
parser := NewParser(nil)
key, value := parser.parseLine("\n")
assertEqual(t, "", key)
assertEqual(t, "", value)
}
func TestParseLineHandlesMultilineValue(t *testing.T) {
parser := NewParser(nil)
key, value := parser.parseLine(" some: value\n")
assertEqual(t, "", key)
assertEqual(t, " some: value", value)
}
func TestParseLineHandlesKeyValue(t *testing.T) {
parser := NewParser(nil)
key, value := parser.parseLine("Key: value\n")
assertEqual(t, "Key", key)
assertEqual(t, " value", value)
}
func TestParseAllBlankLines(t *testing.T) {
data := "\n\n\n\n\n"
reader := bytes.NewBufferString(data)
parser := NewParser(reader)
packages := parser.Parse()
if len(packages) != 0 {
t.Errorf("Expected 0 packages, got: %v", len(packages))
}
}
func TestParseValidData(t *testing.T) {
data := `Package: libquadmath0
Status: install ok installed
Priority: optional
Section: libs
Installed-Size: 275
Maintainer: Debian GCC Maintainers <[email protected]>
Architecture: amd64
Multi-Arch: same
Source: gcc-4.9
Version: 4.9.2-10
Depends: gcc-4.9-base (= 4.9.2-10), libc6 (>= 2.14)
Pre-Depends: multiarch-support
Description: GCC Quad-Precision Math Library
A library, which provides quad-precision mathematical functions on targets
supporting the __float128 datatype. The library is used to provide on such
targets the REAL(16) type in the GNU Fortran compiler.
* Example Test Entry (Ubuntu Style)
Homepage: http://gcc.gnu.org/
Package: netbase
Status: install ok installed
Priority: important
Section: admin
Installed-Size: 44
Maintainer: Ubuntu Developers <[email protected]>
Architecture: all
Multi-Arch: foreign
Version: 5.4
Conffiles:
/etc/protocols bb9c019d6524e913fd72441d58b68216
/etc/rpc f0b6f6352bf886623adc04183120f83b
/etc/services 567c100888518c1163b3462993de7d47
Description: Basic TCP/IP networking system
This package provides the necessary infrastructure for basic TCP/IP based
networking.
Original-Maintainer: Marco d'Itri <[email protected]>
Package: libedit2
Status: install ok installed
Priority: standard
Section: libs
Installed-Size: 277
Maintainer: LLVM Packaging Team <[email protected]>
Architecture: amd64
Multi-Arch: same
Source: libedit
Version: 3.1-20140620-2
Depends: libbsd0 (>= 0.0), libc6 (>= 2.17), libtinfo5
Pre-Depends: multiarch-support
Description: BSD editline and history libraries
Command line editor library provides generic line editing,
history, and tokenization functions.
.
It slightly resembles GNU readline.
Homepage: http://www.thrysoee.dk/editline/
`
reader := bytes.NewBufferString(data)
parser := NewParser(reader)
packages := parser.Parse()
if len(packages) != 3 {
t.Errorf("Expected 3 packages, got: %v", len(packages))
}
pkg := packages[0]
assertEqual(t, "libquadmath0", pkg.Package)
assertEqual(t, "4.9.2-10", pkg.Version)
assertEqual(t, "libs", pkg.Section)
assertEqual(t, "Debian GCC Maintainers <[email protected]>", pkg.Maintainer)
assertEqual(t, "install ok installed", pkg.Status)
assertEqual(t, "gcc-4.9", pkg.Source)
assertEqual(t, "amd64", pkg.Architecture)
assertEqual(t, "same", pkg.MultiArch)
assertEqual(t, "gcc-4.9-base (= 4.9.2-10), libc6 (>= 2.14)", pkg.Depends)
assertEqual(t, "multiarch-support", pkg.PreDepends)
assertEqual(t, "http://gcc.gnu.org/", pkg.Homepage)
assertEqual(t, "optional", pkg.Priority)
if pkg.InstalledSize != 275 {
t.Errorf("Incorrect size: %v", pkg.InstalledSize)
}
pkg = packages[1]
assertEqual(t, "netbase", pkg.Package)
assertEqual(t, "5.4", pkg.Version)
assertEqual(t, "admin", pkg.Section)
assertEqual(t, "Ubuntu Developers <[email protected]>", pkg.Maintainer)
assertEqual(t, "install ok installed", pkg.Status)
assertEqual(t, "all", pkg.Architecture)
assertEqual(t, "foreign", pkg.MultiArch)
assertEqual(t, "important", pkg.Priority)
if pkg.InstalledSize != 44 {
t.Errorf("Incorrect size: %v", pkg.InstalledSize)
}
pkg = packages[2]
assertEqual(t, "libedit2", pkg.Package)
assertEqual(t, "3.1-20140620-2", pkg.Version)
assertEqual(t, "libs", pkg.Section)
assertEqual(t, "LLVM Packaging Team <[email protected]>", pkg.Maintainer)
assertEqual(t, "install ok installed", pkg.Status)
assertEqual(t, "libedit", pkg.Source)
assertEqual(t, "amd64", pkg.Architecture)
assertEqual(t, "same", pkg.MultiArch)
assertEqual(t, "libbsd0 (>= 0.0), libc6 (>= 2.17), libtinfo5", pkg.Depends)
assertEqual(t, "multiarch-support", pkg.PreDepends)
assertEqual(t, "http://www.thrysoee.dk/editline/", pkg.Homepage)
assertEqual(t, "standard", pkg.Priority)
if pkg.InstalledSize != 277 {
t.Errorf("Incorrect size: %v", pkg.InstalledSize)
}
}