-
Notifications
You must be signed in to change notification settings - Fork 9
/
debpkg_test.go
163 lines (132 loc) · 4.37 KB
/
debpkg_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
// Copyright 2017 Debpkg authors. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package debpkg
import (
"fmt"
"go/build"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xor-gate/debpkg/internal/test"
)
// testWrite writes the deb package to a temporary file and verifies with native dpkg tool when available
func testWrite(t *testing.T, deb *DebPkg) error {
f := test.TempFile(t)
err := deb.Write(f)
if err != nil {
return err
}
err = testReadWithLintian(t, f)
if err != nil {
return err
}
testReadWithNativeDpkg(t, f)
return nil
}
// testReadWithNativeDpkg tests a single debian package with the dpkg tool when present
func testReadWithNativeDpkg(t *testing.T, filename string) {
dpkgCmd, err := exec.LookPath("dpkg")
if err != nil || dpkgCmd == "" {
return
}
dpkg := func(action, filename string) error {
return exec.Command(dpkgCmd, "--"+action, filename).Run()
}
// TODO test control, extract, verify...
assert.Nil(t, dpkg("info", filename))
assert.Nil(t, dpkg("contents", filename))
}
func testReadWithLintian(t *testing.T, filename string) error {
lintianCmd, err := exec.LookPath("lintian")
if err != nil || lintianCmd == "" {
return nil
}
lintian := func(filename string) error {
// For now we don't fail on warning or errors (yet)
//return exec.Command(lintianCmd, "--fail-on", "warning,error", filename).Run()
return exec.Command(lintianCmd, "--fail-on", "pedantic", filename).Run()
}
err = lintian(filename)
assert.Nil(t, err)
return err
}
// TestDirectory verifies adding a single directory recursive to the package
func TestAddDirectory(t *testing.T) {
deb := New()
defer deb.Close()
deb.SetName("debpkg-test-add-directory")
deb.SetArchitecture("all")
assert.Nil(t, deb.AddDirectory("internal"))
assert.Nil(t, testWrite(t, deb))
}
// TestWrite verifies Write works as expected with adding just one datafile
func TestWrite(t *testing.T) {
deb := New()
defer deb.Close()
deb.SetName("debpkg-test")
deb.SetArchitecture("all")
deb.SetVersion("0.0.1")
deb.SetMaintainer("Foo Bar")
deb.SetMaintainerEmail("[email protected]")
deb.SetHomepage("https://foobar.com")
deb.SetShortDescription("some awesome foobar pkg")
deb.SetDescription("very very very very long description")
// Set version control system info for control file
deb.SetVcsType(VcsTypeGit)
deb.SetVcsURL("https://github.com/xor-gate/secdl")
deb.SetVcsBrowser("https://github.com/xor-gate/secdl")
deb.AddFile("debpkg.go")
deb.AddFile("debpkg_test.go", "/foo/awesome/test.go")
deb.AddFileString("this is a real file", "/real/file.txt")
assert.Nil(t, testWrite(t, deb))
// Try to Write again on implicit closed package
assert.Equal(t, ErrClosed, testWrite(t, deb))
}
// TestWriteError tests if the Write fails with the correct errors
func TestWriteError(t *testing.T) {
deb := New()
defer deb.Close()
assert.NotNil(t, deb.Write(""), "deb.Write should return nil")
deb.control.info.name = "pkg"
assert.Equal(t, fmt.Errorf("empty package name"), deb.Write(""))
}
// ExampleDebPkgWrite demonstrates generating a simple package
func ExampleDebPkg_Write() {
tempfile := os.TempDir() + "/foobar.deb"
deb := New()
defer deb.Close()
deb.SetName("foobar")
deb.SetVersion("1.2.3")
deb.SetArchitecture("amd64")
deb.SetMaintainer("Foo Bar")
deb.SetMaintainerEmail("[email protected]")
deb.SetHomepage("http://foobar.com")
deb.SetShortDescription("Minimal foo bar package")
deb.SetDescription("Foo bar package doesn't do anything")
deb.AddFile("debpkg.go")
fmt.Println(deb.Write(tempfile))
// Do something with tempfile other than removing it...
}
// TestFilenameFromFullVersion verifies if the whole version string is correctly calculated
func TestFilenameFromFullVersion(t *testing.T) {
deb := New()
defer deb.Close()
deb.SetName("foo")
deb.SetVersion("1.33.7")
deb.SetArchitecture("amd64")
assert.Equal(t, "foo-1.33.7_amd64.deb", deb.GetFilename())
}
// TestGetArchitecture checks the current build.Default.GOARCH compatible debian architecture
func TestGetArchitecture(t *testing.T) {
// On debian 386 GOARCH is presented as i386
goarch := build.Default.GOARCH
build.Default.GOARCH = "386"
assert.Equal(t, "i386", GetArchitecture())
build.Default.GOARCH = goarch
// Check current build GOARCH
if build.Default.GOARCH != "386" {
assert.Equal(t, build.Default.GOARCH, GetArchitecture())
}
}