From bee54ed3ec9ef7959c6c858f7f51f987c27fee7a Mon Sep 17 00:00:00 2001 From: HacKan Date: Wed, 26 Apr 2017 18:47:18 -0300 Subject: [PATCH] Add optional argument to output option Indicate that change in the help and manual, and add tests for it. Change visual order of options in help message, and manual entry. Add explanation about tests in README. --- .gitignore | 2 + README.md | 8 +++- man/b2rsum.1 | 80 +++++++++++++++++++++++++------------- src/b2rsum.bash | 60 ++++++++++++++++------------ tests/t0010-basic-tests.sh | 24 ++++++++++-- 5 files changed, 118 insertions(+), 56 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b72f9be --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +*.swp diff --git a/README.md b/README.md index 002daa9..a559e71 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,13 @@ These come by default in most modern distros. ## Installing -Clone this repository or download a [release](https://github.com/HacKanCuBa/b2rsum/releases), then run `make install` as privileged user (probably `sudo make install`). You can optionally do `make lint` to check for errors (it uses [shellcheck](https://github.com/koalaman/shellcheck)). +Clone this repository or download a [release](https://github.com/HacKanCuBa/b2rsum/releases), then run `make install` as privileged user (probably `sudo make install`). + +### Verifying + +You can optionally do `make lint && make test` to check for errors (it requires [shellcheck](https://github.com/koalaman/shellcheck) for linting), specially if you are cloning the repository. + +Tests can also be run individually with `make tests/tXXXX-description.sh` and debugged with `./tests/tXXXX-description.sh -v`. [Sharness](https://github.com/chriscool/sharness) is used as framework. ## Examples of use diff --git a/man/b2rsum.1 b/man/b2rsum.1 index e41a0d6..ad5bd98 100644 --- a/man/b2rsum.1 +++ b/man/b2rsum.1 @@ -1,4 +1,4 @@ -.TH B2RSUM "1" "April 2017" "b2rsum Copyright (C) 2017 HacKan (https://hackan.net)" "BLAKE2 Recursive Sum" +.TH B2RSUM "1" "April 2017" "b2rsum v0.1.2" "BLAKE2 Recursive Sum" .SH NAME b2rsum \- compute and check BLAKE2 message digest recursively @@ -15,55 +15,75 @@ wrapper that adds the capability of processing an entire directory, so the sums When FILE or DIRECTORY is empty or a dot (.), compute current directory. When FILE is -, read standard input. +The default mode is to compute checksums. Check mode is indicated with --check. + .SH OPTIONS .TP -\fB\-b\fR, \fB\-\-binary\fR -read in binary mode -.TP \fB\-c\fR, \fB\-\-check\fR read BLAKE2 sums from the FILEs and check them + .TP -\fB\-l\fR, \fB\-\-length\fR -digest length in bits; must not exceed the maximum for -the blake2 algorithm and must be a multiple of 8 -.TP -\fB\-\-tag\fR -create a BSD\-style checksum -.TP -\fB\-t\fR, \fB\-\-text\fR -read in text mode (default) -.TP -\fB\-o\fR, \fB\-\-output\fR -output to a file named BLAKE2SUMS in the -current directory instead of standard output +\fB\-o\fR[\fI\,FILE\/\fR], \fB\-\-output\fR[=\fI\,FILE\/\fR] +output to FILE instead of standard output, or a +file named BLAKE2SUMS in the current +directory if FILE is not specified + .TP \fB\-q\fR, \fB\-\-quiet\fR quiet mode: don't print messages, only hashes; during check mode, don't print OK for each successfully verified file + .TP \fB\-s\fR, \fB\-\-status\fR very quiet mode: output only hashes, no messages; status code shows success + +.TP +\fB\-\-license\fR +show license and exit + +.TP +\fB\-\-version\fR +show version information and exit + +.TP +\fB\-h\fR, \fB\-\-help\fR +show this text and exit + +.SS "The following four options are useful only when computing checksums:" + +.TP +\fB\-t\fR, \fB\-\-text\fR +read in text mode (default) + +.TP +\fB\-b\fR, \fB\-\-binary\fR +read in binary mode + +.TP +\fB\-\-tag\fR +create a BSD\-style checksum + +.TP +\fB\-l\fR, \fB\-\-length\fR +digest length in bits; must not exceed the maximum for +the blake2 algorithm and must be a multiple of 8 + .SS "The following three options are useful only when verifying checksums:" + .TP \fB\-\-ignore\-missing\fR don't fail or report status for missing files + .TP \fB\-\-strict\fR exit non\-zero for improperly formatted checksum lines + .TP \fB\-w\fR, \fB\-\-warn\fR warn about improperly formatted checksum lines -.TP -\fB\-h\fR, \fB\-\-help\fR -show this text and exit -.TP -\fB\-\-license\fR -show license and exit -.TP -\fB\-\-version\fR -show version information and exit + .PP The sums are computed as described in RFC 7693. When checking, the input should be a former output of this program. The default mode is to print a @@ -74,7 +94,7 @@ line with checksum, a space, a character indicating input mode ('*' for binary, .BR b2sum (1) .SH AUTHOR -.B b2sum +.B b2rsum was written by .MT hackan@gmail.com HacKan @@ -85,6 +105,12 @@ repository .UE for additional information. +.SH REPORTING BUGS +Please create an issue at the +.UR https://\:github.com/HacKanCuBa/b2rsum/issues +repository +.UE . + .SH COPYING This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/b2rsum.bash b/src/b2rsum.bash index 8b6240e..4f77416 100755 --- a/src/b2rsum.bash +++ b/src/b2rsum.bash @@ -34,13 +34,17 @@ # Changelog #===================== # +# v0.1.2 +# - Some changes in the order of info in help message. +# - Output option now accepts optional argument. +# # v0.1.1 -# - Fix check mode not reading inline data -# - Fix length option being improperly parsed -# - Bail out on wrong option +# - Fix check mode not reading inline data. +# - Fix length option being improperly parsed. +# - Bail out on wrong option. # # v0.1.0 -# - First release +# - First release. # # v0.0.1 # - Developing based on sharsum. @@ -48,7 +52,7 @@ #------------------------------------------------------------------------ # Config -declare -r VERSION="0.1.1" +declare -r VERSION="0.1.2" declare -r OUTPUT_FILENAME_DEFAULT="BLAKE2SUMS" declare -r QUIET_DEFAULT=false #--------------------------# @@ -146,34 +150,39 @@ cmd_help() { cat <<-_EOF Usage: ${PROGRAM} [OPTION]... [FILE or DIRECTORY]... + Print or check BLAKE2 (512-bit) checksums recursively. If no FILE or DIRECTORY is indicated, or it's a dot (.), then the current directory is processed. + The default mode is to compute checksums. Check mode is indicated with --check. Options: - -b, --binary read in binary mode - -c, --check read BLAKE2 sums from the FILEs and check them - -l, --length digest length in bits; must not exceed the maximum for - the blake2 algorithm and must be a multiple of 8 - --tag create a BSD-style checksum - -t, --text read in text mode (default) - -o, --output output to a file named $OUTPUT_FILENAME_DEFAULT in the - current directory instead of standard output - -q, --quiet quiet mode: don't print messages, only hashes; during - check mode, don't print OK for each successfully - verified file - -s, --status very quiet mode: output only hashes, no messages; status - code shows success + -c, --check read BLAKE2 sums from the FILEs and check them + -o[FILE], --output[=FILE] output to FILE instead of standard output, or a + file named $OUTPUT_FILENAME_DEFAULT in the current + directory if FILE is not specified + -q, --quiet quiet mode: don't print messages, only hashes; + during check mode, don't print OK for each + successfully verified file + -s, --status very quiet mode: output only hashes, no messages; + status code shows success + --license show license and exit + --version show version information and exit + -h, --help show this text and exit + + The following four options are useful only when computing checksums: + -t, --text read in text mode (default) + -b, --binary read in binary mode + --tag create a BSD-style checksum + -l, --length digest length in bits; must not exceed the maximum + for the blake2 algorithm and must be a multiple + of 8 The following three options are useful only when verifying checksums: --ignore-missing don't fail or report status for missing files --strict exit non-zero for improperly formatted checksum lines -w, --warn warn about improperly formatted checksum lines - -h, --help show this text and exit - --license show license and exit - --version show version information and exit - Sums are made using 'b2sum'. Full documentation at: . The sums are computed as described in RFC 7693. When checking, the input @@ -273,7 +282,7 @@ declare OUTPUT_FILE='' check_dependencies || die "Dependencies not met, can't continue" # Arguments -OPTS="$(getopt -o hbctwosql: -l help,version,license,binary,check,length:,text,tag,ignore-missing,quiet,status,strict,warn,output -n "$PROGRAM" -- "$@")" +OPTS="$(getopt -o hbctwsql:o:: -l help,version,license,binary,check,length:,text,tag,ignore-missing,quiet,status,strict,warn,output:: -n "$PROGRAM" -- "$@")" [[ $? -ne 0 ]] && die "Wrong option. Try '$PROGRAM --help' for more information." eval set -- "$OPTS" @@ -291,7 +300,10 @@ while true; do case $1 in -s|--status) VERY_QUIET=true; B2SUM_OPTS_CHECK+=( '--status' ); shift;; --strict) B2SUM_OPTS_CHECK+=( '--strict' ); shift;; -w|--warn) B2SUM_OPTS_CHECK+=( '--warn' ); shift;; - -o|--output) OUTPUT_FILE="$OUTPUT_FILENAME_DEFAULT"; check_output_file; shift;; + -o|--output) OUTPUT_FILE="${2:-$OUTPUT_FILENAME_DEFAULT}" + check_output_file + shift 2 + ;; --) shift; break ;; esac done diff --git a/tests/t0010-basic-tests.sh b/tests/t0010-basic-tests.sh index 67f5b07..8c457cc 100755 --- a/tests/t0010-basic-tests.sh +++ b/tests/t0010-basic-tests.sh @@ -12,20 +12,36 @@ test_expect_success 'Verify license' ' "$B2RSUM" --license | grep "under the terms of the GNU General Public License" ' +test_expect_success 'Verify default behaviour' ' + "$B2RSUM" && "$B2RSUM" | grep "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce " +' + test_expect_success 'Verify invalid parameter: --invalid' ' ! "$B2RSUM" --invalid ' +test_expect_success 'Verify parameter w/o argument: --output' ' + "$B2RSUM" --output && [[ -r BLAKE2SUMS ]] +' + +test_expect_success 'Verify parameter w/right argument: --output' ' + "$B2RSUM" --output=B2SUMS && [[ -r B2SUMS ]] +' + +test_expect_success 'Verify parameter w/wrong argument: --output' ' + ! "$B2RSUM" --output WRONG +' + test_expect_success 'Verify parameter w/o argument: --length' ' ! "$B2RSUM" --length ' -test_expect_success 'Verify parameter w/wrong argument: --length' ' - ! "$B2RSUM" --length 7 +test_expect_success 'Verify parameter w/right argument: --length' ' + "$B2RSUM" --length 8 ' -test_expect_success 'Verify default behaviour' ' - "$B2RSUM" +test_expect_success 'Verify parameter w/wrong argument: --length' ' + ! "$B2RSUM" --length 7 ' test_expect_success 'Verify --quiet during creation' '