list-packages: Make 'snippet-link' more tolerant.
[guix.git] / tests / utils.scm
blob017d9170fa744a9687c4e83b6924288348706b4c
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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/>.
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))
30 (test-begin "utils")
32 (test-assert "bytevector->base16-string->bytevector"
33   (every (lambda (bv)
34            (equal? (base16-string->bytevector
35                     (bytevector->base16-string bv))
36                    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)))
49              gnu nix))))
51 (test-assert "package-name->name+version"
52   (every (match-lambda
53           ((name version)
54            (let*-values (((full-name)
55                           (if version
56                               (string-append name "-" version)
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")
63            ("foo-bar" "1.0")
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"))))
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*"
76   '(("foo")
77     ("foo" "bar" "baz")
78     ("foo" "bar" "")
79     ("foo" "bar" "baz"))
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"
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")))
93 (test-equal "fold2, 1 list"
94     (list (reverse (iota 5))
95           (map - (reverse (iota 5))))
96   (call-with-values
97       (lambda ()
98         (fold2 (lambda (i r1 r2)
99                  (values (cons i r1)
100                          (cons (- i) r2)))
101                '() '()
102                (iota 5)))
103     list))
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))))
108   (call-with-values
109       (lambda ()
110         (fold2 (lambda (k v r1 r2)
111                  (values (alist-cons k v r1)
112                          (alist-cons k (- v) r2)))
113                '() '()
114                '(a b c d)
115                '(0 1 2 3)))
116     list))
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"
144   "bash-4.2-p24"
145   (store-path-package-name
146    (string-append (%store-prefix)
147                   "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
149 (test-end)
152 (exit (= (test-runner-fail-count (test-runner-current)) 0))