| 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 "strip-keyword-arguments" |
| 101 | '(a #:b b #:c c) |
| 102 | (strip-keyword-arguments '(#:foo #:bar #:baz) |
| 103 | '(a #:foo 42 #:b b #:baz 3 |
| 104 | #:c c #:bar 4))) |
| 105 | |
| 106 | (test-equal "ensure-keyword-arguments" |
| 107 | '((#:foo 2) |
| 108 | (#:foo 2 #:bar 3) |
| 109 | (#:foo 42 #:bar 3)) |
| 110 | (list (ensure-keyword-arguments '(#:foo 2) '(#:foo 2)) |
| 111 | (ensure-keyword-arguments '(#:foo 2) '(#:bar 3)) |
| 112 | (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42)))) |
| 113 | |
| 114 | (test-equal "default-keyword-arguments" |
| 115 | '((#:foo 2) |
| 116 | (#:foo 2) |
| 117 | (#:foo 2 #:bar 3) |
| 118 | (#:foo 2 #:bar 3) |
| 119 | (#:foo 2 #:bar 3)) |
| 120 | (list (default-keyword-arguments '() '(#:foo 2)) |
| 121 | (default-keyword-arguments '(#:foo 2) '(#:foo 4)) |
| 122 | (default-keyword-arguments '() '(#:bar 3 #:foo 2)) |
| 123 | (default-keyword-arguments '(#:bar 3) '(#:foo 2)) |
| 124 | (default-keyword-arguments '(#:foo 2 #:bar 3) '(#:bar 6)))) |
| 125 | |
| 126 | (test-equal "substitute-keyword-arguments" |
| 127 | '((#:foo 3) |
| 128 | (#:foo 3) |
| 129 | (#:foo 3 #:bar (1 2)) |
| 130 | (#:bar (1 2) #:foo 3) |
| 131 | (#:foo 3)) |
| 132 | (list (substitute-keyword-arguments '(#:foo 2) |
| 133 | ((#:foo f) (1+ f))) |
| 134 | (substitute-keyword-arguments '() |
| 135 | ((#:foo f 2) (1+ f))) |
| 136 | (substitute-keyword-arguments '(#:foo 2 #:bar (2)) |
| 137 | ((#:foo f) (1+ f)) |
| 138 | ((#:bar b) (cons 1 b))) |
| 139 | (substitute-keyword-arguments '(#:foo 2) |
| 140 | ((#:foo _) 3) |
| 141 | ((#:bar b '(2)) (cons 1 b))) |
| 142 | (substitute-keyword-arguments '(#:foo 2) |
| 143 | ((#:foo f 1) (1+ f)) |
| 144 | ((#:bar b) (cons 42 b))))) |
| 145 | |
| 146 | (test-assert "filtered-port, file" |
| 147 | (let* ((file (search-path %load-path "guix.scm")) |
| 148 | (input (open-file file "r0b"))) |
| 149 | (let*-values (((compressed pids1) |
| 150 | (filtered-port `(,%gzip "-c" "--fast") input)) |
| 151 | ((decompressed pids2) |
| 152 | (filtered-port `(,%gzip "-d") compressed))) |
| 153 | (and (every (compose zero? cdr waitpid) |
| 154 | (append pids1 pids2)) |
| 155 | (equal? (get-bytevector-all decompressed) |
| 156 | (call-with-input-file file get-bytevector-all)))))) |
| 157 | |
| 158 | (test-assert "filtered-port, non-file" |
| 159 | (let ((data (call-with-input-file (search-path %load-path "guix.scm") |
| 160 | get-bytevector-all))) |
| 161 | (let*-values (((compressed pids1) |
| 162 | (filtered-port `(,%gzip "-c" "--fast") |
| 163 | (open-bytevector-input-port data))) |
| 164 | ((decompressed pids2) |
| 165 | (filtered-port `(,%gzip "-d") compressed))) |
| 166 | (and (pk (every (compose zero? cdr waitpid) |
| 167 | (append pids1 pids2))) |
| 168 | (equal? (get-bytevector-all decompressed) data))))) |
| 169 | |
| 170 | (test-assert "filtered-port, does not exist" |
| 171 | (let* ((file (search-path %load-path "guix.scm")) |
| 172 | (input (open-file file "r0b"))) |
| 173 | (let-values (((port pids) |
| 174 | (filtered-port '("/does/not/exist") input))) |
| 175 | (any (compose (negate zero?) cdr waitpid) |
| 176 | pids)))) |
| 177 | |
| 178 | (test-assert "compressed-port, decompressed-port, non-file" |
| 179 | (let ((data (call-with-input-file (search-path %load-path "guix.scm") |
| 180 | get-bytevector-all))) |
| 181 | (let*-values (((compressed pids1) |
| 182 | (compressed-port 'xz (open-bytevector-input-port data))) |
| 183 | ((decompressed pids2) |
| 184 | (decompressed-port 'xz compressed))) |
| 185 | (and (every (compose zero? cdr waitpid) |
| 186 | (append pids1 pids2)) |
| 187 | (equal? (get-bytevector-all decompressed) data))))) |
| 188 | |
| 189 | (false-if-exception (delete-file temp-file)) |
| 190 | (test-assert "compressed-output-port + decompressed-port" |
| 191 | (let* ((file (search-path %load-path "guix/derivations.scm")) |
| 192 | (data (call-with-input-file file get-bytevector-all)) |
| 193 | (port (open-file temp-file "w0b"))) |
| 194 | (call-with-compressed-output-port 'xz port |
| 195 | (lambda (compressed) |
| 196 | (put-bytevector compressed data))) |
| 197 | (close-port port) |
| 198 | |
| 199 | (bytevector=? data |
| 200 | (call-with-decompressed-port 'xz (open-file temp-file "r0b") |
| 201 | get-bytevector-all)))) |
| 202 | |
| 203 | ;; This is actually in (guix store). |
| 204 | (test-equal "store-path-package-name" |
| 205 | "bash-4.2-p24" |
| 206 | (store-path-package-name |
| 207 | (string-append (%store-prefix) |
| 208 | "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24"))) |
| 209 | |
| 210 | (test-equal "canonical-newline-port" |
| 211 | "This is a journey\nInto the sound\nA journey ...\n" |
| 212 | (let ((port (open-string-input-port |
| 213 | "This is a journey\r\nInto the sound\r\nA journey ...\n"))) |
| 214 | (get-string-all (canonical-newline-port port)))) |
| 215 | |
| 216 | |
| 217 | (test-equal "edit-expression" |
| 218 | "(display \"GNU Guix\")\n(newline)\n" |
| 219 | (begin |
| 220 | (call-with-output-file temp-file |
| 221 | (lambda (port) |
| 222 | (display "(display \"xiuG UNG\")\n(newline)\n" port))) |
| 223 | (edit-expression `((filename . ,temp-file) |
| 224 | (line . 0) |
| 225 | (column . 9)) |
| 226 | string-reverse) |
| 227 | (call-with-input-file temp-file get-string-all))) |
| 228 | |
| 229 | (test-end) |
| 230 | |
| 231 | (false-if-exception (delete-file temp-file)) |