forked from remyoudompheng/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
package_test.go
123 lines (104 loc) · 2.99 KB
/
package_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
// package_test.go - Tests for package.go
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm_test
import (
"bytes"
"testing"
"text/template"
"time"
alpm "github.com/Jguer/go-alpm/v2"
"github.com/stretchr/testify/assert"
)
// Auxiliary formatting
const pkginfoTemplate = `
Name : {{ .Name }}
Version : {{ .Version }}
Architecture : {{ .Architecture }}
Description : {{ .Description }}
URL : {{ .URL }}
Groups : {{ .Groups.Slice }}
Licenses : {{ .Licenses.Slice }}
Dependencies : {{ range .Depends.Slice }}{{ . }} {{ end }}
Provides : {{ range .Provides.Slice }}{{ . }} {{ end }}
Replaces : {{ range .Replaces.Slice }}{{ . }} {{ end }}
Conflicts : {{ range .Conflicts.Slice }}{{ . }} {{ end }}
Packager : {{ .Packager }}
Build Date : {{ .PrettyBuildDate }}
Install Date : {{ .PrettyInstallDate }}
Package Size : {{ .Size }} bytes
Install Size : {{ .ISize }} bytes
MD5 Sum : {{ .MD5Sum }}
SHA256 Sum : {{ .SHA256Sum }}
Reason : {{ .Reason }}
Required By : {{ .ComputeRequiredBy }}
Files : {{ range .Files }}
{{ .Name }} {{ .Size }}{{ end }}
`
type PrettyPackage struct {
*alpm.Package
}
func (p PrettyPackage) PrettyBuildDate() string {
return p.Package.BuildDate().Format(time.RFC1123)
}
func (p PrettyPackage) PrettyInstallDate() string {
return p.Package.InstallDate().Format(time.RFC1123)
}
// Tests package attribute getters.
func TestPkginfo(t *testing.T) {
t.Parallel()
pkginfoTemp, er := template.New("info").Parse(pkginfoTemplate)
assert.NoError(t, er, "couldn't compile template")
h, er := alpm.Initialize(root, dbpath)
defer h.Release()
if er != nil {
t.Errorf("Failed at alpm initialization: %s", er)
}
t.Log("Printing package information for pacman")
db, _ := h.LocalDB()
pkg := db.Pkg("glibc")
buf := bytes.NewBuffer(nil)
pkginfoTemp.Execute(buf, PrettyPackage{pkg.(*alpm.Package)})
t.Logf("%s...", buf.Bytes()[:1024])
t.Logf("Should ignore %t", pkg.ShouldIgnore())
pkg = db.Pkg("linux")
if pkg != nil {
buf = bytes.NewBuffer(nil)
pkginfoTemp.Execute(buf, PrettyPackage{pkg.(*alpm.Package)})
t.Logf("%s...", buf.Bytes()[:1024])
t.Logf("Should ignore %t", pkg.ShouldIgnore())
}
}
func TestPkgNoExist(t *testing.T) {
t.Parallel()
h, er := alpm.Initialize(root, dbpath)
defer h.Release()
if er != nil {
t.Errorf("Failed at alpm initialization: %s", er)
}
db, _ := h.LocalDB()
pkg := db.Pkg("non-existing-package-fa93f4af")
if pkg != nil {
t.Errorf("pkg should be nil but got %v", pkg)
}
}
func TestPkgFiles(t *testing.T) {
t.Parallel()
h, er := alpm.Initialize(root, dbpath)
defer h.Release()
if er != nil {
t.Errorf("Failed at alpm initialization: %s", er)
}
db, _ := h.LocalDB()
pkg := db.Pkg("glibc")
_, err := pkg.ContainsFile("etc/locale.gen")
if err != nil {
t.Errorf("File should not be nil but got %v", err)
}
_, err = pkg.ContainsFile("etc/does-not-exist")
if err == nil {
t.Errorf("File should be nil but got %v", err)
}
}