import-github.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 (web uri)
  29. #:use-module (ice-9 match))
  30. (test-begin "github")
  31. (define (call-with-releases thunk tags releases)
  32. (mock ((guix build download) open-connection-for-uri
  33. (lambda _
  34. ;; Return a fake socket.
  35. (%make-void-port "w+0")))
  36. (mock ((guix http-client) http-fetch
  37. (lambda* (uri #:key headers #:allow-other-keys)
  38. (let ((uri (if (string? uri)
  39. (string->uri uri)
  40. uri)))
  41. (unless (eq? 'mock (uri-scheme uri))
  42. (error "the URI ~a should not be used" uri))
  43. (define components
  44. (string-tokenize (uri-path uri)
  45. (char-set-complement (char-set #\/))))
  46. (pk 'stuff components headers)
  47. (define (scm->json-port scm)
  48. (open-input-string (scm->json-string scm)))
  49. (match components
  50. (("repos" "foo" "foomatics" "releases")
  51. (scm->json-port releases))
  52. (("repos" "foo" "foomatics" "tags")
  53. (scm->json-port tags))
  54. (rest (error "TODO ~a" rest))))))
  55. (parameterize ((%github-api "mock://"))
  56. (thunk)))))
  57. ;; Copied from tests/minetest.scm
  58. (define (upstream-source->sexp upstream-source)
  59. (define url (upstream-source-urls upstream-source))
  60. (unless (git-reference? url)
  61. (error "a <git-reference> is expected"))
  62. `(,(upstream-source-package upstream-source)
  63. ,(upstream-source-version upstream-source)
  64. ,(git-reference-url url)
  65. ,(git-reference-commit url)))
  66. (define* (expected-sexp new-version new-commit)
  67. `("foomatics" ,new-version "https://github.com/foo/foomatics" ,new-commit))
  68. (define (example-package old-version old-commit)
  69. (package
  70. (name "foomatics")
  71. (version old-version)
  72. (source
  73. (origin
  74. (method git-fetch)
  75. (uri (git-reference
  76. (url "https://github.com/foo/foomatics")
  77. (commit old-commit)))
  78. (sha256 #f) ; not important for following tests
  79. (file-name (git-file-name name version))))
  80. (build-system #f)
  81. (license #f)
  82. (synopsis #f)
  83. (description #f)
  84. (home-page #f)))
  85. (define* (found-sexp old-version old-commit tags releases)
  86. (and=>
  87. (call-with-releases (lambda ()
  88. ((upstream-updater-latest %github-updater)
  89. (example-package old-version old-commit)))
  90. tags releases)
  91. upstream-source->sexp))
  92. (define-syntax-rule (test-release test-case old-version
  93. old-commit new-version new-commit
  94. tags releases)
  95. (test-equal test-case
  96. (expected-sexp new-version new-commit)
  97. (found-sexp old-version old-commit tags releases)))
  98. (test-release "newest release is choosen"
  99. "1.0.0" "v1.0.0" "1.9" "v1.9"
  100. #()
  101. ;; a mixture of current, older and newer versions
  102. #((("tag_name" . "v0.0"))
  103. (("tag_name" . "v1.0.1"))
  104. (("tag_name" . "v1.9"))
  105. (("tag_name" . "v1.0.0"))
  106. (("tag_name" . "v1.0.2"))))
  107. (test-release "tags are used when there are no formal releases"
  108. "1.0.0" "v1.0.0" "1.9" "v1.9"
  109. ;; a mixture of current, older and newer versions
  110. #((("name" . "v0.0"))
  111. (("name" . "v1.0.1"))
  112. (("name" . "v1.9"))
  113. (("name" . "v1.0.0"))
  114. (("name" . "v1.0.2")))
  115. #())
  116. (test-release "\"version-\" prefixes are recognised"
  117. "1.0.0" "v1.0.0" "1.9" "version-1.9"
  118. #((("name" . "version-1.9")))
  119. #())
  120. (test-release "prefixes are optional"
  121. "1.0.0" "v1.0.0" "1.9" "1.9"
  122. #((("name" . "1.9")))
  123. #())
  124. (test-release "prefixing by package name is acceptable"
  125. "1.0.0" "v1.0.0" "1.9" "foomatics-1.9"
  126. #((("name" . "foomatics-1.9")))
  127. #())
  128. (test-release "not all prefixes are acceptable"
  129. "1.0.0" "v1.0.0" "1.0.0" "v1.0.0"
  130. #((("name" . "v1.0.0"))
  131. (("name" . "barstatics-1.9")))
  132. #())
  133. (test-end "github")