package_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // package_test.go - Tests for package.go
  2. //
  3. // Copyright (c) 2013 The go-alpm Authors
  4. //
  5. // MIT Licensed. See LICENSE for details.
  6. package alpm
  7. import (
  8. "bytes"
  9. "fmt"
  10. "testing"
  11. "text/template"
  12. "time"
  13. )
  14. // Auxiliary formatting
  15. const pkginfo_template = `
  16. Name : {{ .Name }}
  17. Version : {{ .Version }}
  18. Architecture : {{ .Architecture }}
  19. Description : {{ .Description }}
  20. URL : {{ .URL }}
  21. Groups : {{ .Groups.Slice }}
  22. Licenses : {{ .Licenses.Slice }}
  23. Dependencies : {{ range .Depends.Slice }}{{ . }} {{ end }}
  24. Provides : {{ range .Provides.Slice }}{{ . }} {{ end }}
  25. Replaces : {{ range .Replaces.Slice }}{{ . }} {{ end }}
  26. Conflicts : {{ range .Conflicts.Slice }}{{ . }} {{ end }}
  27. Packager : {{ .Packager }}
  28. Build Date : {{ .PrettyBuildDate }}
  29. Install Date : {{ .PrettyInstallDate }}
  30. Package Size : {{ .Size }} bytes
  31. Install Size : {{ .ISize }} bytes
  32. MD5 Sum : {{ .MD5Sum }}
  33. SHA256 Sum : {{ .SHA256Sum }}
  34. Reason : {{ .Reason }}
  35. Required By : {{ .ComputeRequiredBy }}
  36. Files : {{ range .Files }}
  37. {{ .Name }} {{ .Size }}{{ end }}
  38. `
  39. var pkginfo_tpl *template.Template
  40. type PrettyPackage struct {
  41. Package
  42. }
  43. func (p PrettyPackage) PrettyBuildDate() string {
  44. return p.BuildDate().Format(time.RFC1123)
  45. }
  46. func (p PrettyPackage) PrettyInstallDate() string {
  47. return p.InstallDate().Format(time.RFC1123)
  48. }
  49. func init() {
  50. var er error
  51. pkginfo_tpl, er = template.New("info").Parse(pkginfo_template)
  52. if er != nil {
  53. fmt.Printf("couldn't compile template: %s\n", er)
  54. panic("template parsing error")
  55. }
  56. }
  57. // Tests package attribute getters.
  58. func TestPkginfo(t *testing.T) {
  59. h, er := Init(root, dbpath)
  60. defer h.Release()
  61. if er != nil {
  62. t.Errorf("Failed at alpm initialization: %s", er)
  63. }
  64. t.Log("Printing package information for pacman")
  65. db, _ := h.LocalDb()
  66. pkg, _ := db.PkgByName("pacman")
  67. buf := bytes.NewBuffer(nil)
  68. pkginfo_tpl.Execute(buf, PrettyPackage{*pkg})
  69. t.Logf("%s...", buf.Bytes()[:1024])
  70. pkg, _ = db.PkgByName("linux")
  71. if pkg != nil {
  72. buf = bytes.NewBuffer(nil)
  73. pkginfo_tpl.Execute(buf, PrettyPackage{*pkg})
  74. t.Logf("%s...", buf.Bytes()[:1024])
  75. }
  76. }