tests: Add `guix hash' test.

* guix/scripts/hash.scm (guix-hash)[eof->null]: New procedure.
  Use it to convert the EOF object to the empty bytevector.
* tests/guix-hash.sh: New file.
* Makefile.am (SH_TESTS): Add it.
This commit is contained in:
Ludovic Courtès 2013-05-11 15:09:56 +02:00
parent 438bb042ff
commit ccbce84809
3 changed files with 53 additions and 19 deletions

View File

@ -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)

View File

@ -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~%"))))))

28
tests/guix-hash.sh Normal file
View File

@ -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