import-github.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-import-github)
  19. #:use-module (json)
  20. #:use-module (srfi srfi-35)
  21. #:use-module (srfi srfi-64)
  22. #:use-module (guix git-download)
  23. #:use-module (guix http-client)
  24. #:use-module (guix import github)
  25. #:use-module (guix packages)
  26. #:use-module (guix tests)
  27. #:use-module (guix upstream)
  28. #:use-module (ice-9 match))
  29. (test-begin "github")
  30. (define (call-with-releases thunk tags releases)
  31. (mock ((guix http-client) http-fetch
  32. (lambda* (uri #:key headers)
  33. (unless (string-prefix? "mock://" uri)
  34. (error "the URI ~a should not be used" uri))
  35. (define components
  36. (string-split (substring uri 8) #\/))
  37. (pk 'stuff components headers)
  38. (define (scm->json-port scm)
  39. (open-input-string (scm->json-string scm)))
  40. (match components
  41. (("repos" "foo" "foomatics" "releases")
  42. (scm->json-port releases))
  43. (("repos" "foo" "foomatics" "tags")
  44. (scm->json-port tags))
  45. (rest (error "TODO ~a" rest)))))
  46. (parameterize ((%github-api "mock://"))
  47. (thunk))))
  48. ;; Copied from tests/minetest.scm
  49. (define (upstream-source->sexp upstream-source)
  50. (define url (upstream-source-urls upstream-source))
  51. (unless (git-reference? url)
  52. (error "a <git-reference> is expected"))
  53. `(,(upstream-source-package upstream-source)
  54. ,(upstream-source-version upstream-source)
  55. ,(git-reference-url url)
  56. ,(git-reference-commit url)))
  57. (define* (expected-sexp new-version new-commit)
  58. `("foomatics" ,new-version "https://github.com/foo/foomatics" ,new-commit))
  59. (define (example-package old-version old-commit)
  60. (package
  61. (name "foomatics")
  62. (version old-version)
  63. (source
  64. (origin
  65. (method git-fetch)
  66. (uri (git-reference
  67. (url "https://github.com/foo/foomatics")
  68. (commit old-commit)))
  69. (sha256 #f) ; not important for following tests
  70. (file-name (git-file-name name version))))
  71. (build-system #f)
  72. (license #f)
  73. (synopsis #f)
  74. (description #f)
  75. (home-page #f)))
  76. (define* (found-sexp old-version old-commit tags releases)
  77. (and=>
  78. (call-with-releases (lambda ()
  79. ((upstream-updater-latest %github-updater)
  80. (example-package old-version old-commit)))
  81. tags releases)
  82. upstream-source->sexp))
  83. (define-syntax-rule (test-release test-case old-version
  84. old-commit new-version new-commit
  85. tags releases)
  86. (test-equal test-case
  87. (expected-sexp new-version new-commit)
  88. (found-sexp old-version old-commit tags releases)))
  89. (test-release "newest release is choosen"
  90. "1.0.0" "v1.0.0" "1.9" "v1.9"
  91. #()
  92. ;; a mixture of current, older and newer versions
  93. #((("tag_name" . "v0.0"))
  94. (("tag_name" . "v1.0.1"))
  95. (("tag_name" . "v1.9"))
  96. (("tag_name" . "v1.0.0"))
  97. (("tag_name" . "v1.0.2"))))
  98. (test-release "tags are used when there are no formal releases"
  99. "1.0.0" "v1.0.0" "1.9" "v1.9"
  100. ;; a mixture of current, older and newer versions
  101. #((("name" . "v0.0"))
  102. (("name" . "v1.0.1"))
  103. (("name" . "v1.9"))
  104. (("name" . "v1.0.0"))
  105. (("name" . "v1.0.2")))
  106. #())
  107. (test-release "\"version-\" prefixes are recognised"
  108. "1.0.0" "v1.0.0" "1.9" "version-1.9"
  109. #((("name" . "version-1.9")))
  110. #())
  111. (test-release "prefixes are optional"
  112. "1.0.0" "v1.0.0" "1.9" "1.9"
  113. #((("name" . "1.9")))
  114. #())
  115. (test-release "prefixing by package name is acceptable"
  116. "1.0.0" "v1.0.0" "1.9" "foomatics-1.9"
  117. #((("name" . "foomatics-1.9")))
  118. #())
  119. (test-release "not all prefixes are acceptable"
  120. "1.0.0" "v1.0.0" "1.0.0" "v1.0.0"
  121. #((("name" . "v1.0.0"))
  122. (("name" . "barstatics-1.9")))
  123. #())
  124. (test-end "github")