Browse Source

Split package

v0
maze 4 years ago
parent
commit
e948177cd1
6 changed files with 737 additions and 698 deletions
  1. +4
    -332
      package.go
  2. +156
    -0
      package_desc.go
  3. +186
    -0
      package_desc_test.go
  4. +199
    -0
      package_info.go
  5. +192
    -0
      package_info_test.go
  6. +0
    -366
      package_test.go

+ 4
- 332
package.go View File

@ -1,15 +1,10 @@
package archlinux
import (
"bufio"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
@ -132,6 +127,9 @@ type Package struct {
// Contributors to this package
Contributors []string
// Files is a list of files provided by the package
Files []string
}
const unknown = "unknown"
@ -221,336 +219,10 @@ func ReadPackage(r io.Reader) (*Package, error) {
if err = pkg.readPackageInfo(t); err != nil {
return nil, err
}
}
}
}
// ReadPackageInfo reads a package description from a .PKGINFO file
func ReadPackageInfo(r io.Reader) (*Package, error) {
pkg := new(Package)
if n, ok := r.(namer); ok {
pkg.Filename = n.Name()
}
if err := pkg.readPackageInfo(r); err != nil {
return nil, err
}
if pkg.Name == "" {
return nil, ErrNoPackageName
}
if pkg.Version == "" {
return nil, ErrNoPackageVersion
}
if pkg.Release == "" {
return nil, ErrNoPackageRelease
}
return pkg, nil
}
// ReadPackageDesc reads a package description from a desc file
func ReadPackageDesc(r io.Reader) (*Package, error) {
pkg := new(Package)
if n, ok := r.(namer); ok {
pkg.Filename = n.Name()
}
if err := pkg.readPackageDesc(r); err != nil {
return nil, err
}
if pkg.Name == "" {
return nil, ErrNoPackageName
}
if pkg.Version == "" {
return nil, ErrNoPackageVersion
}
if pkg.Release == "" {
return nil, ErrNoPackageRelease
}
return pkg, nil
}
func (pkg *Package) readBuildInfo(r io.Reader) (err error) {
s := bufio.NewScanner(r)
for s.Scan() {
if err = s.Err(); err != nil {
if err == io.EOF {
err = nil
}
break
}
fields := strings.SplitN(s.Text(), " = ", 2)
if len(fields) != 2 {
continue
}
switch fields[0] {
case "buildenv":
pkg.BuildEnv = append(pkg.BuildEnv, fields[1])
case "options":
pkg.Options = append(pkg.Options, fields[1])
}
}
return
}
func (pkg *Package) readPackageInfo(r io.Reader) (err error) {
s := bufio.NewScanner(r)
for s.Scan() {
if err = s.Err(); err != nil {
if err == io.EOF {
err = nil
}
break
}
fields := strings.SplitN(s.Text(), " = ", 2)
if len(fields) != 2 {
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:
return fmt.Errorf("archlinux: unknown PKGINFO field %q", fields[0])
pkg.Files = append(pkg.Files, strings.TrimLeft(h.Name, "/"))
}
}
return
}
// WritePackageInfo writes a PKGINFO to the supplied Writer
func (pkg *Package) WritePackageInfo(w io.Writer) 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
)
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
}
func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
var (
s = bufio.NewScanner(r)
line string
lines []string
section string
)
for s.Scan() {
if err = s.Err(); err != nil {
if err == io.EOF {
err = nil
}
break
}
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
}
case "URL":
if pkg.URL, err = url.Parse(lines[0]); err != nil {
return
}
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":
var i int64
if i, err = strconv.ParseInt(lines[0], 10, 64); err != nil {
return
}
pkg.BuildDate = time.Unix(i, 0)
case "PACKAGER":
pkg.Packager = lines[0]
default:
return fmt.Errorf("archlinux: unsupported desc section %q", section)
}
} else if line[0] == '%' && line[len(line)-1] == '%' {
section = line[1 : len(line)-1]
lines = lines[:0]
} else {
lines = append(lines, line)
}
}
return
}
// ParsePackageArchiveName parsea an Arch Linux package archive name


