tests: Factorize the 'dummy-package' macro.
[jackhill/guix/guix.git] / guix / tests.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
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 (define-module (guix tests)
20 #:use-module (guix store)
21 #:use-module (guix derivations)
22 #:use-module (guix packages)
23 #:use-module (gnu packages bootstrap)
24 #:use-module (srfi srfi-34)
25 #:use-module (rnrs bytevectors)
26 #:use-module (web uri)
27 #:export (open-connection-for-tests
28 random-text
29 random-bytevector
30 with-derivation-narinfo
31 dummy-package))
32
33 ;;; Commentary:
34 ;;;
35 ;;; This module provide shared infrastructure for the test suite. For
36 ;;; internal use only.
37 ;;;
38 ;;; Code:
39
40 (define (open-connection-for-tests)
41 "Open a connection to the build daemon for tests purposes and return it."
42 (guard (c ((nix-error? c)
43 (format (current-error-port)
44 "warning: build daemon error: ~s~%" c)
45 #f))
46 (let ((store (open-connection)))
47 ;; Make sure we build everything by ourselves.
48 (set-build-options store #:use-substitutes? #f)
49
50 ;; Use the bootstrap Guile when running tests, so we don't end up
51 ;; building everything in the temporary test store.
52 (%guile-for-build (package-derivation store %bootstrap-guile))
53
54 store)))
55
56 (define %seed
57 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
58
59 (define (random-text)
60 "Return the hexadecimal representation of a random number."
61 (number->string (random (expt 2 256) %seed) 16))
62
63 (define (random-bytevector n)
64 "Return a random bytevector of N bytes."
65 (let ((bv (make-bytevector n)))
66 (let loop ((i 0))
67 (if (< i n)
68 (begin
69 (bytevector-u8-set! bv i (random 256 %seed))
70 (loop (1+ i)))
71 bv))))
72
73 \f
74 ;;;
75 ;;; Narinfo files, as used by the substituter.
76 ;;;
77
78 (define* (derivation-narinfo drv #:optional (nar "example.nar"))
79 "Return the contents of the narinfo corresponding to DRV; NAR should be the
80 file name of the archive containing the substitute for DRV."
81 (format #f "StorePath: ~a
82 URL: ~a
83 Compression: none
84 NarSize: 1234
85 References:
86 System: ~a
87 Deriver: ~a~%"
88 (derivation->output-path drv) ; StorePath
89 nar ; URL
90 (derivation-system drv) ; System
91 (basename
92 (derivation-file-name drv)))) ; Deriver
93
94 (define (call-with-derivation-narinfo drv thunk)
95 "Call THUNK in a context where fake substituter data, as read by 'guix
96 substitute-binary', has been installed for DRV."
97 (let* ((output (derivation->output-path drv))
98 (dir (uri-path
99 (string->uri (getenv "GUIX_BINARY_SUBSTITUTE_URL"))))
100 (info (string-append dir "/nix-cache-info"))
101 (narinfo (string-append dir "/" (store-path-hash-part output)
102 ".narinfo")))
103 (dynamic-wind
104 (lambda ()
105 (call-with-output-file info
106 (lambda (p)
107 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
108 (%store-prefix))))
109 (call-with-output-file narinfo
110 (lambda (p)
111 (display (derivation-narinfo drv) p))))
112 thunk
113 (lambda ()
114 (delete-file narinfo)
115 (delete-file info)))))
116
117 (define-syntax-rule (with-derivation-narinfo drv body ...)
118 "Evaluate BODY in a context where DRV looks substitutable from the
119 substituter's viewpoint."
120 (call-with-derivation-narinfo drv
121 (lambda ()
122 body ...)))
123
124 (define-syntax-rule (dummy-package name* extra-fields ...)
125 "Return a \"dummy\" package called NAME*, with all its compulsory fields
126 initialized with default values, and with EXTRA-FIELDS set as specified."
127 (package extra-fields ...
128 (name name*) (version "0") (source #f)
129 (build-system gnu-build-system)
130 (synopsis #f) (description #f)
131 (home-page #f) (license #f)))
132
133 ;; Local Variables:
134 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
135 ;; End:
136
137 ;;; tests.scm ends here