@@ -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+ }
0 commit comments