+ 156
- 0
package_desc.go View File

@ -0,0 +1,156 @@
package archlinux
import (
"bufio"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
// ReadPackageDesc reads a package description from a desc file
func ReadPackageDesc(r io.Reader) (*Package, error) {
pkg := new(Package)
if n, ok := r.(namer); ok {
pkg.Filename = n.Name()
}
if err := pkg.readPackageDesc(r); err != nil {
return nil, err
}
if pkg.Name == "" {
return nil, ErrNoPackageName
}
if pkg.Version == "" {
return nil, ErrNoPackageVersion
}
if pkg.Release == "" {
return nil, ErrNoPackageRelease
}
return pkg, nil
}
func (pkg *Package) readPackageDesc(r io.Reader) (err error) {
var (
s = bufio.NewScanner(r)
line string
lines []string
section string
)
for s.Scan() {
if err = s.Err(); err != nil {
if err == io.EOF {
err = nil
}
break
}
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
}
case "URL":
if pkg.URL, err = url.Parse(lines[0]); err != nil {
return
}
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":
var i int64
if i, err = strconv.ParseInt(lines[0], 10, 64); err != nil {
return
}
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)
}
} else if line[0] == '%' && line[len(line)-1] == '%' {
section = line[1 : len(line)-1]
lines = lines[:0]
} else {
lines = append(lines, line)
}
}
return
}

+ 186
- 0
package_desc_test.go View File

