Skip to content

Commit d557fc6

Browse files
committed
wip: tests
1 parent 9e612b2 commit d557fc6

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

apt.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"bufio"
2323
"bytes"
2424
"fmt"
25+
"io"
2526
"os/exec"
2627
"regexp"
2728
"strconv"
@@ -64,6 +65,8 @@ func Search(pattern string) ([]*Package, error) {
6465
func parseDpkgQueryOutput(out []byte) []*Package {
6566
res := []*Package{}
6667
scanner := bufio.NewScanner(bytes.NewReader(out))
68+
buf := make([]byte, 0, 1024*1024) // 1 MB buffer
69+
scanner.Buffer(buf, 1024*1024) // Set max buffer size to 1 MB
6770
for scanner.Scan() {
6871
data := strings.Split(scanner.Text(), "\t")
6972
size, err := strconv.Atoi(data[4])
@@ -98,10 +101,20 @@ func ListUpgradable() ([]*Package, error) {
98101
if err != nil {
99102
return nil, fmt.Errorf("running apt list: %s", err)
100103
}
104+
105+
res := parseListUpgradableOutput(bytes.NewReader(out))
106+
return res, nil
107+
}
108+
109+
func parseListUpgradableOutput(r io.Reader) []*Package {
110+
// Example of matched line:
111+
// xserver-xorg-core/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
101112
re := regexp.MustCompile(`^([^ ]+) ([^ ]+) ([^ ]+)( \[upgradable from: [^\[\]]*\])?`)
102113

103114
res := []*Package{}
104-
scanner := bufio.NewScanner(bytes.NewReader(out))
115+
scanner := bufio.NewScanner(r)
116+
buf := make([]byte, 0, 1024*1024) // 1 MB buffer
117+
scanner.Buffer(buf, 1024*1024) // Set max buffer size to 1 MB
105118
for scanner.Scan() {
106119
matches := re.FindAllStringSubmatch(scanner.Text(), -1)
107120
if len(matches) == 0 {
@@ -120,7 +133,7 @@ func ListUpgradable() ([]*Package, error) {
120133
Architecture: matches[0][3],
121134
})
122135
}
123-
return res, nil
136+
return res
124137
}
125138

126139
// Upgrade runs the upgrade for a set of packages

apt_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"os"
25+
"strings"
2526
"testing"
2627

2728
"github.com/stretchr/testify/require"
2829
)
2930

30-
func TestList(t *testing.T) {
31+
func TestParseDpkgQueryOutput(t *testing.T) {
3132
out, err := os.ReadFile("testdata/dpkg-query-output-1.txt")
3233
require.NoError(t, err, "Reading test input data")
3334
list := parseDpkgQueryOutput(out)
@@ -68,3 +69,68 @@ func TestCheckForUpdates(t *testing.T) {
6869
fmt.Printf(">>>\n%s\n<<<\n", string(out))
6970
fmt.Println("ERR:", err)
7071
}
72+
73+
func TestParseListUpgradableOutput(t *testing.T) {
74+
t.Run("special cases", func(t *testing.T) {
75+
tests := []struct {
76+
name string
77+
input string
78+
expected []*Package
79+
}{
80+
{
81+
name: "empty input",
82+
input: "",
83+
expected: []*Package{},
84+
},
85+
{
86+
name: "line not matching regex",
87+
input: "this-is-not-a-valid-line\n",
88+
expected: []*Package{},
89+
},
90+
{
91+
name: "upgradable package without [upgradable from]",
92+
input: "nano/bionic-updates 2.9.3-2 amd64\n",
93+
expected: []*Package{
94+
{
95+
Name: "nano",
96+
Status: "upgradable",
97+
Version: "2.9.3-2",
98+
Architecture: "amd64",
99+
},
100+
},
101+
},
102+
}
103+
for _, tt := range tests {
104+
t.Run(tt.name, func(t *testing.T) {
105+
res := parseListUpgradableOutput(strings.NewReader(tt.input))
106+
require.Equal(t, tt.expected, res)
107+
})
108+
}
109+
})
110+
111+
t.Run("golden file: list-upgradable.golden", func(t *testing.T) {
112+
data, err := os.ReadFile("testdata/apt-list-upgradable.golden")
113+
require.NoError(t, err, "Reading golden file")
114+
result := parseListUpgradableOutput(strings.NewReader(string(data)))
115+
116+
want := []*Package{
117+
{Name: "apt-transport-https", Status: "upgradable", Version: "2.0.11", Architecture: "all"},
118+
{Name: "apt-utils", Status: "upgradable", Version: "2.0.11", Architecture: "amd64"},
119+
{Name: "apt", Status: "upgradable", Version: "2.0.11", Architecture: "amd64"},
120+
{Name: "code-insiders", Status: "upgradable", Version: "1.101.0-1749657374", Architecture: "amd64"},
121+
{Name: "code", Status: "upgradable", Version: "1.100.3-1748872405", Architecture: "amd64"},
122+
{Name: "containerd.io", Status: "upgradable", Version: "1.7.27-1", Architecture: "amd64"},
123+
{Name: "distro-info-data", Status: "upgradable", Version: "0.43ubuntu1.18", Architecture: "all"},
124+
{Name: "docker-ce-cli", Status: "upgradable", Version: "5:28.1.1-1~ubuntu.20.04~focal", Architecture: "amd64"},
125+
{Name: "python3.12", Status: "upgradable", Version: "3.12.11-1+focal1", Architecture: "amd64"},
126+
{Name: "xdg-desktop-portal", Status: "upgradable", Version: "1.14.3-1~flatpak1~20.04", Architecture: "amd64"},
127+
{Name: "xserver-common", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "all"},
128+
{Name: "xserver-xephyr", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
129+
{Name: "xserver-xorg-core", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
130+
{Name: "xserver-xorg-legacy", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
131+
{Name: "xwayland", Status: "upgradable", Version: "2:1.20.13-1ubuntu1~20.04.20", Architecture: "amd64"},
132+
}
133+
require.NotNil(t, result)
134+
require.Equal(t, want, result, "Parsed result should match expected from golden file")
135+
})
136+
}

testdata/apt-list-upgradable.golden

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Listing... Done
2+
apt-transport-https/focal-updates,focal-updates 2.0.11 all [upgradable from: 2.0.10]
3+
apt-utils/focal-updates 2.0.11 amd64 [upgradable from: 2.0.10]
4+
apt/focal-updates 2.0.11 amd64 [upgradable from: 2.0.10]
5+
code-insiders/stable 1.101.0-1749657374 amd64 [upgradable from: 1.100.0-1743745333]
6+
code/stable 1.100.3-1748872405 amd64 [upgradable from: 1.100.2-1747260578]
7+
containerd.io/focal 1.7.27-1 amd64 [upgradable from: 1.7.25-1]
8+
distro-info-data/focal-updates,focal-updates 0.43ubuntu1.18 all [upgradable from: 0.43ubuntu1.16]
9+
docker-ce-cli/focal 5:28.1.1-1~ubuntu.20.04~focal amd64 [upgradable from: 5:28.0.1-1~ubuntu.20.04~focal]
10+
python3.12/focal 3.12.11-1+focal1 amd64 [upgradable from: 3.12.5-1+focal1]
11+
xdg-desktop-portal/focal 1.14.3-1~flatpak1~20.04 amd64 [upgradable from: 1.6.0-1ubuntu2]
12+
xserver-common/focal-updates,focal-updates 2:1.20.13-1ubuntu1~20.04.20 all [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
13+
xserver-xephyr/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
14+
xserver-xorg-core/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
15+
xserver-xorg-legacy/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]
16+
xwayland/focal-updates 2:1.20.13-1ubuntu1~20.04.20 amd64 [upgradable from: 2:1.20.13-1ubuntu1~20.04.19]

0 commit comments

Comments
 (0)