gnu: Add go-github-com-itchyny-gojq.
[jackhill/guix/guix.git] / tests / builders.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2018, 2019, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2021 Lars-Dominik Braun <lars@6xq.net>
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
21 (define-module (tests builders)
22 #:use-module (guix download)
23 #:use-module (guix build-system)
24 #:use-module (guix build-system gnu)
25 #:use-module (guix build gnu-build-system)
26 #:use-module (guix build utils)
27 #:use-module (guix build-system python)
28 #:use-module (guix store)
29 #:use-module (guix monads)
30 #:use-module (guix utils)
31 #:use-module (guix base32)
32 #:use-module (guix derivations)
33 #:use-module (gcrypt hash)
34 #:use-module (guix tests)
35 #:use-module (guix packages)
36 #:use-module (gnu packages bootstrap)
37 #:use-module (ice-9 match)
38 #:use-module (ice-9 textual-ports)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-34)
42 #:use-module (srfi srfi-64))
43
44 ;; Test the higher-level builders.
45
46 (define %store
47 (open-connection-for-tests))
48
49 (define url-fetch*
50 (store-lower url-fetch))
51
52 ;; Globally disable grafts because they can trigger early builds.
53 (%graft? #f)
54
55 \f
56 (test-begin "builders")
57
58 (unless (network-reachable?) (test-skip 1))
59 (test-assert "url-fetch"
60 (let* ((url '("http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz"
61 "ftp://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz"))
62 (hash (nix-base32-string->bytevector
63 "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6"))
64 (drv (url-fetch* %store url 'sha256 hash
65 #:guile %bootstrap-guile))
66 (out-path (derivation->output-path drv)))
67 (and (build-derivations %store (list drv))
68 (file-exists? out-path)
69 (valid-path? %store out-path))))
70
71 (test-assert "url-fetch, file"
72 (let* ((file (search-path %load-path "guix.scm"))
73 (hash (call-with-input-file file port-sha256))
74 (out (url-fetch* %store file 'sha256 hash)))
75 (and (file-exists? out)
76 (valid-path? %store out))))
77
78 (test-assert "url-fetch, file URI"
79 (let* ((file (search-path %load-path "guix.scm"))
80 (hash (call-with-input-file file port-sha256))
81 (out (url-fetch* %store
82 (string-append "file://" (canonicalize-path file))
83 'sha256 hash)))
84 (and (file-exists? out)
85 (valid-path? %store out))))
86
87 (test-assert "gnu-build-system"
88 (build-system? gnu-build-system))
89
90 (define unpack (assoc-ref %standard-phases 'unpack))
91
92 (define compressors '(("gzip" . "gz")
93 ("xz" . "xz")
94 ("bzip2" . "bz2")
95 (#f . #f)))
96
97 (for-each
98 (match-lambda
99 ((comp . ext)
100
101 (unless (network-reachable?) (test-skip 1)) ;for bootstrap binaries
102 (test-equal (string-append "gnu-build-system unpack phase, "
103 "single file (compression: "
104 (if comp comp "None") ")")
105 "expected text"
106 (let*-values
107 (((name) "test")
108 ((compressed-name) (if ext
109 (string-append name "." ext)
110 name))
111 ((file hash) (test-file %store compressed-name "expected text")))
112 (call-with-temporary-directory
113 (lambda (dir)
114 (with-directory-excursion dir
115 (unpack #:source file)
116 (call-with-input-file name get-string-all))))))))
117 compressors)
118
119 \f
120 ;;;
121 ;;; Test the sanity-check phase of the Python build system.
122 ;;;
123
124 (define* (make-python-dummy name #:key (setup-py-extra "")
125 (init-py "") (use-setuptools? #t))
126 (dummy-package (string-append "python-dummy-" name)
127 (version "0.1")
128 (build-system python-build-system)
129 (arguments
130 `(#:tests? #f
131 #:use-setuptools? ,use-setuptools?
132 #:phases
133 (modify-phases %standard-phases
134 (replace 'unpack
135 (lambda _
136 (mkdir-p "dummy")
137 (with-output-to-file "dummy/__init__.py"
138 (lambda _
139 (display ,init-py)))
140 (with-output-to-file "setup.py"
141 (lambda _
142 (format #t "\
143 ~a
144 setup(
145 name='dummy-~a',
146 version='0.1',
147 packages=['dummy'],
148 ~a
149 )"
150 (if ,use-setuptools?
151 "from setuptools import setup"
152 "from distutils.core import setup")
153 ,name ,setup-py-extra))))))))))
154
155 (define python-dummy-ok
156 (make-python-dummy "ok"))
157
158 ;; distutil won't install any metadata, so make sure our script does not fail
159 ;; on a otherwise fine package.
160 (define python-dummy-no-setuptools
161 (make-python-dummy
162 "no-setuptools" #:use-setuptools? #f))
163
164 (define python-dummy-fail-requirements
165 (make-python-dummy "fail-requirements"
166 #:setup-py-extra "install_requires=['nonexistent'],"))
167
168 (define python-dummy-fail-import
169 (make-python-dummy "fail-import" #:init-py "import nonexistent"))
170
171 (define python-dummy-fail-console-script
172 (make-python-dummy "fail-console-script"
173 #:setup-py-extra (string-append "entry_points={'console_scripts': "
174 "['broken = dummy:nonexistent']},")))
175
176 (define (check-build-success store p)
177 (unless store (test-skip 1))
178 (test-assert (string-append "python-build-system: " (package-name p))
179 (let* ((drv (package-derivation store p)))
180 (build-derivations store (list drv)))))
181
182 (define (check-build-failure store p)
183 (unless store (test-skip 1))
184 (test-assert (string-append "python-build-system: " (package-name p))
185 (let ((drv (package-derivation store p)))
186 (guard (c ((store-protocol-error? c)
187 (pk 'failure c #t))) ;good!
188 (build-derivations store (list drv))
189 #f)))) ;bad: it should have failed
190
191 (with-external-store store
192 (for-each (lambda (p) (check-build-success store p))
193 (list
194 python-dummy-ok
195 python-dummy-no-setuptools))
196 (for-each (lambda (p) (check-build-failure store p))
197 (list
198 python-dummy-fail-requirements
199 python-dummy-fail-import
200 python-dummy-fail-console-script)))
201
202 (test-end "builders")