import: go: Upgrade go.mod parser.
[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 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Summary
21 ;; Tests for guix/import/go.scm
22
23 (define-module (tests-import-go)
24 #:use-module (guix base32)
25 #:use-module (guix build-system go)
26 #:use-module (guix import go)
27 #:use-module (guix base32)
28 #:use-module ((guix utils) #:select (call-with-temporary-directory))
29 #:use-module (guix tests)
30 #:use-module (ice-9 match)
31 #:use-module (srfi srfi-19)
32 #:use-module (srfi srfi-64)
33 #:use-module (web response))
34
35 (define go.mod-requirements
36 (@@ (guix import go) go.mod-requirements))
37
38 (define parse-go.mod
39 (@@ (guix import go) parse-go.mod))
40
41 (define fixture-go-mod-simple
42 "module my/thing
43 go 1.12
44 require other/thing v1.0.2
45 require new/thing/v2 v2.3.4
46 exclude old/thing v1.2.3
47 replace bad/thing v1.4.5 => good/thing v1.4.5
48 ")
49
50 (define fixture-go-mod-with-block
51 "module M
52
53 require (
54 A v1
55 B v1.0.0
56 C v1.0.0
57 D v1.2.3
58 E dev
59 )
60
61 exclude D v1.2.3
62 ")
63
64 (define fixture-go-mod-complete
65 "module M
66
67 go 1.13
68
69 replace github.com/myname/myproject/myapi => ./api
70
71 replace github.com/mymname/myproject/thissdk => ../sdk
72
73 replace launchpad.net/gocheck => github.com/go-check/check v0.0.0-20140225173054-eb6ee6f84d0a
74
75 require (
76 github.com/user/project v1.1.11
77 github.com/user/project/sub/directory v1.1.12
78 bitbucket.org/user/project v1.11.20
79 bitbucket.org/user/project/sub/directory v1.11.21
80 launchpad.net/project v1.1.13
81 launchpad.net/project/series v1.1.14
82 launchpad.net/project/series/sub/directory v1.1.15
83 launchpad.net/~user/project/branch v1.1.16
84 launchpad.net/~user/project/branch/sub/directory v1.1.17
85 hub.jazz.net/git/user/project v1.1.18
86 hub.jazz.net/git/user/project/sub/directory v1.1.19
87 k8s.io/kubernetes/subproject v1.1.101
88 one.example.com/abitrary/repo v1.1.111
89 two.example.com/abitrary/repo v0.0.2
90 \"quoted.example.com/abitrary/repo\" v0.0.2
91 )
92
93 replace two.example.com/abitrary/repo => github.com/corp/arbitrary-repo v0.0.2
94
95 replace (
96 golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13
97 golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13
98 )
99
100 ")
101
102 (define fixture-go-mod-unparseable
103 "module my/thing
104 go 1.12 // avoid feature X
105 require other/thing v1.0.2
106 // Security issue: CVE-XXXXX
107 exclude old/thing v1.2.3
108 new-directive another/thing yet-another/thing
109 replace (
110 bad/thing v1.4.5 => good/thing v1.4.5
111 // Unparseable
112 bad/thing [v1.4.5, v1.9.7] => good/thing v2.0.0
113 )
114 ")
115
116 (define fixture-go-mod-retract
117 "retract v0.9.1
118
119 retract (
120 v1.9.2
121 [v1.0.0, v1.7.9]
122 )
123 ")
124
125 (define fixture-go-mod-strings
126 "require `example.com/\"some-repo\"` v1.9.3
127 require (
128 `example.com/\"another.repo\"` v1.0.0
129 \"example.com/special!repo\" v9.3.1
130 )
131 replace \"example.com/\\\"some-repo\\\"\" => `launchpad.net/some-repo` v1.9.3
132 replace (
133 \"example.com/\\\"another.repo\\\"\" => launchpad.net/another-repo v1.0.0
134 )
135 ")
136
137 (define fixtures-go-check-test
138 (let ((version
139 "{\"Version\":\"v0.0.0-20201130134442-10cb98267c6c\",\"Time\":\"2020-11-30T13:44:42Z\"}")
140 (go.mod
141 "module gopkg.in/check.v1
142
143 go 1.11
144
145 require github.com/kr/pretty v0.2.1
146 ")
147 (go-get
148 "<!DOCTYPE html>
149 <html lang=\"en\" >
150 <head>
151 <meta charset=\"utf-8\">
152 <link rel=\"dns-prefetch\" href=\"https://github.githubassets.com\">
153 <script crossorigin=\"anonymous\" defer=\"defer\" integrity=\"sha512-aw5tciVT0IsECUmMuwp9ez60QReE2/yFNL1diLgZnOom6RhU8+0lG3RlAKto4JwbCoEP15E41Pksd7rK5BKfCQ==\" type=\"application/javascript\" src=\"https://github.githubassets.com/assets/topic-suggestions-6b0e6d72.js\"></script>
154 <meta name=\"viewport\" content=\"width=device-width\">
155
156 <title>GitHub - go-check/check: Rich testing for the Go language</title>
157 <meta name=\"description\" content=\"Rich testing for the Go language. Contribute to go-check/check development by creating an account on GitHub.\">
158 <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"/opensearch.xml\" title=\"GitHub\">
159 <link rel=\"fluid-icon\" href=\"https://github.com/fluidicon.png\" title=\"GitHub\">
160 <!-- To prevent page flashing, the optimizely JS needs to be loaded in the
161 <head> tag before the DOM renders -->
162 <meta name=\"hostname\" content=\"github.com\">
163 <meta name=\"user-login\" content=\"\">
164 <link href=\"https://github.com/go-check/check/commits/v1.atom\" rel=\"alternate\" title=\"Recent Commits to check:v1\" type=\"application/atom+xml\">
165 <meta name=\"go-import\" content=\"github.com/go-check/check git https://github.com/go-check/check.git\">
166 </head>
167 <body class=\"logged-out env-production page-responsive\" style=\"word-wrap: break-word;\">
168 </body>
169 </html>
170 ")
171 (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")
172 (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"))
173 `(("https://proxy.golang.org/github.com/go-check/check/@v/v0.0.0-20201130134442-10cb98267c6c.mod"
174 . ,go.mod)
175 ("https://proxy.golang.org/github.com/go-check/check/@latest"
176 . ,version)
177 ("https://github.com/go-check/check?go-get=1"
178 . ,go-get)
179 ("https://pkg.go.dev/github.com/go-check/check"
180 . ,pkg.go.dev)
181 ("https://pkg.go.dev/github.com/go-check/check?tab=licenses"
182 . ,pkg.go.dev-licence)
183 ("https://proxy.golang.org/github.com/go-check/check/@v/list" . ""))))
184
185 (test-begin "go")
186
187 ;;; Unit tests for go build-system
188
189 (test-equal "go-version basic"
190 "v1.0.2"
191 (go-version->git-ref "v1.0.2"))
192
193 (test-equal "go-version with embedded git-ref"
194 "65e3620a7ae7"
195 (go-version->git-ref "v0.0.0-20190821162956-65e3620a7ae7"))
196
197 (test-equal "go-version with complex embedded git-ref"
198 "daa7c04131f5"
199 (go-version->git-ref "v1.2.4-0.20191109021931-daa7c04131f5"))
200
201 (test-assert "go-pseudo-version? multi-digit version number"
202 (go-pseudo-version? "v1.23.1-0.20200526195155-81db48ad09cc"))
203
204 (test-assert "go-pseudo-version? semantic version with rc"
205 (go-pseudo-version? "v1.4.0-rc.4.0.20200313231945-b860323f09d0"))
206
207 ;;; Unit tests for (guix import go)
208
209 (test-equal "go-path-escape"
210 "github.com/!azure/!avere"
211 ((@@ (guix import go) go-path-escape) "github.com/Azure/Avere"))
212
213
214 ;; We define a function for all similar tests with different go.mod files
215 (define (testing-parse-mod name expected input)
216 (define (inf? p1 p2)
217 (string<? (car p1) (car p2)))
218 (test-equal name
219 (sort expected inf?)
220 (sort (go.mod-requirements (parse-go.mod input)) inf?)))
221
222 (testing-parse-mod "parse-go.mod-simple"
223 '(("good/thing" "v1.4.5")
224 ("new/thing/v2" "v2.3.4")
225 ("other/thing" "v1.0.2"))
226 fixture-go-mod-simple)
227
228 (testing-parse-mod "parse-go.mod-with-block"
229 '(("A" "v1")
230 ("B" "v1.0.0")
231 ("C" "v1.0.0")
232 ("D" "v1.2.3")
233 ("E" "dev"))
234 fixture-go-mod-with-block)
235
236 (testing-parse-mod
237 "parse-go.mod-complete"
238 '(("github.com/corp/arbitrary-repo" "v0.0.2")
239 ("quoted.example.com/abitrary/repo" "v0.0.2")
240 ("one.example.com/abitrary/repo" "v1.1.111")
241 ("hub.jazz.net/git/user/project/sub/directory" "v1.1.19")
242 ("hub.jazz.net/git/user/project" "v1.1.18")
243 ("launchpad.net/~user/project/branch/sub/directory" "v1.1.17")
244 ("launchpad.net/~user/project/branch" "v1.1.16")
245 ("launchpad.net/project/series/sub/directory" "v1.1.15")
246 ("launchpad.net/project/series" "v1.1.14")
247 ("launchpad.net/project" "v1.1.13")
248 ("bitbucket.org/user/project/sub/directory" "v1.11.21")
249 ("bitbucket.org/user/project" "v1.11.20")
250 ("k8s.io/kubernetes/subproject" "v1.1.101")
251 ("github.com/user/project/sub/directory" "v1.1.12")
252 ("github.com/user/project" "v1.1.11")
253 ("github.com/go-check/check" "v0.0.0-20140225173054-eb6ee6f84d0a"))
254 fixture-go-mod-complete)
255
256 (test-equal "parse-go.mod: simple"
257 `((module (module-path "my/thing"))
258 (go (version "1.12"))
259 (require (module-path "other/thing") (version "v1.0.2"))
260 (require (module-path "new/thing/v2") (version "v2.3.4"))
261 (exclude (module-path "old/thing") (version "v1.2.3"))
262 (replace (original (module-path "bad/thing") (version "v1.4.5"))
263 (with (module-path "good/thing") (version "v1.4.5"))))
264 (parse-go.mod fixture-go-mod-simple))
265
266 (test-equal "parse-go.mod: comments and unparseable lines"
267 `((module (module-path "my/thing"))
268 (go (version "1.12") (comment "avoid feature X"))
269 (require (module-path "other/thing") (version "v1.0.2"))
270 (comment "Security issue: CVE-XXXXX")
271 (exclude (module-path "old/thing") (version "v1.2.3"))
272 (unknown "new-directive another/thing yet-another/thing")
273 (replace (original (module-path "bad/thing") (version "v1.4.5"))
274 (with (module-path "good/thing") (version "v1.4.5")))
275 (comment "Unparseable")
276 (unknown "bad/thing [v1.4.5, v1.9.7] => good/thing v2.0.0"))
277 (parse-go.mod fixture-go-mod-unparseable))
278
279 (test-equal "parse-go.mod: retract"
280 `((retract (version "v0.9.1"))
281 (retract (version "v1.9.2"))
282 (retract (range (version "v1.0.0") (version "v1.7.9"))))
283 (parse-go.mod fixture-go-mod-retract))
284
285 (test-equal "parse-go.mod: raw strings and quoted strings"
286 `((require (module-path "example.com/\"some-repo\"") (version "v1.9.3"))
287 (require (module-path "example.com/\"another.repo\"") (version "v1.0.0"))
288 (require (module-path "example.com/special!repo") (version "v9.3.1"))
289 (replace (original (module-path "example.com/\"some-repo\""))
290 (with (module-path "launchpad.net/some-repo") (version "v1.9.3")))
291 (replace (original (module-path "example.com/\"another.repo\""))
292 (with (module-path "launchpad.net/another-repo") (version "v1.0.0"))))
293 (parse-go.mod fixture-go-mod-strings))
294
295 (test-equal "parse-go.mod: complete"
296 `((module (module-path "M"))
297 (go (version "1.13"))
298 (replace (original (module-path "github.com/myname/myproject/myapi"))
299 (with (file-path "./api")))
300 (replace (original (module-path "github.com/mymname/myproject/thissdk"))
301 (with (file-path "../sdk")))
302 (replace (original (module-path "launchpad.net/gocheck"))
303 (with (module-path "github.com/go-check/check")
304 (version "v0.0.0-20140225173054-eb6ee6f84d0a")))
305 (require (module-path "github.com/user/project")
306 (version "v1.1.11"))
307 (require (module-path "github.com/user/project/sub/directory")
308 (version "v1.1.12"))
309 (require (module-path "bitbucket.org/user/project")
310 (version "v1.11.20"))
311 (require (module-path "bitbucket.org/user/project/sub/directory")
312 (version "v1.11.21"))
313 (require (module-path "launchpad.net/project")
314 (version "v1.1.13"))
315 (require (module-path "launchpad.net/project/series")
316 (version "v1.1.14"))
317 (require (module-path "launchpad.net/project/series/sub/directory")
318 (version "v1.1.15"))
319 (require (module-path "launchpad.net/~user/project/branch")
320 (version "v1.1.16"))
321 (require (module-path "launchpad.net/~user/project/branch/sub/directory")
322 (version "v1.1.17"))
323 (require (module-path "hub.jazz.net/git/user/project")
324 (version "v1.1.18"))
325 (require (module-path "hub.jazz.net/git/user/project/sub/directory")
326 (version "v1.1.19"))
327 (require (module-path "k8s.io/kubernetes/subproject")
328 (version "v1.1.101"))
329 (require (module-path "one.example.com/abitrary/repo")
330 (version "v1.1.111"))
331 (require (module-path "two.example.com/abitrary/repo")
332 (version "v0.0.2"))
333 (require (module-path "quoted.example.com/abitrary/repo")
334 (version "v0.0.2"))
335 (replace (original (module-path "two.example.com/abitrary/repo"))
336 (with (module-path "github.com/corp/arbitrary-repo")
337 (version "v0.0.2")))
338 (replace (original (module-path "golang.org/x/sys"))
339 (with (module-path "golang.org/x/sys")
340 (version "v0.0.0-20190813064441-fde4db37ae7a"))
341 (comment "pinned to release-branch.go1.13"))
342 (replace (original (module-path "golang.org/x/tools"))
343 (with (module-path "golang.org/x/tools")
344 (version "v0.0.0-20190821162956-65e3620a7ae7"))
345 (comment "pinned to release-branch.go1.13")))
346 (parse-go.mod fixture-go-mod-complete))
347
348 ;;; End-to-end tests for (guix import go)
349 (define (mock-http-fetch testcase)
350 (lambda (url . rest)
351 (let ((body (assoc-ref testcase url)))
352 (if body
353 (open-input-string body)
354 (error "mocked http-fetch Unexpected URL: " url)))))
355
356 (define (mock-http-get testcase)
357 (lambda (url . rest)
358 (let ((body (assoc-ref testcase url))
359 (response-header
360 (build-response
361 #:version '(1 . 1)
362 #:code 200
363 #:reason-phrase "Ok"
364 #:headers `(
365 (content-type text/html (charset . "utf-8"))
366 (date . ,(make-date 0 10 58 12 6 3 2021 0))
367 (transfer-encoding (chunked)))
368 #:port #f
369 #:validate-headers? #t)))
370 (if body
371 (values response-header body)
372 (error "mocked http-get Unexpected URL: " url)))))
373
374 (test-equal "go-module->guix-package"
375 '(package
376 (name "go-github-com-go-check-check")
377 (version "0.0.0-20201130134442-10cb98267c6c")
378 (source
379 (origin
380 (method git-fetch)
381 (uri (git-reference
382 (url "https://github.com/go-check/check")
383 (commit (go-version->git-ref version))))
384 (file-name (git-file-name name version))
385 (sha256
386 (base32
387 "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5"))))
388 (build-system go-build-system)
389 (arguments
390 '(#:import-path "github.com/go-check/check"))
391 (propagated-inputs
392 `(("go-github-com-kr-pretty" ,go-github-com-kr-pretty)))
393 (home-page "https://github.com/go-check/check")
394 (synopsis "Instructions")
395 (description "Package check is a rich testing extension for Go's testing \
396 package.")
397 (license license:bsd-2))
398
399 ;; Replace network resources with sample data.
400 (call-with-temporary-directory
401 (lambda (checkout)
402 (mock ((web client) http-get
403 (mock-http-get fixtures-go-check-test))
404 (mock ((guix http-client) http-fetch
405 (mock-http-fetch fixtures-go-check-test))
406 (mock ((guix git) update-cached-checkout
407 (lambda* (url #:key ref)
408 ;; Return an empty directory and its hash.
409 (values checkout
410 (nix-base32-string->bytevector
411 "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5")
412 #f)))
413 (go-module->guix-package "github.com/go-check/check")))))))
414
415 (test-end "go")