diff --git a/deb/format.go b/deb/format.go index 95febe35..3ac4a77f 100644 --- a/deb/format.go +++ b/deb/format.go @@ -288,8 +288,14 @@ func (c *ControlFileReader) ReadStanza() (Stanza, error) { lastField = canonicalCase(parts[0]) lastFieldMultiline = isMultilineField(lastField, c.isRelease) if lastFieldMultiline { - stanza[lastField] = parts[1] - if parts[1] != "" { + // Trim trailing whitespace from the inline value so that + // "Package-List: " (trailing space, as used by Debian) is + // treated identically to "Package-List:" (no inline content). + // Without this, the trailing space is stored and later + // re-emitted as a spurious blank continuation line. + inlineVal := strings.TrimRight(parts[1], " \t") + stanza[lastField] = inlineVal + if inlineVal != "" { stanza[lastField] += "\n" } } else { diff --git a/deb/format_test.go b/deb/format_test.go index 7228566b..00555c90 100644 --- a/deb/format_test.go +++ b/deb/format_test.go @@ -128,6 +128,42 @@ func (s *ControlFileSuite) TestReadWriteStanza(c *C) { c.Assert(strings.HasPrefix(str, "Package: "), Equals, true) } +// TestPackageListTrailingSpace is a regression test for +// https://github.com/aptly-dev/aptly/issues/1538. +// Upstream Debian Sources files write "Package-List: " with a trailing space +// on the header line. That trailing space must not be preserved and re-emitted +// as a spurious blank continuation line when the stanza is written back out. +func (s *ControlFileSuite) TestPackageListTrailingSpace(c *C) { + // Input mirrors the format used by real Debian Sources files: + // the "Package-List:" header carries a trailing space, not bare colon. + input := "Package-List: \n" + + " bash deb shells required arch=any\n" + + " bash-doc deb doc optional arch=all\n" + + r := NewControlFileReader(bytes.NewBufferString(input), false, false) + stanza, err := r.ReadStanza() + c.Assert(err, IsNil) + + // The stored value must equal what a bare "Package-List:\n" header gives: + // no leading whitespace / blank line, just the continuation lines. + c.Check(stanza["Package-List"], Equals, + " bash deb shells required arch=any\n"+ + " bash-doc deb doc optional arch=all\n") + + // Round-trip: written output must not contain a spurious blank line. + buf := &bytes.Buffer{} + w := bufio.NewWriter(buf) + err = stanza.Copy().WriteTo(w, true, false, false) + c.Assert(err, IsNil) + c.Assert(w.Flush(), IsNil) + + written := buf.String() + c.Assert(strings.Contains(written, "Package-List:\n \n"), Equals, false, + Commentf("spurious blank continuation line found in written output:\n%s", written)) + c.Assert(strings.Contains(written, "Package-List:\n bash"), Equals, true, + Commentf("expected Package-List entries not found in written output:\n%s", written)) +} + func (s *ControlFileSuite) TestReadWriteInstallerStanza(c *C) { s.reader = bytes.NewBufferString(installerFile) r := NewControlFileReader(s.reader, false, true)