1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (test-utils)
20 #:use-module ((guix config) #:select (%gzip))
21 #:use-module (guix utils)
22 #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-11)
25 #:use-module (srfi srfi-64)
26 #:use-module (rnrs bytevectors)
27 #:use-module (rnrs io ports)
28 #:use-module (ice-9 match))
32 (test-assert "bytevector->base16-string->bytevector"
34 (equal? (base16-string->bytevector
35 (bytevector->base16-string bv))
37 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
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)))
51 (test-assert "package-name->name+version"
54 (let*-values (((full-name)
56 (string-append name "-" version)
59 (package-name->name+version full-name)))
60 (and (equal? name* name)
61 (equal? version* version)))))
65 ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
66 ("nixpkgs" "1.0pre22125_a28fe19")
69 (test-assert "guile-version>? 1.8"
70 (guile-version>? "1.8"))
72 (test-assert "guile-version>? 10.5"
73 (not (guile-version>? "10.5")))
75 (test-equal "string-tokenize*"
80 (list (string-tokenize* "foo" ":")
81 (string-tokenize* "foo;bar;baz" ";")
82 (string-tokenize* "foo!bar!" "!")
83 (string-tokenize* "foo+-+bar+-+baz" "+-+")))
85 (test-equal "string-replace-substring"
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")))
93 (test-equal "fold2, 1 list"
94 (list (reverse (iota 5))
95 (map - (reverse (iota 5))))
98 (fold2 (lambda (i r1 r2)
105 (test-equal "fold2, 2 lists"
106 (list (reverse '((a . 0) (b . 1) (c . 2) (d . 3)))
107 (reverse '((a . 0) (b . -1) (c . -2) (d . -3))))
110 (fold2 (lambda (k v r1 r2)
111 (values (alist-cons k v r1)
112 (alist-cons k (- v) r2)))
118 (test-assert "filtered-port, file"
119 (let* ((file (search-path %load-path "guix.scm"))
120 (input (open-file file "r0b")))
121 (let*-values (((compressed pids1)
122 (filtered-port `(,%gzip "-c" "--fast") input))
123 ((decompressed pids2)
124 (filtered-port `(,%gzip "-d") compressed)))
125 (and (every (compose zero? cdr waitpid)
126 (append pids1 pids2))
127 (equal? (get-bytevector-all decompressed)
128 (call-with-input-file file get-bytevector-all))))))
130 (test-assert "filtered-port, non-file"
131 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
132 get-bytevector-all)))
133 (let*-values (((compressed pids1)
134 (filtered-port `(,%gzip "-c" "--fast")
135 (open-bytevector-input-port data)))
136 ((decompressed pids2)
137 (filtered-port `(,%gzip "-d") compressed)))
138 (and (pk (every (compose zero? cdr waitpid)
139 (append pids1 pids2)))
140 (equal? (get-bytevector-all decompressed) data)))))
142 ;; This is actually in (guix store).
143 (test-equal "store-path-package-name"
145 (store-path-package-name
146 (string-append (%store-prefix)
147 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
152 (exit (= (test-runner-fail-count (test-runner-current)) 0))