Arch Linux helpers and parsers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.9 KiB

  1. package archlinux
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "io"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestReadPackageInfo(t *testing.T) {
  14. var tests = []struct {
  15. PKGINFO string
  16. Package
  17. }{
  18. {
  19. testPackageInfo, Package{
  20. Name: "xterm",
  21. Version: "330",
  22. Release: "1",
  23. Arch: []string{"x86_64"},
  24. Size: 851968,
  25. },
  26. },
  27. {
  28. testPackageInfo2, Package{
  29. Name: "imagemagick",
  30. Version: "6.9.9.15",
  31. Release: "1",
  32. Arch: []string{"x86_64"},
  33. Size: 9897984,
  34. },
  35. },
  36. }
  37. for _, test := range tests {
  38. t.Run(test.Package.String(), func(t *testing.T) {
  39. p, err := ReadPackageInfo(bytes.NewBufferString(test.PKGINFO))
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. testComparePackage(t, p, &test.Package)
  44. })
  45. }
  46. }
  47. var (
  48. testPackageInfo = `# Generated by makepkg 5.0.2
  49. # using fakeroot version 1.21
  50. # Fri Jun 23 20:03:18 UTC 2017
  51. pkgname = xterm
  52. pkgver = 330-1
  53. pkgdesc = X Terminal Emulator
  54. url = http://invisible-island.net/xterm/
  55. builddate = 1498248198
  56. packager = Andreas Radke <andyrtr@archlinux.org>
  57. size = 851968
  58. arch = x86_64
  59. license = custom
  60. depend = libxft
  61. depend = libxaw
  62. depend = ncurses
  63. depend = xorg-luit
  64. depend = xbitmaps
  65. depend = libutempter
  66. depend = libxkbfile
  67. `
  68. testPackageInfo2 = `# Generated by makepkg 5.0.2
  69. # using fakeroot version 1.22
  70. # Sat Sep 23 21:53:45 UTC 2017
  71. pkgname = imagemagick
  72. pkgbase = imagemagick
  73. pkgver = 6.9.9.15-1
  74. pkgdesc = An image viewing/manipulation program
  75. url = http://www.imagemagick.org/
  76. builddate = 1506203625
  77. packager = Antonio Rojas <arojas@archlinux.org>
  78. size = 9897984
  79. arch = x86_64
  80. license = custom
  81. backup = etc/ImageMagick-6/coder.xml
  82. backup = etc/ImageMagick-6/colors.xml
  83. backup = etc/ImageMagick-6/delegates.xml
  84. backup = etc/ImageMagick-6/log.xml
  85. backup = etc/ImageMagick-6/magic.xml
  86. backup = etc/ImageMagick-6/mime.xml
  87. backup = etc/ImageMagick-6/policy.xml
  88. backup = etc/ImageMagick-6/quantization-table.xml
  89. backup = etc/ImageMagick-6/thresholds.xml
  90. backup = etc/ImageMagick-6/type.xml
  91. backup = etc/ImageMagick-6/type-dejavu.xml
  92. backup = etc/ImageMagick-6/type-ghostscript.xml
  93. backup = etc/ImageMagick-6/type-windows.xml
  94. depend = libltdl
  95. depend = lcms2
  96. depend = libxt
  97. depend = fontconfig
  98. depend = libxext
  99. depend = liblqr
  100. depend = libraqm
  101. depend = opencl-icd-loader
  102. depend = perl>=5.26
  103. depend = perl<5.27
  104. optdepend = imagemagick-doc: for additional information
  105. optdepend = ghostscript: for Ghostscript support
  106. optdepend = openexr: for OpenEXR support
  107. optdepend = openjpeg2: for JP2 support
  108. optdepend = libwmf: for WMF support
  109. optdepend = librsvg: for SVG support
  110. optdepend = libxml2: for XML support
  111. optdepend = libpng: for PNG support
  112. optdepend = libwebp: for WEBP support
  113. optdepend = libraw: for DNG support
  114. makedepend = libltdl
  115. makedepend = lcms2
  116. makedepend = libxt
  117. makedepend = fontconfig
  118. makedepend = libxext
  119. makedepend = ghostscript
  120. makedepend = openexr
  121. makedepend = libwmf
  122. makedepend = librsvg
  123. makedepend = libxml2
  124. makedepend = liblqr
  125. makedepend = openjpeg2
  126. makedepend = libraw
  127. makedepend = libraqm
  128. makedepend = opencl-headers
  129. makedepend = opencl-icd-loader
  130. makedepend = libwebp
  131. makedepend = subversion
  132. makedepend = glu
  133. `
  134. )
  135. func TestReadPackageInfoError(t *testing.T) {
  136. var tests = []struct {
  137. PKGINFO string
  138. Error string
  139. }{
  140. {testPackageInfoError, `unknown PKGINFO field`},
  141. {testPackageInfoErrorName, "name is missing"},
  142. {testPackageInfoErrorVersion, "version is missing"},
  143. //{testPackageInfoErrorVersionInvalid, "invalid version"},
  144. {testPackageInfoErrorRelease, "release is missing"},
  145. }
  146. for _, test := range tests {
  147. t.Run(test.Error, func(t *testing.T) {
  148. _, err := ReadPackageInfo(bytes.NewBufferString(test.PKGINFO))
  149. if err == nil {
  150. t.Fatal("expected error")
  151. }
  152. if s := err.Error(); !strings.Contains(s, test.Error) {
  153. t.Fatalf(`expected error %q, got %q`, test.Error, s)
  154. }
  155. })
  156. }
  157. }
  158. var (
  159. testPackageInfoError = `foo = bar`
  160. testPackageInfoErrorName = `pkgver = 330-1`
  161. testPackageInfoErrorVersion = `pkgname = xterm`
  162. testPackageInfoErrorVersionInvalid = `pkgname = xterm
  163. pkgver = 330`
  164. testPackageInfoErrorRelease = `pkgname = xterm
  165. pkgver = 330-`
  166. )
  167. func TestWritePackageInfo(t *testing.T) {
  168. var w io.Writer
  169. if os.Getenv("TEST_DEBUG_WRITER") != "" {
  170. w = os.Stdout
  171. } else {
  172. w = ioutil.Discard
  173. }
  174. n, err := testWritePackageInfo.WritePackageInfo(w)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. if n == 0 {
  179. t.Fatal(`expected >0 bytes to be written`)
  180. }
  181. t.Log(n, "bytes written")
  182. }
  183. var (
  184. testWritePackageInfoURL, _ = url.Parse("http://www.imagemagick.org/")
  185. 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==")
  186. testWritePackageInfo = &Package{
  187. Name: "imagemagick",
  188. Base: "imagemagick",
  189. Version: "6.9.9.15",
  190. Release: "1",
  191. Description: "An image viewing/manipulation program",
  192. URL: testWritePackageInfoURL,
  193. Arch: []string{"x86_64"},
  194. License: []string{"custom"},
  195. Depends: []string{"libltdl", "lcms2", "libxt", "fontconfig", "libxext", "liblqr", "libraqm", "opencl-icd-loader", "perl>=5.26", "perl<5.27"},
  196. MakeDepends: []string{"libltdl", "lcms2", "libxt", "fontconfig", "libxext", "ghostscript", "openexr", "libwmf", "librsvg", "libxml2", "liblqr", "openjpeg2", "libraw", "libraqm", "opencl-headers", "opencl-icd-loader", "libwebp", "subversion", "glu"},
  197. 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"},
  198. 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"},
  199. Size: 9897984,
  200. BuildDate: time.Unix(1506203625, 0),
  201. MD5: []byte("\x54\x71\xa4\x62\x04\xd5\x25\xcb\x3c\x3c\xcf\xc8\x58\x71\x1e\x02"),
  202. 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"),
  203. PGPSignature: testWritePackagePGPSignature,
  204. Packager: "Antonio Rojas <arojas@archlinux.org>",
  205. }
  206. )