@ -0,0 +1,186 @@
package archlinux
import (
"bytes"
"testing"
)
func TestReadPackageDesc(t *testing.T) {
pkg, err := ReadPackageDesc(bytes.NewBufferString(testReadPackageDesc))
if err != nil {
t.Fatal(err)
}
// TODO(maze): checks
_ = pkg
}
var (
testReadPackageDesc = `%FILENAME%
perl-5.26.0-4-x86_64.pkg.tar.xz
%NAME%
perl
%VERSION%
5.26.0-4
%DESC%
A highly capable, feature-rich programming language
%GROUPS%
base
%CSIZE%
14343632
%ISIZE%
54492160
%MD5SUM%
08a8b1fcab42ec43fdf07c4a5b1b2f50
%SHA256SUM%
ab958e0796650557a227f7eff56924ea8d03160803f8d061717b6013ee0045c1
%PGPSIG%
iQEzBAABCAAdFiEEhs/8qRjPOvRxR1iAUeixSKmZnDQFAlmew3MACgkQUeixSKmZnDRRhwf/Q0Ve+pIQAxknR0v3p2FyWgfuanwYCpWhIZgTCKGCmCntayBKZ0U9v41Mac7PJ11AJvntNWJ9vhmR3dqpj6AOw6lXa4AGXUCHOmo21FMYEPltcp27zRCk/7FHFHwxDd+8y0cU8x4l7QVmTq4odMjAQiimMEcxY/jXsoUX084SzOQ02ix2DA67spFLAD7BC9z6tmdH9ABXaRA8QL51uQxGaQ+fxc8FKXFjOhbNyPcjOZasiwvrLboB/nQRfygf0baBSzzbM/hOnHVgqc3WfAqE1y4Qve8cE1L+2hhXXHaFCj9/v6vI1JZGQlMYisbhYMuK0n4kM4RbX7N6I78IHuRuyg==
%URL%
http://www.perl.org
%LICENSE%
GPL
PerlArtistic
%ARCH%
x86_64
%BUILDDATE%
1503575763
%PACKAGER%
Evangelos Foutras <evangelos@foutrelis.com>
%PROVIDES%
perl-archive-tar=2.24
perl-attribute-handlers=0.99
perl-autodie=2.29
perl-autoloader=5.74
perl-autouse=1.11
perl-b-debug=1.24
perl-base=2.25
perl-bignum=0.47
perl-carp=1.42
perl-compress-raw-bzip2=2.074
perl-compress-raw-zlib=2.074
perl-config-perl-v=0.28
perl-constant=1.33
perl-cpan-meta-requirements=2.140
perl-cpan-meta-yaml=0.018
perl-cpan-meta=2.150010
perl-cpan=2.18
perl-data-dumper=2.167
perl-db_file=1.840
perl-devel-ppport=3.35
perl-devel-selfstubber=1.06
perl-digest-md5=2.55
perl-digest-sha=5.96
perl-digest=1.17_01
perl-dumpvalue=1.18
perl-encode=2.88
perl-encoding-warnings=0.13
perl-env=1.04
perl-experimental=0.016
perl-exporter=5.72
perl-extutils-cbuilder=0.280225
perl-extutils-constant=0.23
perl-extutils-install=2.04
perl-extutils-makemaker=7.24
perl-extutils-manifest=1.70
perl-extutils-parsexs=3.34
perl-file-fetch=0.52
perl-file-path=2.12_01
perl-file-temp=0.2304
perl-filter-simple=0.93
perl-filter-util-call=1.55
perl-getopt-long=2.49
perl-http-tiny=0.070
perl-i18n-collate=1.02
perl-i18n-langtags=0.42
perl-if=0.0606
perl-io-compress=2.074
perl-io-socket-ip=0.38
perl-io-zlib=1.10
perl-io=1.38
perl-ipc-cmd=0.96
perl-ipc-sysv=2.07
perl-json-pp=2.27400_02
perl-lib=0.64
perl-libnet=3.10
perl-locale-codes=3.42
perl-locale-maketext-simple=0.21_01
perl-locale-maketext=1.28
perl-math-bigint-fastcalc=0.5005
perl-math-bigint=1.999806
perl-math-bigrat=0.2611
perl-math-complex=1.5901
perl-memoize=1.03_01
perl-mime-base64=3.15
perl-module-corelist=5.20170530
perl-module-load-conditional=0.68
perl-module-load=0.32
perl-module-loaded=0.08
perl-module-metadata=1.000033
perl-net-ping=2.55
perl-params-check=0.38
perl-parent=0.236
perl-pathtools=3.67
perl-perl-ostype=1.010
perl-perlfaq=5.021011
perl-perlio-via-quotedprint=0.08
perl-pod-checker=1.73
perl-pod-escapes=1.07
perl-pod-parser=1.63
perl-pod-perldoc=3.28
perl-pod-simple=3.35
perl-pod-usage=1.69
perl-podlators=5.006
perl-safe=2.40
perl-scalar-list-utils=1.46_02
perl-search-dict=1.07
perl-selfloader=1.23
perl-socket=2.020_03
perl-storable=2.62
perl-sys-syslog=0.35
perl-term-ansicolor=4.06
perl-term-cap=1.17
perl-term-complete=1.403
perl-term-readline=1.16
perl-test-harness=3.38
perl-test-simple=1.302073
perl-test=1.30
perl-text-abbrev=1.02
perl-text-balanced=2.03
perl-text-parsewords=3.30
perl-text-tabs=2013.0523
perl-thread-queue=3.12
perl-thread-semaphore=2.13
perl-threads-shared=1.56
perl-threads=2.15
perl-tie-file=1.02
perl-tie-refhash=1.39
perl-time-hires=1.9741
perl-time-local=1.25
perl-time-piece=1.31
perl-unicode-collate=1.19
perl-unicode-normalize=1.25
perl-version=0.9917
perl-xsloader=0.27
%DEPENDS%
gdbm
db
glibc
`
)

+ 199
- 0
package_info.go View File

