summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am1
-rw-r--r--guix/scripts/hash.scm43
-rw-r--r--tests/guix-hash.sh28
3 files changed, 53 insertions, 19 deletions
diff --git a/Makefile.am b/Makefile.am
index d1f06d7a71..6786bd7327 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -324,6 +324,7 @@ SH_TESTS =					\
   tests/guix-build.sh				\
   tests/guix-download.sh			\
   tests/guix-gc.sh				\
+  tests/guix-hash.sh				\
   tests/guix-package.sh
 
 TESTS = $(SCM_TESTS) $(SH_TESTS)
diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm
index deded63136..1b14aaadd0 100644
--- a/guix/scripts/hash.scm
+++ b/guix/scripts/hash.scm
@@ -98,23 +98,28 @@ Supported formats: 'nix-base32' (default), 'base32', and 'base16'
                   (alist-cons 'argument arg result))
                 %default-options))
 
-    (let* ((opts (parse-options))
-           (args (filter-map (match-lambda
-                              (('argument . value)
-                               value)
-                              (_ #f))
-                             (reverse opts)))
-           (fmt  (assq-ref opts 'format)))
+  (define (eof->null x)
+    (if (eof-object? x)
+        #vu8()
+        x))
 
-      (match args
-        ((file)
-         (catch 'system-error
-           (lambda ()
-             (format #t "~a~%"
-                     (call-with-input-file file
-                       (compose fmt sha256 get-bytevector-all))))
-           (lambda args
-             (leave (_ "~a~%")
-                    (strerror (system-error-errno args))))))
-        (_
-         (leave (_ "wrong number of arguments~%"))))))
+  (let* ((opts (parse-options))
+         (args (filter-map (match-lambda
+                            (('argument . value)
+                             value)
+                            (_ #f))
+                           (reverse opts)))
+         (fmt  (assq-ref opts 'format)))
+
+    (match args
+      ((file)
+       (catch 'system-error
+         (lambda ()
+           (format #t "~a~%"
+                   (call-with-input-file file
+                     (compose fmt sha256 eof->null get-bytevector-all))))
+         (lambda args
+           (leave (_ "~a~%")
+                  (strerror (system-error-errno args))))))
+      (_
+       (leave (_ "wrong number of arguments~%"))))))
diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh
new file mode 100644
index 0000000000..53325ce1f4
--- /dev/null
+++ b/tests/guix-hash.sh
@@ -0,0 +1,28 @@
+# GNU Guix --- Functional package management for GNU
+# Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
+#
+# This file is part of GNU Guix.
+#
+# GNU Guix is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or (at
+# your option) any later version.
+#
+# GNU Guix is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+#
+# Test the `guix hash' command-line utility.
+#
+
+guix hash --version
+
+test `guix hash /dev/null` = 0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
+test `guix hash -f nix-base32 /dev/null` = 0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73
+test `guix hash -f hex /dev/null` = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
+test `guix hash -f base32 /dev/null` = 4oymiquy7qobjgx36tejs35zeqt24qpemsnzgtfeswmrw6csxbkq