Fix ErrorToStringArray handling of empty stderr output

strings.Split(output, "\n") on empty output returns [""], not []. so
ErrorToStringArray() reported empty stderr as one line of empty output
instead of no output, and any caller checking len() got a wrong count.

volume_ls_test.go had to carry HaveLen(1) just to tolerate that on
empty stderr, updated to BeEmpty() now that the length is actually
correct.

filter out empty lines when building the result.

Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
This commit is contained in:
Atishyy27 2026-08-16 01:42:39 +05:30 committed by Atishyy27
parent dbae2a4496
commit ef171ba1b9
No known key found for this signature in database
3 changed files with 15 additions and 3 deletions

View file

@ -21,7 +21,7 @@ var _ = Describe("Podman volume ls", func() {
empty := podmanTest.PodmanExitCleanly("volume", "ls")
Expect(empty.OutputToString()).To(ContainSubstring("DRIVER"))
Expect(empty.OutputToString()).To(ContainSubstring("VOLUME NAME"))
Expect(empty.ErrorToStringArray()).To(HaveLen(1))
Expect(empty.ErrorToStringArray()).To(BeEmpty())
session := podmanTest.Podman([]string{"volume", "create", "myvol"})
session.WaitWithDefaultTimeout()

View file

@ -27,7 +27,13 @@ var _ = Describe("PodmanSession test", func() {
})
It("Test ErrorToStringArray", func() {
Expect(session.ErrorToStringArray()).To(Equal([]string{"PodmanSession", "test", "Podman Session", ""}))
Expect(session.ErrorToStringArray()).To(Equal([]string{"PodmanSession", "test", "Podman Session"}))
})
It("Test ErrorToStringArray with empty output", func() {
session = StartFakeCmdSession([]string{})
session.WaitWithDefaultTimeout()
Expect(session.ErrorToStringArray()).To(BeEmpty())
})
It("Test GrepString", func() {

View file

@ -269,8 +269,14 @@ func (s *PodmanSession) ErrorToString() string {
// ErrorToStringArray returns the stderr output as a []string
// where each array item is a line split by newline
func (s *PodmanSession) ErrorToStringArray() []string {
var results []string
output := string(s.Err.Contents())
return strings.Split(output, "\n")
for line := range strings.SplitSeq(output, "\n") {
if line != "" {
results = append(results, line)
}
}
return results
}
// GrepString takes session output and behaves like grep. it returns a bool