| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org> |
| 4 | ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org> |
| 5 | ;;; |
| 6 | ;;; This file is part of GNU Guix. |
| 7 | ;;; |
| 8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 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 | ;;; |
| 13 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 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 |
| 19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | (define-module (test-utils) |
| 22 | #:use-module ((guix config) #:select (%gzip)) |
| 23 | #:use-module (guix utils) |
| 24 | #:use-module ((guix store) #:select (%store-prefix store-path-package-name)) |
| 25 | #:use-module ((guix search-paths) #:select (string-tokenize*)) |
| 26 | #:use-module (srfi srfi-1) |
| 27 | #:use-module (srfi srfi-11) |
| 28 | #:use-module (srfi srfi-64) |
| 29 | #:use-module (rnrs bytevectors) |
| 30 | #:use-module (rnrs io ports) |
| 31 | #:use-module (ice-9 match) |
| 32 | #:use-module (ice-9 vlist)) |
| 33 | |
| 34 | (define temp-file |
| 35 | (string-append "t-utils-" (number->string (getpid)))) |
| 36 | |
| 37 | (test-begin "utils") |
| 38 | |
| 39 | (test-assert "bytevector->base16-string->bytevector" |
| 40 | (every (lambda (bv) |
| 41 | (equal? (base16-string->bytevector |
| 42 | (bytevector->base16-string bv)) |
| 43 | bv)) |
| 44 | (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar")))) |
| 45 | |
| 46 | (test-assert "gnu-triplet->nix-system" |
| 47 | (let ((samples '(("i586-gnu0.3" "i686-gnu") |
| 48 | ("x86_64-unknown-linux-gnu" "x86_64-linux") |
| 49 | ("i386-pc-linux-gnu" "i686-linux") |
| 50 | ("x86_64-unknown-freebsd8.2" "x86_64-freebsd") |
| 51 | ("x86_64-apple-darwin10.8.0" "x86_64-darwin") |
| 52 | ("i686-pc-cygwin" "i686-cygwin")))) |
| 53 | (let-values (((gnu nix) (unzip2 samples))) |
| 54 | (every (lambda (gnu nix) |
| 55 | (equal? nix (gnu-triplet->nix-system gnu))) |
| 56 | gnu nix)))) |
| 57 | |
| 58 | (test-assert "package-name->name+version" |
| 59 | (every (match-lambda |
| 60 | ((name version) |
| 61 | (let*-values (((full-name) |
| 62 | (if version |
| 63 | (string-append name "@" version) |
| 64 | name)) |
| 65 | ((name* version*) |
| 66 | (package-name->name+version full-name))) |
| 67 | (and (equal? name* name) |
| 68 | (equal? version* version))))) |
| 69 | '(("foo" "0.9.1b") |
| 70 | ("foo-14-bar" "320") |
| 71 | ("foo-bar2" #f) |
| 72 | ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen' |
| 73 | ("nixpkgs" "1.0pre22125_a28fe19") |
| 74 | ("gtk2" "2.38.0")))) |
| 75 | |
| 76 | (test-assert "guile-version>? 1.8" |
| 77 | (guile-version>? "1.8")) |
| 78 | |
| 79 | (test-assert "guile-version>? 10.5" |
| 80 | (not (guile-version>? "10.5"))) |
| 81 | |
| 82 | (test-equal "string-tokenize*" |
| 83 | '(("foo") |
| 84 | ("foo" "bar" "baz") |
| 85 | ("foo" "bar" "") |
| 86 | ("foo" "bar" "baz")) |
| 87 | (list (string-tokenize* "foo" ":") |
| 88 | (string-tokenize* "foo;bar;baz" ";") |
| 89 | (string-tokenize* "foo!bar!" "!") |
| 90 | (string-tokenize* "foo+-+bar+-+baz" "+-+"))) |
| 91 | |
| 92 | (test-equal "string-replace-substring" |
| 93 | '("foo BAR! baz" |
| 94 | "/gnu/store/chbouib" |
| 95 | "") |
| 96 | (list (string-replace-substring "foo bar baz" "bar" "BAR!") |
| 97 | (string-replace-substring "/nix/store/chbouib" "/nix/" "/gnu/") |
| 98 | (string-replace-substring "" "foo" "bar"))) |
| 99 | |
| 100 | (test-equal "fold2, 1 list" |
| 101 | (list (reverse (iota 5)) |
| 102 | (map - (reverse (iota 5)))) |
| 103 | (call-with-values |
| 104 | (lambda () |
| 105 | (fold2 (lambda (i r1 r2) |
| 106 | (values (cons i r1) |
| 107 | (cons (- i) r2))) |
| 108 | '() '() |
| 109 | (iota 5))) |
| 110 | list)) |
| 111 | |
| 112 | (test-equal "fold2, 2 lists" |
| 113 | (list (reverse '((a . 0) (b . 1) (c . 2) (d . 3))) |
| 114 | (reverse '((a . 0) (b . -1) (c . -2) (d . -3)))) |
| 115 | (call-with-values |
| 116 | (lambda () |
| 117 | (fold2 (lambda (k v r1 r2) |
| 118 | (values (alist-cons k v r1) |
| 119 | (alist-cons k (- v) r2))) |
| 120 | '() '() |
| 121 | '(a b c d) |
| 122 | '(0 1 2 3))) |
| 123 | list)) |
| 124 | |
| 125 | (test-equal "split, element is in list" |
| 126 | '((foo) (baz)) |
| 127 | (call-with-values |
| 128 | (lambda () |
| 129 | (split '(foo bar baz) 'bar)) |
| 130 | list)) |
| 131 | |
| 132 | (test-equal "split, element is not in list" |
| 133 | '((foo bar baz) ()) |
| 134 | (call-with-values |
| 135 | (lambda () |
| 136 | (split '(foo bar baz) 'quux)) |
| 137 | list)) |
| 138 | |
| 139 | (test-equal "strip-keyword-arguments" |
| 140 | '(a #:b b #:c c) |
| 141 | (strip-keyword-arguments '(#:foo #:bar #:baz) |
| 142 | '(a #:foo 42 #:b b #:baz 3 |
| 143 | #:c c #:bar 4))) |
| 144 | |
| 145 | (test-equal "ensure-keyword-arguments" |
| 146 | '((#:foo 2) |
| 147 | (#:foo 2 #:bar 3) |
| 148 | (#:foo 42 #:bar 3)) |
| 149 | (list (ensure-keyword-arguments '(#:foo 2) '(#:foo 2)) |
| 150 | (ensure-keyword-arguments '(#:foo 2) '(#:bar 3)) |
| 151 | (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42)))) |
| 152 | |
| 153 | (let* ((tree (alist->vhash |
| 154 | '((0 2 3) (1 3 4) (2) (3 5 6) (4 6) (5) (6)) |
| 155 | hashq)) |
| 156 | (add-one (lambda (_ r) (1+ r))) |
| 157 | (tree-lookup (lambda (n) (cdr (vhash-assq n tree))))) |
| 158 | (test-equal "fold-tree, single root" |
| 159 | 5 (fold-tree add-one 0 tree-lookup '(0))) |
| 160 | (test-equal "fold-tree, two roots" |
| 161 | 7 (fold-tree add-one 0 tree-lookup '(0 1))) |
| 162 | (test-equal "fold-tree, sum" |
| 163 | 16 (fold-tree + 0 tree-lookup '(0))) |
| 164 | (test-equal "fold-tree, internal" |
| 165 | 18 (fold-tree + 0 tree-lookup '(3 4))) |
| 166 | (test-equal "fold-tree, cons" |
| 167 | '(1 3 4 5 6) |
| 168 | (sort (fold-tree cons '() tree-lookup '(1)) <)) |
| 169 | (test-equal "fold-tree, overlapping paths" |
| 170 | '(1 3 4 5 6) |
| 171 | (sort (fold-tree cons '() tree-lookup '(1 4)) <)) |
| 172 | (test-equal "fold-tree, cons, two roots" |
| 173 | '(0 2 3 4 5 6) |
| 174 | (sort (fold-tree cons '() tree-lookup '(0 4)) <)) |
| 175 | (test-equal "fold-tree-leaves, single root" |
| 176 | 2 (fold-tree-leaves add-one 0 tree-lookup '(1))) |
| 177 | (test-equal "fold-tree-leaves, single root, sum" |
| 178 | 11 (fold-tree-leaves + 0 tree-lookup '(1))) |
| 179 | (test-equal "fold-tree-leaves, two roots" |
| 180 | 3 (fold-tree-leaves add-one 0 tree-lookup '(0 1))) |
| 181 | (test-equal "fold-tree-leaves, two roots, sum" |
| 182 | 13 (fold-tree-leaves + 0 tree-lookup '(0 1)))) |
| 183 | |
| 184 | (test-assert "filtered-port, file" |
| 185 | (let* ((file (search-path %load-path "guix.scm")) |
| 186 | (input (open-file file "r0b"))) |
| 187 | (let*-values (((compressed pids1) |
| 188 | (filtered-port `(,%gzip "-c" "--fast") input)) |
| 189 | ((decompressed pids2) |
| 190 | (filtered-port `(,%gzip "-d") compressed))) |
| 191 | (and (every (compose zero? cdr waitpid) |
| 192 | (append pids1 pids2)) |
| 193 | (equal? (get-bytevector-all decompressed) |
| 194 | (call-with-input-file file get-bytevector-all)))))) |
| 195 | |
| 196 | (test-assert "filtered-port, non-file" |
| 197 | (let ((data (call-with-input-file (search-path %load-path "guix.scm") |
| 198 | get-bytevector-all))) |
| 199 | (let*-values (((compressed pids1) |
| 200 | (filtered-port `(,%gzip "-c" "--fast") |
| 201 | (open-bytevector-input-port data))) |
| 202 | ((decompressed pids2) |
| 203 | (filtered-port `(,%gzip "-d") compressed))) |
| 204 | (and (pk (every (compose zero? cdr waitpid) |
| 205 | (append pids1 pids2))) |
| 206 | (equal? (get-bytevector-all decompressed) data))))) |
| 207 | |
| 208 | (test-assert "filtered-port, does not exist" |
| 209 | (let* ((file (search-path %load-path "guix.scm")) |
| 210 | (input (open-file file "r0b"))) |
| 211 | (let-values (((port pids) |
| 212 | (filtered-port '("/does/not/exist") input))) |
| 213 | (any (compose (negate zero?) cdr waitpid) |
| 214 | pids)))) |
| 215 | |
| 216 | (test-assert "compressed-port, decompressed-port, non-file" |
| 217 | (let ((data (call-with-input-file (search-path %load-path "guix.scm") |
| 218 | get-bytevector-all))) |
| 219 | (let*-values (((compressed pids1) |
| 220 | (compressed-port 'xz (open-bytevector-input-port data))) |
| 221 | ((decompressed pids2) |
| 222 | (decompressed-port 'xz compressed))) |
| 223 | (and (every (compose zero? cdr waitpid) |
| 224 | (append pids1 pids2)) |
| 225 | (equal? (get-bytevector-all decompressed) data))))) |
| 226 | |
| 227 | (false-if-exception (delete-file temp-file)) |
| 228 | (test-assert "compressed-output-port + decompressed-port" |
| 229 | (let* ((file (search-path %load-path "guix/derivations.scm")) |
| 230 | (data (call-with-input-file file get-bytevector-all)) |
| 231 | (port (open-file temp-file "w0b"))) |
| 232 | (call-with-compressed-output-port 'xz port |
| 233 | (lambda (compressed) |
| 234 | (put-bytevector compressed data))) |
| 235 | (close-port port) |
| 236 | |
| 237 | (bytevector=? data |
| 238 | (call-with-decompressed-port 'xz (open-file temp-file "r0b") |
| 239 | get-bytevector-all)))) |
| 240 | |
| 241 | (false-if-exception (delete-file temp-file)) |
| 242 | (test-equal "fcntl-flock wait" |
| 243 | 42 ; the child's exit status |
| 244 | (let ((file (open-file temp-file "w0b"))) |
| 245 | ;; Acquire an exclusive lock. |
| 246 | (fcntl-flock file 'write-lock) |
| 247 | (match (primitive-fork) |
| 248 | (0 |
| 249 | (dynamic-wind |
| 250 | (const #t) |
| 251 | (lambda () |
| 252 | ;; Reopen FILE read-only so we can have a read lock. |
| 253 | (let ((file (open-file temp-file "r0b"))) |
| 254 | ;; Wait until we can acquire the lock. |
| 255 | (fcntl-flock file 'read-lock) |
| 256 | (primitive-exit (read file))) |
| 257 | (primitive-exit 1)) |
| 258 | (lambda () |
| 259 | (primitive-exit 2)))) |
| 260 | (pid |
| 261 | ;; Write garbage and wait. |
| 262 | (display "hello, world!" file) |
| 263 | (force-output file) |
| 264 | (sleep 1) |
| 265 | |
| 266 | ;; Write the real answer. |
| 267 | (seek file 0 SEEK_SET) |
| 268 | (truncate-file file 0) |
| 269 | (write 42 file) |
| 270 | (force-output file) |
| 271 | |
| 272 | ;; Unlock, which should let the child continue. |
| 273 | (fcntl-flock file 'unlock) |
| 274 | |
| 275 | (match (waitpid pid) |
| 276 | ((_ . status) |
| 277 | (let ((result (status:exit-val status))) |
| 278 | (close-port file) |
| 279 | result))))))) |
| 280 | |
| 281 | (test-equal "fcntl-flock non-blocking" |
| 282 | EAGAIN ; the child's exit status |
| 283 | (match (pipe) |
| 284 | ((input . output) |
| 285 | (match (primitive-fork) |
| 286 | (0 |
| 287 | (dynamic-wind |
| 288 | (const #t) |
| 289 | (lambda () |
| 290 | (close-port output) |
| 291 | |
| 292 | ;; Wait for the green light. |
| 293 | (read-char input) |
| 294 | |
| 295 | ;; Open FILE read-only so we can have a read lock. |
| 296 | (let ((file (open-file temp-file "w0"))) |
| 297 | (catch 'flock-error |
| 298 | (lambda () |
| 299 | ;; This attempt should throw EAGAIN. |
| 300 | (fcntl-flock file 'write-lock #:wait? #f)) |
| 301 | (lambda (key errno) |
| 302 | (primitive-exit (pk 'errno errno))))) |
| 303 | (primitive-exit -1)) |
| 304 | (lambda () |
| 305 | (primitive-exit -2)))) |
| 306 | (pid |
| 307 | (close-port input) |
| 308 | (let ((file (open-file temp-file "w0"))) |
| 309 | ;; Acquire an exclusive lock. |
| 310 | (fcntl-flock file 'write-lock) |
| 311 | |
| 312 | ;; Tell the child to continue. |
| 313 | (write 'green-light output) |
| 314 | (force-output output) |
| 315 | |
| 316 | (match (waitpid pid) |
| 317 | ((_ . status) |
| 318 | (let ((result (status:exit-val status))) |
| 319 | (fcntl-flock file 'unlock) |
| 320 | (close-port file) |
| 321 | result))))))))) |
| 322 | |
| 323 | ;; This is actually in (guix store). |
| 324 | (test-equal "store-path-package-name" |
| 325 | "bash-4.2-p24" |
| 326 | (store-path-package-name |
| 327 | (string-append (%store-prefix) |
| 328 | "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24"))) |
| 329 | |
| 330 | (test-equal "canonical-newline-port" |
| 331 | "This is a journey\nInto the sound\nA journey ...\n" |
| 332 | (let ((port (open-string-input-port |
| 333 | "This is a journey\r\nInto the sound\r\nA journey ...\n"))) |
| 334 | (get-string-all (canonical-newline-port port)))) |
| 335 | |
| 336 | (test-end) |
| 337 | |
| 338 | (false-if-exception (delete-file temp-file)) |