tests: Mock up http-fetch.
[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 ("http://example.com/Foo-Bar-0.1.tar.gz"
72 test-source)
73 (_ (error "Unexpected URL: " url))))))))
74 (mock ((guix http-client) http-fetch
75 (lambda (url)
76 (match url
77 ("https://api.metacpan.org/release/Foo-Bar"
78 (values (open-input-string test-json)
79 (string-length test-json)))
80 ("https://api.metacpan.org/module/Test::Script?fields=distribution"
81 (let ((result "{ \"distribution\" : \"Test-Script\" }"))
82 (values (open-input-string result)
83 (string-length result))))
84 (_ (error "Unexpected URL: " url)))))
85 (match (cpan->guix-package "Foo::Bar")
86 (('package
87 ('name "perl-foo-bar")
88 ('version "0.1")
89 ('source ('origin
90 ('method 'url-fetch)
91 ('uri ('string-append "http://example.com/Foo-Bar-"
92 'version ".tar.gz"))
93 ('sha256
94 ('base32
95 (? string? hash)))))
96 ('build-system 'perl-build-system)
97 ('inputs
98 ('quasiquote
99 (("perl-test-script" ('unquote 'perl-test-script)))))
100 ('home-page "http://search.cpan.org/dist/Foo-Bar")
101 ('synopsis "Fizzle Fuzz")
102 ('description 'fill-in-yourself!)
103 ('license (package-license perl)))
104 (string=? (bytevector->nix-base32-string
105 (call-with-input-string test-source port-sha256))
106 hash))
107 (x
108 (pk 'fail x #f))))))
109
110 (test-equal "source-url-http"
111 ((@@ (guix import cpan) cpan-source-url)
112 `(("download_url" .
113 "http://cpan.metacpan.org/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")))
114 "mirror://cpan/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")
115
116 (test-equal "source-url-https"
117 ((@@ (guix import cpan) cpan-source-url)
118 `(("download_url" .
119 "https://cpan.metacpan.org/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")))
120 "mirror://cpan/authors/id/T/TE/TEST/Foo-Bar-0.1.tar.gz")
121
122 (test-end "cpan")