Skip to content

Commit b0c5dec

Browse files
authored
fix(scanner/suse): skip table header in zypper -q lu (#2093)
1 parent 2c84be8 commit b0c5dec

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

scanner/suse.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,27 @@ var warnRepoPattern = regexp.MustCompile(`Warning: Repository '.+' appears to be
228228
func (o *suse) parseZypperLULines(stdout string) (models.Packages, error) {
229229
updatables := models.Packages{}
230230
scanner := bufio.NewScanner(strings.NewReader(stdout))
231+
headerParsed := false
231232
for scanner.Scan() {
232233
line := scanner.Text()
233-
if line == "" || strings.Contains(line, "S | Repository") || strings.Contains(line, "--+----------------") || warnRepoPattern.MatchString(line) {
234+
if line == "" || warnRepoPattern.MatchString(line) {
234235
continue
235236
}
237+
238+
if !headerParsed {
239+
if func() bool {
240+
for _, c := range line {
241+
if !strings.ContainsRune("-+", c) {
242+
return false
243+
}
244+
}
245+
return true
246+
}() {
247+
headerParsed = true
248+
}
249+
continue
250+
}
251+
236252
pack, err := o.parseZypperLUOneLine(line)
237253
if err != nil {
238254
return nil, err
@@ -248,6 +264,9 @@ func (o *suse) parseZypperLUOneLine(line string) (*models.Package, error) {
248264
return nil, xerrors.Errorf("zypper -q lu Unknown format: %s", line)
249265
}
250266
available := strings.Split(strings.TrimSpace(ss[4]), "-")
267+
if len(available) != 2 {
268+
return nil, xerrors.Errorf("unexpected Available Version. expected: %q, actual: %q", "<major>-<release>", strings.TrimSpace(ss[4]))
269+
}
251270
return &models.Package{
252271
Name: strings.TrimSpace(ss[2]),
253272
NewVersion: available[0],

scanner/suse_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ v | Update repository with updates from SUSE Linux Enterprise 15 | git-core | 2.
6868
},
6969
},
7070
},
71+
{
72+
name: "table header",
73+
args: args{
74+
stdout: `S | Repository | Name | Current Version | Available Version | Arch
75+
---+--------------------------------------------------------------+----------+-----------------+-------------------+-------
76+
v | Update repository with updates from SUSE Linux Enterprise 15 | iproute2 | 5.14-150400.1.8 | 6.4-150600.7.3.1 | x86_64`,
77+
},
78+
want: models.Packages{
79+
"iproute2": {
80+
Name: "iproute2",
81+
NewVersion: "6.4",
82+
NewRelease: "150600.7.3.1",
83+
Arch: "x86_64",
84+
},
85+
},
86+
},
7187
}
7288
for _, tt := range tests {
7389
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)