@ -0,0 +1,199 @@
package archlinux
import (
"bufio"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
// ReadPackageInfo reads a package description from a .PKGINFO file
func ReadPackageInfo(r io.Reader) (*Package, error) {
pkg := new(Package)
if n, ok := r.(namer); ok {
pkg.Filename = n.Name()
}
if err := pkg.readPackageInfo(r); err != nil {
return nil, err
}
if pkg.Name == "" {
return nil, ErrNoPackageName
}
if pkg.Version == "" {
return nil, ErrNoPackageVersion
}
if pkg.Release == "" {
return nil, ErrNoPackageRelease
}
return pkg, nil
}
func (pkg *Package) readBuildInfo(r io.Reader) (err error) {
s := bufio.NewScanner(r)
for s.Scan() {
if err = s.Err(); err != nil {
if err == io.EOF {
err = nil
}
break
}
fields := strings.SplitN(s.Text(), " = ", 2)
if len(fields) != 2 {
continue
}
switch fields[0] {
case "buildenv":
pkg.BuildEnv = append(pkg.BuildEnv, fields[1])
case "options":
pkg.Options = append(pkg.Options, fields[1])
}
}
return
}
func (pkg *Package) readPackageInfo(r io.Reader) (err error) {
s := bufio.NewScanner(r)
for s.Scan() {
if err = s.Err(); err != nil {
if err == io.EOF {
err = nil
}
break
}
fields := strings.SplitN(s.Text(), " = ", 2)
if len(fields) != 2 {
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:
return fmt.Errorf("archlinux: unknown PKGINFO field %q", fields[0])
}
}
return
}
// WritePackageInfo writes a PKGINFO to the supplied Writer
func (pkg *Package) WritePackageInfo(w io.Writer) 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
)
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
}

+ 192
- 0
package_info_test.go View File

