gnu: Add emacs-exec-path-from-shell.
[jackhill/guix/guix.git] / tests / utils.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
4c0c4db0 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
516e3b6f 3;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
1b846da8 4;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
e3deeebb 5;;;
233e7676 6;;; This file is part of GNU Guix.
e3deeebb 7;;;
233e7676 8;;; GNU Guix is free software; you can redistribute it and/or modify it
e3deeebb
LC
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
233e7676 13;;; GNU Guix is distributed in the hope that it will be useful, but
e3deeebb
LC
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
233e7676 19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
e3deeebb 20
e3deeebb 21(define-module (test-utils)
e0fbbc88 22 #:use-module ((guix config) #:select (%gzip))
e3deeebb 23 #:use-module (guix utils)
b980f0f9 24 #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
d838e702 25 #:use-module ((guix search-paths) #:select (string-tokenize*))
e3deeebb 26 #:use-module (srfi srfi-1)
98090557 27 #:use-module (srfi srfi-11)
e3deeebb 28 #:use-module (srfi srfi-64)
f9c7080a 29 #:use-module (rnrs bytevectors)
e0fbbc88 30 #:use-module (rnrs io ports)
516e3b6f
EB
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 vlist))
e3deeebb 33
827d5563
LC
34(define temp-file
35 (string-append "t-utils-" (number->string (getpid))))
36
e3deeebb
LC
37(test-begin "utils")
38
98090557
LC
39(test-assert "gnu-triplet->nix-system"
40 (let ((samples '(("i586-gnu0.3" "i686-gnu")
41 ("x86_64-unknown-linux-gnu" "x86_64-linux")
42 ("i386-pc-linux-gnu" "i686-linux")
43 ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
44 ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
45 ("i686-pc-cygwin" "i686-cygwin"))))
46 (let-values (((gnu nix) (unzip2 samples)))
47 (every (lambda (gnu nix)
48 (equal? nix (gnu-triplet->nix-system gnu)))
49 gnu nix))))
50
9b48fb88
LC
51(test-assert "package-name->name+version"
52 (every (match-lambda
53 ((name version)
54 (let*-values (((full-name)
55 (if version
1b846da8 56 (string-append name "@" version)
9b48fb88
LC
57 name))
58 ((name* version*)
59 (package-name->name+version full-name)))
60 (and (equal? name* name)
61 (equal? version* version)))))
62 '(("foo" "0.9.1b")
1b846da8 63 ("foo-14-bar" "320")
9b48fb88
LC
64 ("foo-bar2" #f)
65 ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
66 ("nixpkgs" "1.0pre22125_a28fe19")
67 ("gtk2" "2.38.0"))))
68
7db3ff4a
LC
69(test-assert "guile-version>? 1.8"
70 (guile-version>? "1.8"))
71
72(test-assert "guile-version>? 10.5"
73 (not (guile-version>? "10.5")))
74
2bcfb9e0
LC
75(test-equal "string-tokenize*"
76 '(("foo")
77 ("foo" "bar" "baz")
78 ("foo" "bar" "")
79 ("foo" "bar" "baz"))
d838e702
LC
80 (list (string-tokenize* "foo" ":")
81 (string-tokenize* "foo;bar;baz" ";")
82 (string-tokenize* "foo!bar!" "!")
83 (string-tokenize* "foo+-+bar+-+baz" "+-+")))
2bcfb9e0 84
56b943de
LC
85(test-equal "string-replace-substring"
86 '("foo BAR! baz"
87 "/gnu/store/chbouib"
88 "")
89 (list (string-replace-substring "foo bar baz" "bar" "BAR!")
90 (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/")
91 (string-replace-substring "" "foo" "bar")))
92
5e110382
LC
93(test-equal "strip-keyword-arguments"
94 '(a #:b b #:c c)
95 (strip-keyword-arguments '(#:foo #:bar #:baz)
96 '(a #:foo 42 #:b b #:baz 3
97 #:c c #:bar 4)))
98
6071122b
LC
99(test-equal "ensure-keyword-arguments"
100 '((#:foo 2)
101 (#:foo 2 #:bar 3)
102 (#:foo 42 #:bar 3))
103 (list (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
104 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
105 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))))
106
347df601
EB
107(test-equal "default-keyword-arguments"
108 '((#:foo 2)
109 (#:foo 2)
110 (#:foo 2 #:bar 3)
111 (#:foo 2 #:bar 3)
112 (#:foo 2 #:bar 3))
113 (list (default-keyword-arguments '() '(#:foo 2))
114 (default-keyword-arguments '(#:foo 2) '(#:foo 4))
115 (default-keyword-arguments '() '(#:bar 3 #:foo 2))
116 (default-keyword-arguments '(#:bar 3) '(#:foo 2))
117 (default-keyword-arguments '(#:foo 2 #:bar 3) '(#:bar 6))))
118
b8b129eb
EB
119(test-equal "substitute-keyword-arguments"
120 '((#:foo 3)
121 (#:foo 3)
122 (#:foo 3 #:bar (1 2))
123 (#:bar (1 2) #:foo 3)
124 (#:foo 3))
125 (list (substitute-keyword-arguments '(#:foo 2)
126 ((#:foo f) (1+ f)))
127 (substitute-keyword-arguments '()
128 ((#:foo f 2) (1+ f)))
129 (substitute-keyword-arguments '(#:foo 2 #:bar (2))
130 ((#:foo f) (1+ f))
131 ((#:bar b) (cons 1 b)))
132 (substitute-keyword-arguments '(#:foo 2)
133 ((#:foo _) 3)
134 ((#:bar b '(2)) (cons 1 b)))
135 (substitute-keyword-arguments '(#:foo 2)
136 ((#:foo f 1) (1+ f))
137 ((#:bar b) (cons 42 b)))))
138
e0fbbc88 139(test-assert "filtered-port, file"
101d9f3f 140 (let* ((file (search-path %load-path "guix.scm"))
b6952cad 141 (input (open-file file "r0b")))
101d9f3f
LC
142 (let*-values (((compressed pids1)
143 (filtered-port `(,%gzip "-c" "--fast") input))
144 ((decompressed pids2)
145 (filtered-port `(,%gzip "-d") compressed)))
146 (and (every (compose zero? cdr waitpid)
147 (append pids1 pids2))
148 (equal? (get-bytevector-all decompressed)
149 (call-with-input-file file get-bytevector-all))))))
e0fbbc88
LC
150
151(test-assert "filtered-port, non-file"
152 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
153 get-bytevector-all)))
154 (let*-values (((compressed pids1)
155 (filtered-port `(,%gzip "-c" "--fast")
156 (open-bytevector-input-port data)))
157 ((decompressed pids2)
158 (filtered-port `(,%gzip "-d") compressed)))
159 (and (pk (every (compose zero? cdr waitpid)
160 (append pids1 pids2)))
161 (equal? (get-bytevector-all decompressed) data)))))
162
443eb4e9
LC
163(test-assert "filtered-port, does not exist"
164 (let* ((file (search-path %load-path "guix.scm"))
165 (input (open-file file "r0b")))
166 (let-values (((port pids)
167 (filtered-port '("/does/not/exist") input)))
168 (any (compose (negate zero?) cdr waitpid)
169 pids))))
170
7a8024a3
LC
171(test-assert "compressed-port, decompressed-port, non-file"
172 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
173 get-bytevector-all)))
174 (let*-values (((compressed pids1)
175 (compressed-port 'xz (open-bytevector-input-port data)))
176 ((decompressed pids2)
177 (decompressed-port 'xz compressed)))
178 (and (every (compose zero? cdr waitpid)
179 (append pids1 pids2))
180 (equal? (get-bytevector-all decompressed) data)))))
181
80dea563 182(false-if-exception (delete-file temp-file))
01ac19dc
LC
183(test-assert "compressed-output-port + decompressed-port"
184 (let* ((file (search-path %load-path "guix/derivations.scm"))
30ce8012
LC
185 (data (call-with-input-file file get-bytevector-all))
186 (port (open-file temp-file "w0b")))
187 (call-with-compressed-output-port 'xz port
01ac19dc
LC
188 (lambda (compressed)
189 (put-bytevector compressed data)))
30ce8012 190 (close-port port)
01ac19dc
LC
191
192 (bytevector=? data
193 (call-with-decompressed-port 'xz (open-file temp-file "r0b")
194 get-bytevector-all))))
80dea563 195
e3d74106
LC
196;; This is actually in (guix store).
197(test-equal "store-path-package-name"
198 "bash-4.2-p24"
199 (store-path-package-name
b980f0f9
LC
200 (string-append (%store-prefix)
201 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
e3d74106 202
c8be6f0d
FB
203(test-equal "canonical-newline-port"
204 "This is a journey\nInto the sound\nA journey ...\n"
205 (let ((port (open-string-input-port
206 "This is a journey\r\nInto the sound\r\nA journey ...\n")))
207 (get-string-all (canonical-newline-port port))))
208
50a3d594
SB
209
210(test-equal "edit-expression"
211 "(display \"GNU Guix\")\n(newline)\n"
212 (begin
213 (call-with-output-file temp-file
214 (lambda (port)
215 (display "(display \"xiuG UNG\")\n(newline)\n" port)))
216 (edit-expression `((filename . ,temp-file)
217 (line . 0)
218 (column . 9))
219 string-reverse)
220 (call-with-input-file temp-file get-string-all)))
221
e3deeebb
LC
222(test-end)
223
827d5563 224(false-if-exception (delete-file temp-file))