Browse Source

Use reflection for Reader/Writer

v0
maze 4 years ago
parent
commit
18889a4dc4
5 changed files with 502 additions and 212 deletions
  1. +46
    -19
      package.go
  2. +182
    -89
      package_desc.go
  3. +20
    -0
      package_desc_test.go
  4. +230
    -101
      package_info.go
  5. +24
    -3
      package_info_test.go

+ 46
- 19
package.go View File

@ -5,6 +5,7 @@ import (
"errors"
"io"
"net/url"
"reflect"
"strings"
"time"
@ -28,22 +29,22 @@ type Package struct {
Filename string
// Name of the package
Name string
Name string `pkginfo:"pkgname"`
// Base of the package, if this package is a member of a split package
Base string
Base string `pkginfo:"pkgbase"`
// Version number
Version string
Version string `pkgdesc:"VERSION,evr" pkginfo:"pkgver,evr"`
// Release number
Release string
Release string `pkgdesc:"-" pkginfo:"-"`
// Epoch
Epoch int
Epoch int `pkgdesc:"-" pkginfo:"-"`
// Description of the package
Description string
Description string `pkgdesc:"DESC" pkginfo:"pkgdesc"`
// Arch slice of supported architectures
Arch []string
@ -58,19 +59,19 @@ type Package struct {
Groups []string
// Depends are the packages this package depends on
Depends []string
Depends []string `pkgdesc:"DEPENDS" pkginfo:"depend"`
// MakeDepends are the packages this package depends on for building
MakeDepends []string
MakeDepends []string `pkgdesc:"MAKEDEPENDS" pkginfo:"makedepend"`
// CheckDepends are the packages this package depends on for running the
// checks during build
CheckDepends []string
CheckDepends []string `pkgdesc:"CHECKDEPENDS" pkginfo:"checkdepend"`
// OptionalDepends are the packages this package recommends having
// installed. Note that these values may contain comments, indicated by
// "spec: comment" (the separator is ": ")
OptionalDepends []string
OptionalDepends []string `pkgdesc:"OPTDEPENDS" pkginfo:"optdepend"`
// Provides are virtual package names provided by this package
Provides []string
@ -86,29 +87,29 @@ type Package struct {
Backup []string
// MD5 is the MD-5 hex digest of the package file
MD5 []byte
MD5 []byte `pkgdesc:"MD5SUM,hex" pkginfo:"md5sum,hex"`
// SHA1 is the SHA-1 hex digest of the package file
SHA1 []byte
SHA1 []byte `pkgdesc:"SHA1SUM,hex" pkginfo:"sha1sum,hex"`
// SHA256 is the SHA-256 hex digest of the package file
SHA256 []byte
SHA256 []byte `pkgdesc:"SHA256SUM,hex" pkginfo:"sha256sum,hex"`
// SHA384 is the SHA-384 hex digest of the package file
SHA384 []byte
SHA384 []byte `pkgdesc:"SHA384SUM,hex" pkginfo:"sha384sum,hex"`
// SHA512 is the SHA-512 hex digest of the package file
SHA512 []byte
SHA512 []byte `pkgdesc:"SHA512SUM,hex" pkginfo:"sha512sum,hex"`
// PGPSignature is the deteched PGP signature base64 encoded digest of the
// package file
PGPSignature []byte
PGPSignature []byte `pkgdesc:"PGPSIG,base64" pkginfo:"pgpsig,base64"`
// Size of the installed package
Size int64
Size int64 `pkgdesc:"ISIZE" pkginfo:"size"`
// CompressedSize of the archive
CompressedSize int64
CompressedSize int64 `pkgdesc:"CSIZE" pkginfo:"-"`
// Options are the makepkg build options used for this package
Options []string
@ -132,7 +133,13 @@ type Package struct {
Files []string
}
const unknown = "unknown"
// Common strings
const (
unknown = "unknown"
base64Encoding = "base64"
evrEncoding = "evr"
hexEncoding = "hex"
)
func (pkg Package) String() string {
var s = make([]string, 3)
@ -225,6 +232,26 @@ func ReadPackage(r io.Reader) (*Package, error) {
}
}
func (pkg *Package) parseFields(parseField func(reflect.StructField) (string, string)) (values map[string]reflect.Value, encodings map[string]string) {
values = make(map[string]reflect.Value)
encodings = make(map[string]string)
var (
value = reflect.Indirect(reflect.ValueOf(pkg))
typ = value.Type()
)
for i := 0; i < value.NumField(); i++ {
fieldValue := value.Field(i)
name, encoding := parseField(typ.Field(i))
if name == "" {
// No name means ignored field
continue
}
values[name] = fieldValue
encodings[name] = encoding
}
return
}
// ParsePackageArchiveName parsea an Arch Linux package archive name
func ParsePackageArchiveName(arc string) (name string, v version.Version, arch, ext string, err error) {
i := strings.Index(arc, ".pkg.tar.")


+ 182
- 89
package_desc.go View File

@ -7,9 +7,12 @@ import (
"fmt"
"io"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"maze.io/archlinux.v0/version"
)
// ReadPackageDesc reads a package description from a desc file
@ -34,6 +37,24 @@ func ReadPackageDesc(r io.Reader) (*Package, error) {
}
func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
var (
fields = make(map[string]reflect.Value)
encodings = make(map[string]string)
value = reflect.Indirect(reflect.ValueOf(pkg))
typ = value.Type()
)
for i := 0; i < value.NumField(); i++ {
fieldValue := value.Field(i)
fieldType := typ.Field(i)
name, encoding := parseFieldDesc(fieldType)
if name == "" {
// No name means ignored field
continue
}
fields[name] = fieldValue
encodings[name] = encoding
}
var (
s = bufio.NewScanner(r)
line string
@ -50,100 +71,58 @@ func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
line = s.Text()
if len(line) == 0 {
switch section {
case "FILENAME":
pkg.Filename = lines[0]
case "NAME":
pkg.Name = lines[0]
case "BASE":
pkg.Base = lines[0]
case "VERSION":
i := strings.LastIndexByte(lines[0], '-')
if i == -1 {
return fmt.Errorf("archlinux: invalid version %q", lines[0])
}
pkg.Version, pkg.Release = lines[0][:i], lines[0][i+1:]
case "DESC":
pkg.Description = strings.TrimSpace(strings.Join(lines, "\n"))
case "ARCH":
pkg.Arch = make([]string, len(lines))
copy(pkg.Arch, lines)
case "LICENSE":
pkg.License = make([]string, len(lines))
copy(pkg.License, lines)
case "GROUPS":
pkg.Groups = make([]string, len(lines))
copy(pkg.Groups, lines)
case "CSIZE":
if pkg.CompressedSize, err = strconv.ParseInt(lines[0], 10, 64); err != nil {
return
}
case "ISIZE":
if pkg.Size, err = strconv.ParseInt(lines[0], 10, 64); err != nil {
return
}
case "MD5SUM":
if pkg.MD5, err = hex.DecodeString(lines[0]); err != nil {
return
}
case "SHA1SUM":
if pkg.SHA1, err = hex.DecodeString(lines[0]); err != nil {
return
}
case "SHA256SUM":
if pkg.SHA256, err = hex.DecodeString(lines[0]); err != nil {
return
}
case "SHA384SUM":
if pkg.SHA384, err = hex.DecodeString(lines[0]); err != nil {
return
}
case "SHA512SUM":
if pkg.SHA512, err = hex.DecodeString(lines[0]); err != nil {
return
}
case "PGPSIG":
if pkg.PGPSignature, err = base64.StdEncoding.DecodeString(lines[0]); err != nil {
return
v, ok := fields[section]
if !ok {
return fmt.Errorf("archlinux: unsupported section %q", section)
}
switch v.Interface().(type) {
case string:
switch encodings[section] {
case "":
v.SetString(lines[0])
case evrEncoding:
p := version.Parse(lines[0])
pkg.Version = p.Version
pkg.Release = p.Release
if p.Epoch != "" {
if pkg.Epoch, err = strconv.Atoi(p.Epoch); err != nil {
return fmt.Errorf("archlinux: invalid epoch %q: %v", p.Epoch, err)
}
}
}
case "URL":
if pkg.URL, err = url.Parse(lines[0]); err != nil {
return
case []string:
c := make([]string, len(lines))
copy(c, lines)
v.Set(reflect.ValueOf(c))
case []byte:
switch encodings[section] {
case "":
v.SetBytes([]byte(strings.Join(lines, "\n")))
case base64Encoding:
var b []byte
if b, err = base64.StdEncoding.DecodeString(lines[0]); err != nil {
return fmt.Errorf("archlinux: error parsing section %q: %v", section, err)
}
v.SetBytes(b)
case hexEncoding:
var b []byte
if b, err = hex.DecodeString(lines[0]); err != nil {
return fmt.Errorf("archlinux: error parsing section %q: %v", section, err)
}
v.SetBytes(b)
}
case "DEPENDS":
pkg.Depends = make([]string, len(lines))
copy(pkg.Depends, lines)
case "OPTDEPENDS":
pkg.OptionalDepends = make([]string, len(lines))
copy(pkg.OptionalDepends, lines)
case "MAKEDEPENDS":
pkg.MakeDepends = make([]string, len(lines))
copy(pkg.MakeDepends, lines)
case "CHECKDEPENDS":
pkg.CheckDepends = make([]string, len(lines))
copy(pkg.CheckDepends, lines)
case "PROVIDES":
pkg.Provides = make([]string, len(lines))
copy(pkg.Provides, lines)
case "CONFLICTS":
pkg.Conflicts = make([]string, len(lines))
copy(pkg.Conflicts, lines)
case "REPLACES":
pkg.Replaces = make([]string, len(lines))
copy(pkg.Replaces, lines)
case "BUILDDATE":
case time.Time:
var i int64
if i, err = strconv.ParseInt(lines[0], 10, 64); err != nil {
return
return fmt.Errorf("archlinux: error parsing section %q: %v", section, err)
}
pkg.BuildDate = time.Unix(i, 0)
case "PACKAGER":
pkg.Packager = lines[0]
case "FILES":
pkg.Files = make([]string, len(lines))
copy(lines, pkg.Files)
default:
return fmt.Errorf("archlinux: unsupported desc section %q", section)
v.Set(reflect.ValueOf(time.Unix(i, 0)))
case *url.URL:
var u *url.URL
if u, err = url.Parse(lines[0]); err != nil {
return fmt.Errorf("archlinux: error parsing section %q: %v", section, err)
}
v.Set(reflect.ValueOf(u))
}
} else if line[0] == '%' && line[len(line)-1] == '%' {
section = line[1 : len(line)-1]
@ -154,3 +133,117 @@ func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
}
return
}
func parseFieldDesc(field reflect.StructField) (name, encoding string) {
tag := field.Tag.Get("pkgdesc")
switch tag {
case "-":
// Ignored tag
case "":
// No desc tag, default to upper case field name
name = strings.ToUpper(field.Name)
default:
if i := strings.IndexByte(tag, ','); i != -1 {
name, encoding = strings.ToUpper(tag[:i]), tag[i+1:]
} else {
name = strings.ToUpper(tag)
}
}
return
}
var timeZero time.Time
// WritePackageDesc writes a package desc file to the target Writer.
func (pkg *Package) WritePackageDesc(w io.Writer) (int, error) {
var (
value = reflect.Indirect(reflect.ValueOf(pkg))
typ = value.Type()
total, n int
err error
)
for i := 0; i < value.NumField(); i++ {
fieldValue := value.Field(i)
fieldType := typ.Field(i)
name, encoding := parseFieldDesc(fieldType)
if name == "" {
// No name means ignored field
continue
}
switch v := fieldValue.Interface().(type) {
case string:
if v == "" {
continue
}
switch encoding {
case "":
// No additional encoding
case evrEncoding:
// Epoch:Version-Release
if pkg.Epoch > 0 {
v = fmt.Sprintf("%d:%s-%s", pkg.Epoch, pkg.Version, pkg.Release)
} else {
v = fmt.Sprintf("%s-%s", pkg.Version, pkg.Release)
}
}
if n, err = fmt.Fprintf(w, "%%%s%%\n%s\n\n", name, v); err != nil {
return total + n, err
}
total += n
case int64:
if v == 0 {
continue
}
if n, err = fmt.Fprintf(w, "%%%s%%\n%d\n\n", name, v); err != nil {
return total + n, err
}
total += n
case []string:
if len(v) == 0 {
continue
}
if n, err = fmt.Fprintf(w, "%%%s%%\n%s\n\n", name, strings.Join(v, "\n")); err != nil {
return total + n, err
}
total += n
case []byte:
if len(v) == 0 {
continue
}
var o []byte
switch encoding {
case "":
o = v
case base64Encoding:
o = make([]byte, base64.StdEncoding.EncodedLen(len(v)))
base64.StdEncoding.Encode(o, v)
case hexEncoding:
o = make([]byte, hex.EncodedLen(len(v)))
hex.Encode(o, v)
}
if n, err = fmt.Fprintf(w, "%%%s%%\n%s\n\n", name, o); err != nil {
return total + n, err
}
total += n
case time.Time:
if v.Equal(timeZero) {
continue
}
if n, err = fmt.Fprintf(w, "%%%s%%\n%d\n\n", name, v.Unix()); err != nil {
return total + n, err
}
total += n
case *url.URL:
if v == nil {
continue
}
if n, err = fmt.Fprintf(w, "%%%s%%\n%s\n\n", name, v); err != nil {
return total + n, err
}
total += n
default:
return total, fmt.Errorf("archlinux: can't write %T to desc", v)
}
}
return total, nil
}

+ 20
- 0
package_desc_test.go View File

@ -2,6 +2,9 @@ package archlinux
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
)
@ -184,3 +187,20 @@ glibc
`
)
func TestWritePackageDesc(t *testing.T) {
var w io.Writer
if os.Getenv("TEST_DEBUG_WRITER") != "" {
w = os.Stdout
} else {
w = ioutil.Discard
}
n, err := testWritePackageInfo.WritePackageDesc(w)
if err != nil {
t.Fatal(err)
}
if n == 0 {
t.Fatal(`expected >0 bytes to be written`)
}
t.Log(n, "bytes written")
}

+ 230
- 101
package_info.go View File

@ -2,12 +2,17 @@ package archlinux
import (
"bufio"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"maze.io/archlinux.v0/version"
)
// ReadPackageInfo reads a package description from a .PKGINFO file
@ -57,6 +62,7 @@ func (pkg *Package) readBuildInfo(r io.Reader) (err error) {
}
func (pkg *Package) readPackageInfo(r io.Reader) (err error) {
values, encodings := pkg.parseFields(parseFieldInfo)
s := bufio.NewScanner(r)
for s.Scan() {
if err = s.Err(); err != nil {
@ -71,129 +77,252 @@ func (pkg *Package) readPackageInfo(r io.Reader) (err error) {
continue
}
switch fields[0] {
case "pkgname":
pkg.Name = fields[1]
case "pkgbase":
pkg.Base = fields[1]
case "pkgver":
if i := strings.IndexByte(fields[1], '-'); i != -1 {
pkg.Version, pkg.Release = fields[1][:i], fields[1][i+1:]
} else {
return fmt.Errorf("archlinux: invalid version %q", fields[1])
}
case "pkgdesc":
pkg.Description = fields[1]
case "url":
if pkg.URL, err = url.Parse(fields[1]); err != nil {
return fmt.Errorf("archlinux: invalid url %q: %v", fields[1], err)
}
case "builddate":
var t int64
if t, err = strconv.ParseInt(fields[1], 10, 64); err != nil {
return fmt.Errorf("archlinux: invalid build date %q: %v", fields[1], err)
}
pkg.BuildDate = time.Unix(t, 0)
case "packager":
pkg.Packager = fields[1]
case "size":
if pkg.Size, err = strconv.ParseInt(fields[1], 10, 64); err != nil {
return fmt.Errorf("archlinux: invalid size %q: %v", fields[1], err)
}
case "arch":
pkg.Arch = append(pkg.Arch, fields[1])
case "group":
pkg.Groups = append(pkg.Groups, fields[1])
case "license":
pkg.License = append(pkg.License, fields[1])
case "backup":
pkg.Backup = append(pkg.Backup, fields[1])
case "replaces":
pkg.Replaces = append(pkg.Replaces, fields[1])
case "depend":
pkg.Depends = append(pkg.Depends, fields[1])
case "conflict":
pkg.Conflicts = append(pkg.Conflicts, fields[1])
case "provides":
pkg.Provides = append(pkg.Provides, fields[1])
case "optdepend":
pkg.OptionalDepends = append(pkg.OptionalDepends, fields[1])
case "makedepend":
pkg.MakeDepends = append(pkg.MakeDepends, fields[1])
case "checkdepend":
pkg.CheckDepends = append(pkg.CheckDepends, fields[1])
default:
v, ok := values[fields[0]]
if !ok {
return fmt.Errorf("archlinux: unknown PKGINFO field %q", fields[0])
}
encoding := encodings[fields[0]]
switch v.Interface().(type) {
case string:
switch encoding {
case "":
v.SetString(fields[1])
case "evr":
p := version.Parse(fields[1])
if p.Version == "" {
return ErrNoPackageVersion
}
pkg.Version = p.Version
if p.Release == "" {
return ErrNoPackageRelease
}
pkg.Release = p.Release
if p.Epoch != "" {
if pkg.Epoch, err = strconv.Atoi(p.Epoch); err != nil {
return fmt.Errorf("archlinux: invalid section %q: %v", fields[0], err)
}
}
}
case int64:
var i int64
if i, err = strconv.ParseInt(fields[1], 10, 64); err != nil {
return fmt.Errorf("archlinux: invalid section %q: %v", fields[0], err)
}
v.SetInt(i)
case []string:
v.Set(reflect.Append(v, reflect.ValueOf(fields[1])))
case []byte:
switch encoding {
case "":
v.SetBytes([]byte(fields[1]))
case "base64":
var b []byte
if b, err = base64.StdEncoding.DecodeString(fields[1]); err != nil {
return fmt.Errorf("archlinux: invalid section %q: %v", fields[0], err)
}
v.SetBytes(b)
case "hex":
var b []byte
if b, err = hex.DecodeString(fields[1]); err != nil {
return fmt.Errorf("archlinux: invalid section %q: %v", fields[0], err)
}
v.SetBytes(b)
}
case time.Time:
var i int64
if i, err = strconv.ParseInt(fields[1], 10, 64); err != nil {
return fmt.Errorf("archlinux: invalid section %q: %v", fields[0], err)
}
v.Set(reflect.ValueOf(time.Unix(i, 0).UTC()))
case *url.URL:
var u *url.URL
if u, err = url.Parse(fields[1]); err != nil {
return fmt.Errorf("archlinux: invalid section %q: %v", fields[0], err)
}
v.Set(reflect.ValueOf(u))
}
}
return
}
func parseFieldInfo(field reflect.StructField) (name, encoding string) {
tag := field.Tag.Get("pkginfo")
switch tag {
case "-":
// Ignored tag
case "":
// No desc tag, default to upper case field name
name = strings.ToLower(field.Name)
default:
if i := strings.IndexByte(tag, ','); i != -1 {
name, encoding = tag[:i], tag[i+1:]
} else {
name = tag
}
}
return
}
// WritePackageInfo writes a PKGINFO to the supplied Writer
func (pkg *Package) WritePackageInfo(w io.Writer) error {
func (pkg *Package) WritePackageInfo(w io.Writer) (int, error) {
var (
fields = []struct {
Key string
Value interface{}
}{
{"pkgname", pkg.Name},
{"pkgbase", pkg.Base},
{"pkgver", pkg.Version + "-" + pkg.Release},
{"pkgdesc", pkg.Description},
{"url", pkg.URL},
{"builddate", pkg.BuildDate},
{"packager", pkg.Packager},
{"size", pkg.Size},
{"arch", pkg.Arch},
{"license", pkg.License},
{"provides", pkg.Provides},
{"conflicts", pkg.Conflicts},
{"replaces", pkg.Replaces},
{"backup", pkg.Backup},
{"depend", pkg.Depends},
{"optdepend", pkg.OptionalDepends},
{"makedepend", pkg.MakeDepends},
{"checkdepend", pkg.CheckDepends},
}
err error
value = reflect.Indirect(reflect.ValueOf(pkg))
typ = value.Type()
total, n int
err error
)
if _, err = fmt.Fprintln(w, "# Generated by maze.io/archlinux.v0\n#", time.Now().UTC().Format(time.UnixDate)); err != nil {
return err
}
for _, f := range fields {
switch value := f.Value.(type) {
for i := 0; i < value.NumField(); i++ {
fieldValue := value.Field(i)
fieldType := typ.Field(i)
name, encoding := parseFieldInfo(fieldType)
if name == "" {
// No name means ignored field
continue
}
switch v := fieldValue.Interface().(type) {
case string:
if value == "" {
if v == "" {
continue
}
if _, err = fmt.Fprintln(w, f.Key, "=", value); err != nil {
return err
switch encoding {
case "":
// No additional encoding
case evrEncoding:
// Epoch:Version-Release
if pkg.Epoch > 0 {
v = fmt.Sprintf("%d:%s-%s", pkg.Epoch, pkg.Version, pkg.Release)
} else {
v = fmt.Sprintf("%s-%s", pkg.Version, pkg.Release)
}
}
if n, err = fmt.Fprintf(w, "%s = %s\n", name, v); err != nil {
return total + n, err
}
total += n
case []string:
if len(value) > 0 {
for _, v := range value {
if v == "" {
continue
}
if _, err = fmt.Fprintln(w, f.Key, "=", v); err != nil {
return err
}
if len(v) == 0 {
continue
}
for _, s := range v {
if n, err = fmt.Fprintf(w, "%s = %s\n", name, s); err != nil {
return total + n, err
}
}
total += n
case []byte:
if len(v) == 0 {
continue
}
var o []byte
switch encoding {
case "":
o = v
case base64Encoding:
o = make([]byte, base64.StdEncoding.EncodedLen(len(v)))
base64.StdEncoding.Encode(o, v)
case hexEncoding:
o = make([]byte, hex.EncodedLen(len(v)))
hex.Encode(o, v)
}
if n, err = fmt.Fprintf(w, "%s = %s\n", name, o); err != nil {
return total + n, err
}
total += n
case int64:
if value > 0 {
if _, err = fmt.Fprintln(w, f.Key, "=", value); err != nil {
return err
}
if v == 0 {
continue
}
if n, err = fmt.Fprintf(w, "%s = %d\n", name, v); err != nil {
return total + n, err
}
total += n
case time.Time:
if v.Equal(timeZero) {
continue
}
if n, err = fmt.Fprintf(w, "%s = %d\n", name, v.Unix()); err != nil {
return total + n, err
}
total += n
case *url.URL:
if value == nil {
if v == nil {
continue
}
if _, err = fmt.Fprintln(w, f.Key, "=", value.String()); err != nil {
return err
if n, err = fmt.Fprintf(w, "%s = %s\n", name, v); err != nil {
return total + n, err
}
total += n
default:
return total, fmt.Errorf("archlinux: can't write %T to info", v)
}
}
return nil
return total, nil
/*
var (
fields = []struct {
Key string
Value interface{}
}{
{"pkgname", pkg.Name},
{"pkgbase", pkg.Base},
{"pkgver", pkg.Version + "-" + pkg.Release},
{"pkgdesc", pkg.Description},
{"url", pkg.URL},
{"builddate", pkg.BuildDate},
{"packager", pkg.Packager},
{"size", pkg.Size},
{"arch", pkg.Arch},
{"license", pkg.License},
{"provides", pkg.Provides},
{"conflicts", pkg.Conflicts},
{"replaces", pkg.Replaces},
{"backup", pkg.Backup},
{"depend", pkg.Depends},
{"optdepend", pkg.OptionalDepends},
{"makedepend", pkg.MakeDepends},
{"checkdepend", pkg.CheckDepends},
}
err error
)
if _, err = fmt.Fprintln(w, "# Generated by maze.io/archlinux.v0\n#", time.Now().UTC().Format(time.UnixDate)); err != nil {
return err
}
for _, f := range fields {
switch value := f.Value.(type) {
case string:
if value == "" {
continue
}
if _, err = fmt.Fprintln(w, f.Key, "=", value); err != nil {
return err
}
case []string:
if len(value) > 0 {
for _, v := range value {
if v == "" {
continue
}
if _, err = fmt.Fprintln(w, f.Key, "=", v); err != nil {
return err
}
}
}
case int64:
if value > 0 {
if _, err = fmt.Fprintln(w, f.Key, "=", value); err != nil {
return err
}
}
case *url.URL:
if value == nil {
continue
}
if _, err = fmt.Fprintln(w, f.Key, "=", value.String()); err != nil {
return err
}
}
}
return nil
*/
}

+ 24
- 3
package_info_test.go View File

@ -2,7 +2,11 @@ package archlinux
import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"testing"
"time"
@ -140,7 +144,7 @@ func TestReadPackageInfoError(t *testing.T) {
{testPackageInfoError, `unknown PKGINFO field`},
{testPackageInfoErrorName, "name is missing"},
{testPackageInfoErrorVersion, "version is missing"},
{testPackageInfoErrorVersionInvalid, "invalid version"},
//{testPackageInfoErrorVersionInvalid, "invalid version"},
{testPackageInfoErrorRelease, "release is missing"},
}
for _, test := range tests {
@ -167,18 +171,32 @@ pkgver = 330-`
)
func TestWritePackageInfo(t *testing.T) {
if err := testWritePackageInfo.WritePackageInfo(ioutil.Discard); err != nil {
var w io.Writer
if os.Getenv("TEST_DEBUG_WRITER") != "" {
w = os.Stdout
} else {
w = ioutil.Discard
}
n, err := testWritePackageInfo.WritePackageInfo(w)
if err != nil {
t.Fatal(err)
}
if n == 0 {
t.Fatal(`expected >0 bytes to be written`)
}
t.Log(n, "bytes written")
}
var (
testWritePackageInfo = &Package{
testWritePackageInfoURL, _ = url.Parse("http://www.imagemagick.org/")
testWritePackagePGPSignature, _ = base64.StdEncoding.DecodeString("iQEzBAABCAAdFiEEFRnVq6Zb9vwrc8dWek52CV2KUuQFAlnG2vYACgkQek52CV2KUuTvKAf/XgeeH4ahrRKSvlJYBw2SQ1ZaAYTFv/uc90myNFFO3orRnE+0FBNSR2pHhuXXXpBwalflsVWvtoJzY/0XHU2uLeeWmFGcC5DMqlatlJJgBcIJ+GrQlCqlTonYQ+cK/vdrb7HE3pGEJ+HDx5ye4vsefX1C3ToKXU1D/6r75wF/xIJpRyX8HfZcO4i3IURMjJUGaKu0HklXcvpdR608t/HFDIprk+Oz1BA+QbMIjR0r/hFuxP6wK/e/JckRPDsxV+nOz2dRYDkaWkuiIl1y1wpWWySHh4s17/mWi+BBsG9d6H4/V79ADD1BoggsJDtYO9K5alAivWlACLSPJ1ZQ6yFqlw==")
testWritePackageInfo = &Package{
Name: "imagemagick",
Base: "imagemagick",
Version: "6.9.9.15",
Release: "1",
Description: "An image viewing/manipulation program",
URL: testWritePackageInfoURL,
Arch: []string{"x86_64"},
License: []string{"custom"},
Depends: []string{"libltdl", "lcms2", "libxt", "fontconfig", "libxext", "liblqr", "libraqm", "opencl-icd-loader", "perl>=5.26", "perl<5.27"},
@ -187,6 +205,9 @@ var (
Backup: []string{"etc/ImageMagick-6/coder.xml", "etc/ImageMagick-6/colors.xml", "etc/ImageMagick-6/delegates.xml", "etc/ImageMagick-6/log.xml", "etc/ImageMagick-6/magic.xml", "etc/ImageMagick-6/mime.xml", "etc/ImageMagick-6/policy.xml", "etc/ImageMagick-6/quantization-table.xml", "etc/ImageMagick-6/thresholds.xml", "etc/ImageMagick-6/type.xml", "etc/ImageMagick-6/type-dejavu.xml", "etc/ImageMagick-6/type-ghostscript.xml", "etc/ImageMagick-6/type-windows.xml"},
Size: 9897984,
BuildDate: time.Unix(1506203625, 0),
MD5: []byte("\x54\x71\xa4\x62\x04\xd5\x25\xcb\x3c\x3c\xcf\xc8\x58\x71\x1e\x02"),
SHA256: []byte("\x25\xf5\xfd\x35\x60\x55\xd9\x91\xdc\xb1\x98\x5e\x59\xbd\x65\xb6\x2b\x8a\x14\x34\xb9\x1e\xd4\x94\xea\x99\x2c\x2c\x9e\x53\xe4\xec"),
PGPSignature: testWritePackagePGPSignature,
Packager: "Antonio Rojas <arojas@archlinux.org>",
}
)

Loading…
Cancel
Save