go.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 François Joulaud <francois.joulaud@radiofrance.com>
  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. ;;; Summary
  19. ;; Tests for guix/import/go.scm
  20. (define-module (test-import-go)
  21. #:use-module (guix base32)
  22. #:use-module (guix build-system go)
  23. #:use-module (guix import go)
  24. #:use-module (guix base32)
  25. #:use-module ((guix utils) #:select (call-with-temporary-directory))
  26. #:use-module (guix tests)
  27. #:use-module (ice-9 match)
  28. #:use-module (srfi srfi-19)
  29. #:use-module (srfi srfi-64)
  30. #:use-module (web response))
  31. (define parse-go.mod
  32. (@@ (guix import go) parse-go.mod))
  33. (define fixture-go-mod-simple
  34. "module my/thing
  35. go 1.12
  36. require other/thing v1.0.2
  37. require new/thing/v2 v2.3.4
  38. exclude old/thing v1.2.3
  39. replace bad/thing v1.4.5 => good/thing v1.4.5
  40. ")
  41. (define fixture-go-mod-with-block
  42. "module M
  43. require (
  44. A v1
  45. B v1.0.0
  46. C v1.0.0
  47. D v1.2.3
  48. E dev
  49. )
  50. exclude D v1.2.3
  51. ")
  52. (define fixture-go-mod-complete
  53. "module M
  54. go 1.13
  55. replace github.com/myname/myproject/myapi => ./api
  56. replace github.com/mymname/myproject/thissdk => ../sdk
  57. replace launchpad.net/gocheck => github.com/go-check/check v0.0.0-20140225173054-eb6ee6f84d0a
  58. require (
  59. github.com/user/project v1.1.11
  60. github.com/user/project/sub/directory v1.1.12
  61. bitbucket.org/user/project v1.11.20
  62. bitbucket.org/user/project/sub/directory v1.11.21
  63. launchpad.net/project v1.1.13
  64. launchpad.net/project/series v1.1.14
  65. launchpad.net/project/series/sub/directory v1.1.15
  66. launchpad.net/~user/project/branch v1.1.16
  67. launchpad.net/~user/project/branch/sub/directory v1.1.17
  68. hub.jazz.net/git/user/project v1.1.18
  69. hub.jazz.net/git/user/project/sub/directory v1.1.19
  70. k8s.io/kubernetes/subproject v1.1.101
  71. one.example.com/abitrary/repo v1.1.111
  72. two.example.com/abitrary/repo v0.0.2
  73. \"quoted.example.com/abitrary/repo\" v0.0.2
  74. )
  75. replace two.example.com/abitrary/repo => github.com/corp/arbitrary-repo v0.0.2
  76. replace (
  77. golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13
  78. golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13
  79. )
  80. ")
  81. (define fixture-latest-for-go-check
  82. "{\"Version\":\"v0.0.0-20201130134442-10cb98267c6c\",\"Time\":\"2020-11-30T13:44:42Z\"}")
  83. (define fixtures-go-check-test
  84. (let ((version
  85. "{\"Version\":\"v0.0.0-20201130134442-10cb98267c6c\",\"Time\":\"2020-11-30T13:44:42Z\"}")
  86. (go.mod
  87. "module gopkg.in/check.v1
  88. go 1.11
  89. require github.com/kr/pretty v0.2.1
  90. ")
  91. (go-get
  92. "<!DOCTYPE html>
  93. <html lang=\"en\" >
  94. <head>
  95. <meta charset=\"utf-8\">
  96. <link rel=\"dns-prefetch\" href=\"https://github.githubassets.com\">
  97. <script crossorigin=\"anonymous\" defer=\"defer\" integrity=\"sha512-aw5tciVT0IsECUmMuwp9ez60QReE2/yFNL1diLgZnOom6RhU8+0lG3RlAKto4JwbCoEP15E41Pksd7rK5BKfCQ==\" type=\"application/javascript\" src=\"https://github.githubassets.com/assets/topic-suggestions-6b0e6d72.js\"></script>
  98. <meta name=\"viewport\" content=\"width=device-width\">
  99. <title>GitHub - go-check/check: Rich testing for the Go language</title>
  100. <meta name=\"description\" content=\"Rich testing for the Go language. Contribute to go-check/check development by creating an account on GitHub.\">
  101. <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"/opensearch.xml\" title=\"GitHub\">
  102. <link rel=\"fluid-icon\" href=\"https://github.com/fluidicon.png\" title=\"GitHub\">
  103. <!-- To prevent page flashing, the optimizely JS needs to be loaded in the
  104. <head> tag before the DOM renders -->
  105. <meta name=\"hostname\" content=\"github.com\">
  106. <meta name=\"user-login\" content=\"\">
  107. <link href=\"https://github.com/go-check/check/commits/v1.atom\" rel=\"alternate\" title=\"Recent Commits to check:v1\" type=\"application/atom+xml\">
  108. <meta name=\"go-import\" content=\"github.com/go-check/check git https://github.com/go-check/check.git\">
  109. </head>
  110. <body class=\"logged-out env-production page-responsive\" style=\"word-wrap: break-word;\">
  111. </body>
  112. </html>
  113. ")
  114. (pkg.go.dev "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\">\n</head>\n<body class=\"Site Site--wide Site--redesign\">\n <div class=\"Site-content\">\n <div class=\"Container\">\n <div class=\"UnitDetails\" data-test-id=\"UnitDetails\">\n <div class=\"UnitDetails-content js-unitDetailsContent\" role=\"main\" data-test-id=\"UnitDetails-content\">\n <div class=\"UnitReadme js-readme\">\n <h2 class=\"UnitReadme-title\" id=\"section-readme\"><img height=\"25px\" width=\"20px\" src=\"/static/img/pkg-icon-readme_20x16.svg\" alt=\"\">README</h2>\n <div class=\"UnitReadme-content\" data-test-id=\"Unit-readmeContent\">\n <div class=\"Overview-readmeContent js-readmeContent\">\n <h3 class=\"h1\" id=\"readme-instructions\">Instructions</h3>\n <p>Install the package with:</p>\n <pre><code>go get gopkg.in/check.v1\n</code></pre>\n </div>\n <div class=\"UnitReadme-fadeOut\"></div>\n </div>\n </div>\n <div class=\"UnitDoc\">\n <h2 class=\"UnitDoc-title\" id=\"section-documentation\"><img height=\"25px\" width=\"20px\" src=\"/static/img/pkg-icon-doc_20x12.svg\" alt=\"\">Documentation</h2>\n <div class=\"Documentation js-documentation\">\n <div class=\"Documentation-content js-docContent\">\n <section class=\"Documentation-overview\">\n <h3 tabindex=\"-1\" id=\"pkg-overview\" class=\"Documentation-overviewHeader\">Overview <a href=\"#pkg-overview\">¶</a></h3>\n <div role=\"navigation\" aria-label=\"Table of Contents\">\n <ul class=\"Documentation-toc\"></ul>\n </div>\n <p>Package check is a rich testing extension for Go's testing package.</p>\n <p>For details about the project, see:</p>\n <pre><a href=\"http://labix.org/gocheck\">http://labix.org/gocheck</a>\n</pre>\n </section>\n <h3 tabindex=\"-1\" id=\"pkg-constants\" class=\"Documentation-constantsHeader\">Constants <a href=\"#pkg-constants\">¶</a></h3>\n </div>\n </div>\n </div>\n </div>\n </div>\n </div>\n </div>\n</body>\n</html>\n")
  115. (pkg.go.dev-licence "<!DOCTYPE html>\n<html lang=\"en\">\n<meta charset=\"utf-8\">\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n<body class=\"Site Site--wide Site--redesign\">\n <div class=\"Unit-content\" role=\"main\">\n <section class=\"License\" id=\"lic-0\">\n <h2><div id=\"#lic-0\">BSD-2-Clause</div></h2>\n <p>This is not legal advice. <a href=\"/license-policy\">Read disclaimer.</a></p>\n <pre class=\"License-contents\">Gocheck - A rich testing framework for Go\n \nCopyright line\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met: \n\n1. Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer. \n2. Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution. \n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &#34;AS IS&#34; AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n</pre>\n </section>\n <div class=\"License-source\">Source: github.com/go-check/check@v0.0.0-20201128035030-22ab2dfb190c/LICENSE</div>\n </div>\n </div>\n"))
  116. `(("https://proxy.golang.org/github.com/go-check/check/@v/v0.0.0-20201130134442-10cb98267c6c.mod"
  117. . ,go.mod)
  118. ("https://proxy.golang.org/github.com/go-check/check/@latest"
  119. . ,version)
  120. ("https://github.com/go-check/check?go-get=1"
  121. . ,go-get)
  122. ("https://pkg.go.dev/github.com/go-check/check"
  123. . ,pkg.go.dev)
  124. ("https://pkg.go.dev/github.com/go-check/check?tab=licenses"
  125. . ,pkg.go.dev-licence))))
  126. (test-begin "go")
  127. ;;; Unit tests for go build-system
  128. (test-equal "go-version basic"
  129. "v1.0.2"
  130. (go-version->git-ref "v1.0.2"))
  131. (test-equal "go-version omited 'v' character"
  132. "v1.0.2"
  133. (go-version->git-ref "v1.0.2"))
  134. (test-equal "go-version with embeded git-ref"
  135. "65e3620a7ae7"
  136. (go-version->git-ref "v0.0.0-20190821162956-65e3620a7ae7"))
  137. (test-equal "go-version with complex embeded git-ref"
  138. "daa7c04131f5"
  139. (go-version->git-ref "v1.2.4-0.20191109021931-daa7c04131f5"))
  140. ;;; Unit tests for (guix import go)
  141. (test-equal "go-path-escape"
  142. "github.com/!azure/!avere"
  143. ((@@ (guix import go) go-path-escape) "github.com/Azure/Avere"))
  144. ;; We define a function for all similar tests with different go.mod files
  145. (define (testing-parse-mod name expected input)
  146. (define (inf? p1 p2)
  147. (string<? (car p1) (car p2)))
  148. (let ((input-port (open-input-string input)))
  149. (test-equal name
  150. (sort expected inf?)
  151. (sort
  152. ( (@@ (guix import go) parse-go.mod)
  153. input-port)
  154. inf?))))
  155. (testing-parse-mod "parse-go.mod-simple"
  156. '(("good/thing" . "v1.4.5")
  157. ("new/thing/v2" . "v2.3.4")
  158. ("other/thing" . "v1.0.2"))
  159. fixture-go-mod-simple)
  160. (testing-parse-mod "parse-go.mod-with-block"
  161. '(("A" . "v1")
  162. ("B" . "v1.0.0")
  163. ("C" . "v1.0.0")
  164. ("D" . "v1.2.3")
  165. ("E" . "dev"))
  166. fixture-go-mod-with-block)
  167. (testing-parse-mod "parse-go.mod-complete"
  168. '(("github.com/corp/arbitrary-repo" . "v0.0.2")
  169. ("quoted.example.com/abitrary/repo" . "v0.0.2")
  170. ("one.example.com/abitrary/repo" . "v1.1.111")
  171. ("hub.jazz.net/git/user/project/sub/directory" . "v1.1.19")
  172. ("hub.jazz.net/git/user/project" . "v1.1.18")
  173. ("launchpad.net/~user/project/branch/sub/directory" . "v1.1.17")
  174. ("launchpad.net/~user/project/branch" . "v1.1.16")
  175. ("launchpad.net/project/series/sub/directory" . "v1.1.15")
  176. ("launchpad.net/project/series" . "v1.1.14")
  177. ("launchpad.net/project" . "v1.1.13")
  178. ("bitbucket.org/user/project/sub/directory" . "v1.11.21")
  179. ("bitbucket.org/user/project" . "v1.11.20")
  180. ("k8s.io/kubernetes/subproject" . "v1.1.101")
  181. ("github.com/user/project/sub/directory" . "v1.1.12")
  182. ("github.com/user/project" . "v1.1.11")
  183. ("github.com/go-check/check" . "v0.0.0-20140225173054-eb6ee6f84d0a"))
  184. fixture-go-mod-complete)
  185. ;;; End-to-end tests for (guix import go)
  186. (define (mock-http-fetch testcase)
  187. (lambda (url . rest)
  188. (let ((body (assoc-ref testcase url)))
  189. (if body
  190. (open-input-string body)
  191. (error "mocked http-fetch Unexpected URL: " url)))))
  192. (define (mock-http-get testcase)
  193. (lambda (url . rest)
  194. (let ((body (assoc-ref testcase url))
  195. (response-header
  196. (build-response
  197. #:version '(1 . 1)
  198. #:code 200
  199. #:reason-phrase "Ok"
  200. #:headers `(
  201. (content-type text/html (charset . "utf-8"))
  202. (date . ,(make-date 0 10 58 12 6 3 2021 0))
  203. (transfer-encoding (chunked)))
  204. #:port #f
  205. #:validate-headers? #t)))
  206. (if body
  207. (values response-header body)
  208. (error "mocked http-get Unexpected URL: " url)))))
  209. (test-equal "go-module->guix-package"
  210. '(package
  211. (name "go-github-com-go-check-check")
  212. (version "0.0.0-20201130134442-10cb98267c6c")
  213. (source
  214. (origin
  215. (method git-fetch)
  216. (uri (git-reference
  217. (url "https://github.com/go-check/check.git")
  218. (commit (go-version->git-ref version))))
  219. (file-name (git-file-name name version))
  220. (sha256
  221. (base32
  222. "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5"))))
  223. (build-system go-build-system)
  224. (arguments
  225. (quote (#:import-path "github.com/go-check/check")))
  226. (inputs
  227. (quasiquote (("go-github-com-kr-pretty"
  228. (unquote go-github-com-kr-pretty)))))
  229. (home-page "https://github.com/go-check/check")
  230. (synopsis "Instructions")
  231. (description #f)
  232. (license license:bsd-2))
  233. ;; Replace network resources with sample data.
  234. (call-with-temporary-directory
  235. (lambda (checkout)
  236. (mock ((web client) http-get
  237. (mock-http-get fixtures-go-check-test))
  238. (mock ((guix http-client) http-fetch
  239. (mock-http-fetch fixtures-go-check-test))
  240. (mock ((guix git) update-cached-checkout
  241. (lambda* (url #:key ref)
  242. ;; Return an empty directory and its hash.
  243. (values checkout
  244. (nix-base32-string->bytevector
  245. "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5")
  246. #f)))
  247. (go-module->guix-package "github.com/go-check/check")))))))
  248. (test-end "go")