Merge remote-tracking branch 'origin/master' into core-updates
[jackhill/guix/guix.git] / tests / go.scm
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
19 ;;; Summary
20 ;; Tests for guix/import/go.scm
21
22 (define-module (tests-import-go)
23 #:use-module (guix base32)
24 #:use-module (guix build-system go)
25 #:use-module (guix import go)
26 #:use-module (guix base32)
27 #:use-module ((guix utils) #:select (call-with-temporary-directory))
28 #:use-module (guix tests)
29 #:use-module (ice-9 match)
30 #:use-module (srfi srfi-19)
31 #:use-module (srfi srfi-64)
32 #:use-module (web response))
33
34 (define parse-go.mod
35 (@@ (guix import go) parse-go.mod))
36
37 (define fixture-go-mod-simple
38 "module my/thing
39 go 1.12
40 require other/thing v1.0.2
41 require new/thing/v2 v2.3.4
42 exclude old/thing v1.2.3
43 replace bad/thing v1.4.5 => good/thing v1.4.5
44 ")
45
46 (define fixture-go-mod-with-block
47 "module M
48
49 require (
50 A v1
51 B v1.0.0
52 C v1.0.0
53 D v1.2.3
54 E dev
55 )
56
57 exclude D v1.2.3
58 ")
59
60
61 (define fixture-go-mod-complete
62 "module M
63
64 go 1.13
65
66 replace github.com/myname/myproject/myapi => ./api
67
68 replace github.com/mymname/myproject/thissdk => ../sdk
69
70 replace launchpad.net/gocheck => github.com/go-check/check v0.0.0-20140225173054-eb6ee6f84d0a
71
72 require (
73 github.com/user/project v1.1.11
74 github.com/user/project/sub/directory v1.1.12
75 bitbucket.org/user/project v1.11.20
76 bitbucket.org/user/project/sub/directory v1.11.21
77 launchpad.net/project v1.1.13
78 launchpad.net/project/series v1.1.14
79 launchpad.net/project/series/sub/directory v1.1.15
80 launchpad.net/~user/project/branch v1.1.16
81 launchpad.net/~user/project/branch/sub/directory v1.1.17
82 hub.jazz.net/git/user/project v1.1.18
83 hub.jazz.net/git/user/project/sub/directory v1.1.19
84 k8s.io/kubernetes/subproject v1.1.101
85 one.example.com/abitrary/repo v1.1.111
86 two.example.com/abitrary/repo v0.0.2
87 \"quoted.example.com/abitrary/repo\" v0.0.2
88 )
89
90 replace two.example.com/abitrary/repo => github.com/corp/arbitrary-repo v0.0.2
91
92 replace (
93 golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13
94 golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13
95 )
96
97 ")
98
99
100
101 (define fixture-latest-for-go-check
102 "{\"Version\":\"v0.0.0-20201130134442-10cb98267c6c\",\"Time\":\"2020-11-30T13:44:42Z\"}")
103
104
105 (define fixtures-go-check-test
106 (let ((version
107 "{\"Version\":\"v0.0.0-20201130134442-10cb98267c6c\",\"Time\":\"2020-11-30T13:44:42Z\"}")
108 (go.mod
109 "module gopkg.in/check.v1
110
111 go 1.11
112
113 require github.com/kr/pretty v0.2.1
114 ")
115 (go-get
116 "<!DOCTYPE html>
117 <html lang=\"en\" >
118 <head>
119 <meta charset=\"utf-8\">
120 <link rel=\"dns-prefetch\" href=\"https://github.githubassets.com\">
121 <script crossorigin=\"anonymous\" defer=\"defer\" integrity=\"sha512-aw5tciVT0IsECUmMuwp9ez60QReE2/yFNL1diLgZnOom6RhU8+0lG3RlAKto4JwbCoEP15E41Pksd7rK5BKfCQ==\" type=\"application/javascript\" src=\"https://github.githubassets.com/assets/topic-suggestions-6b0e6d72.js\"></script>
122 <meta name=\"viewport\" content=\"width=device-width\">
123
124 <title>GitHub - go-check/check: Rich testing for the Go language</title>
125 <meta name=\"description\" content=\"Rich testing for the Go language. Contribute to go-check/check development by creating an account on GitHub.\">
126 <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"/opensearch.xml\" title=\"GitHub\">
127 <link rel=\"fluid-icon\" href=\"https://github.com/fluidicon.png\" title=\"GitHub\">
128 <!-- To prevent page flashing, the optimizely JS needs to be loaded in the
129 <head> tag before the DOM renders -->
130 <meta name=\"hostname\" content=\"github.com\">
131 <meta name=\"user-login\" content=\"\">
132 <link href=\"https://github.com/go-check/check/commits/v1.atom\" rel=\"alternate\" title=\"Recent Commits to check:v1\" type=\"application/atom+xml\">
133 <meta name=\"go-import\" content=\"github.com/go-check/check git https://github.com/go-check/check.git\">
134 </head>
135 <body class=\"logged-out env-production page-responsive\" style=\"word-wrap: break-word;\">
136 </body>
137 </html>
138 ")
139 (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")
140 (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"))
141 `(("https://proxy.golang.org/github.com/go-check/check/@v/v0.0.0-20201130134442-10cb98267c6c.mod"
142 . ,go.mod)
143 ("https://proxy.golang.org/github.com/go-check/check/@latest"
144 . ,version)
145 ("https://github.com/go-check/check?go-get=1"
146 . ,go-get)
147 ("https://pkg.go.dev/github.com/go-check/check"
148 . ,pkg.go.dev)
149 ("https://pkg.go.dev/github.com/go-check/check?tab=licenses"
150 . ,pkg.go.dev-licence)
151 ("https://proxy.golang.org/github.com/go-check/check/@v/list" . ""))))
152
153 (test-begin "go")
154
155 ;;; Unit tests for go build-system
156
157 (test-equal "go-version basic"
158 "v1.0.2"
159 (go-version->git-ref "v1.0.2"))
160
161 (test-equal "go-version omited 'v' character"
162 "v1.0.2"
163 (go-version->git-ref "v1.0.2"))
164
165 (test-equal "go-version with embeded git-ref"
166 "65e3620a7ae7"
167 (go-version->git-ref "v0.0.0-20190821162956-65e3620a7ae7"))
168
169 (test-equal "go-version with complex embeded git-ref"
170 "daa7c04131f5"
171 (go-version->git-ref "v1.2.4-0.20191109021931-daa7c04131f5"))
172
173 (test-assert "go-pseudo-version? multi-digit version number"
174 (go-pseudo-version? "v1.23.1-0.20200526195155-81db48ad09cc"))
175
176 (test-assert "go-pseudo-version? semantic version with rc"
177 (go-pseudo-version? "v1.4.0-rc.4.0.20200313231945-b860323f09d0"))
178
179 ;;; Unit tests for (guix import go)
180
181 (test-equal "go-path-escape"
182 "github.com/!azure/!avere"
183 ((@@ (guix import go) go-path-escape) "github.com/Azure/Avere"))
184
185
186 ;; We define a function for all similar tests with different go.mod files
187 (define (testing-parse-mod name expected input)
188 (define (inf? p1 p2)
189 (string<? (car p1) (car p2)))
190 (test-equal name
191 (sort expected inf?)
192 (sort ((@@ (guix import go) parse-go.mod) input) inf?)))
193
194 (testing-parse-mod "parse-go.mod-simple"
195 '(("good/thing" "v1.4.5")
196 ("new/thing/v2" "v2.3.4")
197 ("other/thing" "v1.0.2"))
198 fixture-go-mod-simple)
199
200 (testing-parse-mod "parse-go.mod-with-block"
201 '(("A" "v1")
202 ("B" "v1.0.0")
203 ("C" "v1.0.0")
204 ("D" "v1.2.3")
205 ("E" "dev"))
206 fixture-go-mod-with-block)
207
208 (testing-parse-mod
209 "parse-go.mod-complete"
210 '(("github.com/corp/arbitrary-repo" "v0.0.2")
211 ("quoted.example.com/abitrary/repo" "v0.0.2")
212 ("one.example.com/abitrary/repo" "v1.1.111")
213 ("hub.jazz.net/git/user/project/sub/directory" "v1.1.19")
214 ("hub.jazz.net/git/user/project" "v1.1.18")
215 ("launchpad.net/~user/project/branch/sub/directory" "v1.1.17")
216 ("launchpad.net/~user/project/branch" "v1.1.16")
217 ("launchpad.net/project/series/sub/directory" "v1.1.15")
218 ("launchpad.net/project/series" "v1.1.14")
219 ("launchpad.net/project" "v1.1.13")
220 ("bitbucket.org/user/project/sub/directory" "v1.11.21")
221 ("bitbucket.org/user/project" "v1.11.20")
222 ("k8s.io/kubernetes/subproject" "v1.1.101")
223 ("github.com/user/project/sub/directory" "v1.1.12")
224 ("github.com/user/project" "v1.1.11")
225 ("github.com/go-check/check" "v0.0.0-20140225173054-eb6ee6f84d0a"))
226 fixture-go-mod-complete)
227
228 ;;; End-to-end tests for (guix import go)
229 (define (mock-http-fetch testcase)
230 (lambda (url . rest)
231 (let ((body (assoc-ref testcase url)))
232 (if body
233 (open-input-string body)
234 (error "mocked http-fetch Unexpected URL: " url)))))
235
236 (define (mock-http-get testcase)
237 (lambda (url . rest)
238 (let ((body (assoc-ref testcase url))
239 (response-header
240 (build-response
241 #:version '(1 . 1)
242 #:code 200
243 #:reason-phrase "Ok"
244 #:headers `(
245 (content-type text/html (charset . "utf-8"))
246 (date . ,(make-date 0 10 58 12 6 3 2021 0))
247 (transfer-encoding (chunked)))
248 #:port #f
249 #:validate-headers? #t)))
250 (if body
251 (values response-header body)
252 (error "mocked http-get Unexpected URL: " url)))))
253
254 (test-equal "go-module->guix-package"
255 '(package
256 (name "go-github-com-go-check-check")
257 (version "0.0.0-20201130134442-10cb98267c6c")
258 (source
259 (origin
260 (method git-fetch)
261 (uri (git-reference
262 (url "https://github.com/go-check/check")
263 (commit (go-version->git-ref version))))
264 (file-name (git-file-name name version))
265 (sha256
266 (base32
267 "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5"))))
268 (build-system go-build-system)
269 (arguments
270 '(#:import-path "github.com/go-check/check"))
271 (propagated-inputs
272 `(("go-github-com-kr-pretty" ,go-github-com-kr-pretty)))
273 (home-page "https://github.com/go-check/check")
274 (synopsis "Instructions")
275 (description "Package check is a rich testing extension for Go's testing \
276 package.")
277 (license license:bsd-2))
278
279 ;; Replace network resources with sample data.
280 (call-with-temporary-directory
281 (lambda (checkout)
282 (mock ((web client) http-get
283 (mock-http-get fixtures-go-check-test))
284 (mock ((guix http-client) http-fetch
285 (mock-http-fetch fixtures-go-check-test))
286 (mock ((guix git) update-cached-checkout
287 (lambda* (url #:key ref)
288 ;; Return an empty directory and its hash.
289 (values checkout
290 (nix-base32-string->bytevector
291 "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5")
292 #f)))
293 (go-module->guix-package "github.com/go-check/check")))))))
294
295 (test-end "go")