@ -0,0 +1,192 @@
package archlinux
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"time"
)
func TestReadPackageInfo(t *testing.T) {
var tests = []struct {
PKGINFO string
Package
}{
{
testPackageInfo, Package{
Name: "xterm",
Version: "330",
Release: "1",
Arch: []string{"x86_64"},
Size: 851968,
},
},
{
testPackageInfo2, Package{
Name: "imagemagick",
Version: "6.9.9.15",
Release: "1",
Arch: []string{"x86_64"},
Size: 9897984,
},
},
}
for _, test := range tests {
t.Run(test.Package.String(), func(t *testing.T) {
p, err := ReadPackageInfo(bytes.NewBufferString(test.PKGINFO))
if err != nil {
t.Fatal(err)
}
testComparePackage(t, p, &test.Package)
})
}
}
var (
testPackageInfo = `# Generated by makepkg 5.0.2
# using fakeroot version 1.21
# Fri Jun 23 20:03:18 UTC 2017
pkgname = xterm
pkgver = 330-1
pkgdesc = X Terminal Emulator
url = http://invisible-island.net/xterm/
builddate = 1498248198
packager = Andreas Radke <andyrtr@archlinux.org>
size = 851968
arch = x86_64
license = custom
depend = libxft
depend = libxaw
depend = ncurses
depend = xorg-luit
depend = xbitmaps
depend = libutempter
depend = libxkbfile
`
testPackageInfo2 = `# Generated by makepkg 5.0.2
# using fakeroot version 1.22
# Sat Sep 23 21:53:45 UTC 2017
pkgname = imagemagick
pkgbase = imagemagick
pkgver = 6.9.9.15-1
pkgdesc = An image viewing/manipulation program
url = http://www.imagemagick.org/
builddate = 1506203625
packager = Antonio Rojas <arojas@archlinux.org>
size = 9897984
arch = x86_64
license = custom
backup = etc/ImageMagick-6/coder.xml
backup = etc/ImageMagick-6/colors.xml
backup = etc/ImageMagick-6/delegates.xml
backup = etc/ImageMagick-6/log.xml
backup = etc/ImageMagick-6/magic.xml
backup = etc/ImageMagick-6/mime.xml
backup = etc/ImageMagick-6/policy.xml
backup = etc/ImageMagick-6/quantization-table.xml
backup = etc/ImageMagick-6/thresholds.xml
backup = etc/ImageMagick-6/type.xml
backup = etc/ImageMagick-6/type-dejavu.xml
backup = etc/ImageMagick-6/type-ghostscript.xml
backup = etc/ImageMagick-6/type-windows.xml
depend = libltdl
depend = lcms2
depend = libxt
depend = fontconfig
depend = libxext
depend = liblqr
depend = libraqm
depend = opencl-icd-loader
depend = perl>=5.26
depend = perl<5.27
optdepend = imagemagick-doc: for additional information
optdepend = ghostscript: for Ghostscript support
optdepend = openexr: for OpenEXR support
optdepend = openjpeg2: for JP2 support
optdepend = libwmf: for WMF support
optdepend = librsvg: for SVG support
optdepend = libxml2: for XML support
optdepend = libpng: for PNG support
optdepend = libwebp: for WEBP support
optdepend = libraw: for DNG support
makedepend = libltdl
makedepend = lcms2
makedepend = libxt
makedepend = fontconfig
makedepend = libxext
makedepend = ghostscript
makedepend = openexr
makedepend = libwmf
makedepend = librsvg
makedepend = libxml2
makedepend = liblqr
makedepend = openjpeg2
makedepend = libraw
makedepend = libraqm
makedepend = opencl-headers
makedepend = opencl-icd-loader
makedepend = libwebp
makedepend = subversion
makedepend = glu
`
)
func TestReadPackageInfoError(t *testing.T) {
var tests = []struct {
PKGINFO string
Error string
}{
{testPackageInfoError, `unknown PKGINFO field`},
{testPackageInfoErrorName, "name is missing"},
{testPackageInfoErrorVersion, "version is missing"},
{testPackageInfoErrorVersionInvalid, "invalid version"},
{testPackageInfoErrorRelease, "release is missing"},
}
for _, test := range tests {
t.Run(test.Error, func(t *testing.T) {
_, err := ReadPackageInfo(bytes.NewBufferString(test.PKGINFO))
if err == nil {
t.Fatal("expected error")
}
if s := err.Error(); !strings.Contains(s, test.Error) {
t.Fatalf(`expected error %q, got %q`, test.Error, s)
}
})
}
}
var (
testPackageInfoError = `foo = bar`
testPackageInfoErrorName = `pkgver = 330-1`
testPackageInfoErrorVersion = `pkgname = xterm`
testPackageInfoErrorVersionInvalid = `pkgname = xterm
pkgver = 330`
testPackageInfoErrorRelease = `pkgname = xterm
pkgver = 330-`
)
func TestWritePackageInfo(t *testing.T) {
if err := testWritePackageInfo.WritePackageInfo(ioutil.Discard); err != nil {
t.Fatal(err)
}
}
var (
testWritePackageInfo = &Package{
Name: "imagemagick",
Base: "imagemagick",
Version: "6.9.9.15",
Release: "1",
Description: "An image viewing/manipulation program",
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"},
MakeDepends: []string{"libltdl", "lcms2", "libxt", "fontconfig", "libxext", "ghostscript", "openexr", "libwmf", "librsvg", "libxml2", "liblqr", "openjpeg2", "libraw", "libraqm", "opencl-headers", "opencl-icd-loader", "libwebp", "subversion", "glu"},
OptionalDepends: []string{"imagemagick-doc: for additional information", "ghostscript: for Ghostscript support", "openexr: for OpenEXR support", "openjpeg2: for JP2 support", "libwmf: for WMF support", "librsvg: for SVG support", "libxml2: for XML support", "libpng: for PNG support", "libwebp: for WEBP support", "libraw: for DNG support"},
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),
Packager: "Antonio Rojas <arojas@archlinux.org>",
}
)

+ 0
- 366
package_test.go
File diff suppressed because it is too large
View File


Loading…
Cancel
Save