Package equality, string representation.

This commit is contained in:
Andrey Smirnov
2013-12-18 12:11:19 +04:00
parent 148bbc8b2a
commit a2a51358f9
2 changed files with 36 additions and 2 deletions

17
debian/package.go vendored
View File

@@ -2,6 +2,8 @@ package debian
import (
"bytes"
"fmt"
"github.com/smira/aptly/utils"
debc "github.com/smira/godebiancontrol"
"github.com/ugorji/go/codec"
"strings"
@@ -59,7 +61,7 @@ func NewPackageFromControlFile(input debc.Paragraph) *Package {
// Key returns unique key identifying package
func (p *Package) Key() []byte {
return []byte(p.Name + " " + p.Version + " " + p.Architecture)
return []byte("P" + p.Name + " " + p.Version + " " + p.Architecture)
}
// Encode does msgpack encoding of Package
@@ -77,3 +79,16 @@ func (p *Package) Decode(input []byte) error {
decoder := codec.NewDecoderBytes(input, &codec.MsgpackHandle{})
return decoder.Decode(p)
}
// String creates readable representation
func (p *Package) String() string {
return fmt.Sprintf("%s-%s_%s", p.Name, p.Version, p.Architecture)
}
// Equals compares two packages to be identical
func (p *Package) Equals(p2 *Package) bool {
return p.Name == p2.Name && p.Version == p2.Version && p.Filename == p2.Filename &&
p.Architecture == p2.Architecture && utils.StrSlicesEqual(p.Depends, p2.Depends) &&
utils.StrSlicesEqual(p.PreDepends, p2.PreDepends) && utils.StrSlicesEqual(p.Suggests, p2.Suggests) &&
utils.StrSlicesEqual(p.Recommends, p2.Recommends) && utils.StrMapsEqual(p.Extra, p2.Extra)
}

View File

@@ -34,7 +34,7 @@ func (s *PackageSuite) TestNewFromPara(c *C) {
func (s *PackageSuite) TestKey(c *C) {
p := NewPackageFromControlFile(s.para)
c.Check(p.Key(), DeepEquals, []byte("alien-arena-common 7.40-2 i386"))
c.Check(p.Key(), DeepEquals, []byte("Palien-arena-common 7.40-2 i386"))
}
func (s *PackageSuite) TestEncodeDecode(c *C) {
@@ -46,3 +46,22 @@ func (s *PackageSuite) TestEncodeDecode(c *C) {
c.Assert(err, IsNil)
c.Assert(p2, DeepEquals, p)
}
func (s *PackageSuite) TestString(c *C) {
p := NewPackageFromControlFile(s.para)
c.Assert(p.String(), Equals, "alien-arena-common-7.40-2_i386")
}
func (s *PackageSuite) TestEquals(c *C) {
p := NewPackageFromControlFile(s.para)
para2 := make(debc.Paragraph)
for k, v := range packagePara {
para2[k] = v
}
p2 := NewPackageFromControlFile(para2)
c.Check(p.Equals(p2), Equals, true)
p2.Depends = []string{"package1"}
c.Check(p.Equals(p2), Equals, false)
}