tests: Adjust 'url-fetch' mocks to TLS changes.
[jackhill/guix/guix.git] / tests / cpan.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
3 ;;; Copyright © 2016 Alex Sassmannshausen <alex@pompo.co>
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 (define-module (test-cpan)
21 #:use-module (guix import cpan)
22 #:use-module (guix base32)
23 #:use-module (guix hash)
24 #:use-module (guix tests)
25 #:use-module (guix grafts)
26 #:use-module (srfi srfi-64)
27 #:use-module (ice-9 match))
28
29 ;; Globally disable grafts because they can trigger early builds.
30 (%graft? #f)
31
32 (define test-json
33 "{
34 \"metadata\" : {
35 \"prereqs\" : {
36 \"runtime\" : {
37 \"requires\" : {
38 \"Test::Script\" : \"1.05\",
39 }
40 }
41 }
42 \"name\" : \"Foo-Bar\",
43 \"version\" : \"0.1\"
44 }
45 \"name\" : \"Foo-Bar-0.1\",
46 \"distribution\" : \"Foo-Bar\",
47 \"license\" : [
48 \"perl_5\"
49 ],
50 \"abstract\" : \"Fizzle Fuzz\",
51 \"download_url\" : \"http://example.com/Foo-Bar-0.1.tar.gz\",
52 \"author\" : \"Guix\",
53 \"version\" : \"0.1\"
54 }")
55
56 (define test-source
57 "foobar")
58
59 (test-begin "cpan")
60
61 (test-assert "cpan->guix-package"
62 ;; Replace network resources with sample data.
63 (mock ((guix build download) url-fetch
64 (lambda* (url file-name
65 #:key
66 (mirrors '()) verify-certificate?)
67 (with-output-to-file file-name
68 (lambda ()
69 (display
70 (match url
71 ("https://api.metacpan.org/release/Foo-Bar"
72 test-json)
73 ("https://api.metacpan.org/module/Test::Script"
74 "{ \"distribution\" : \"Test-Script\" }")
75 ("http://example.com/Foo-Bar-0.1.tar.gz"
76 test-source)
77 (_ (error "Unexpected URL: " url))))))))
78 (match (cpan->guix-package "Foo::Bar")
79 (('package
80 ('name "perl-foo-bar")
81 ('version "0.1")
82 ('source ('origin
83 ('method 'url-fetch)
84 ('uri ('string-append "http://example.com/Foo-Bar-"
85 'version ".tar.gz"))
86 ('sha256
87 ('base32
88 (? string? hash)))))
89 ('build-system 'perl-build-system)
90 ('inputs
91 ('quasiquote
92 (("perl-test-script" ('unquote 'perl-test-script)))))
93 ('home-page "http://search.cpan.org/dist/Foo-Bar")
94 ('synopsis "Fizzle Fuzz")
95 ('description 'fill-in-yourself!)
96 ('license (package-license perl)))
97 (string=? (bytevector->nix-base32-string
98 (call-with-input-string test-source port-sha256))
99 hash))
100 (x
101 (pk 'fail x #f)))))
102
103 (test-equal "source-url-http"
104 ((@@ (guix import cpan) fix-source-url)
105 "http://cpan.metacpan.org/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")
106 "mirror://cpan/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")
107
108 (test-equal "source-url-https"
109 ((@@ (guix import cpan) fix-source-url)
110 "https://cpan.metacpan.org/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")
111 "mirror://cpan/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")
112
113 (test-end "cpan")