diff --git a/.gitignore b/.gitignore index ea8c4bf..c849890 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +dist/ diff --git a/justfile b/justfile new file mode 100644 index 0000000..88e7fe6 --- /dev/null +++ b/justfile @@ -0,0 +1,15 @@ +dist: target-release-skip + if test ! -d dist ; then mkdir dist ; fi + cp target/release/skip dist/ + +inttest: target-debug-skip + ./test.sh + +target-release-skip: unittest inttest + cargo build --release + +target-debug-skip: + cargo build + +unittest: target-debug-skip + cargo test diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..4e0e078 --- /dev/null +++ b/test.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash + +set -e + +SKIP="./target/debug/skip" +DIFF="diff -u --color" + +if test ! -x $SKIP ; then + echo "File missing: $SKIP - try 'zig build'" + exit 1 +fi + +echo "> skip a line when reading from stdin" +INPUT=$(cat< test.expect +echo "$INPUT" | $SKIP 1 > test.out +$DIFF test.expect test.out +rm test.expect test.out + +echo "> skip a line when reading from a file" +cat< test.in +line 1 +line 2 +EOF +echo "line 2" > test.expect +$SKIP 1 test.in > test.out +$DIFF test.expect test.out +rm test.expect test.out + +echo "> skip until 2 matching lines seen" +cat< test.in +alpha +beta +alpha +alpha +gamma +alpha +EOF +cat< test.expect +alpha +gamma +alpha +EOF +$SKIP 2 test.in --line alpha > test.out +$DIFF test.expect test.out +rm test.in test.expect test.out + +echo "> skip lines until 2 tokens seen" +cat< test.in +Lorem ipsum dolor sit amet, +consectetur adipiscing elit, +sed do eiusmod tempor incididunt +ut labore et dolore magna aliqua. +Ut enim ad minim veniam, +quis nostrud exercitation ullamco +laboris nisi ut aliquip ex ea +commodo consequat. +EOF +cat< test.expect +Ut enim ad minim veniam, +quis nostrud exercitation ullamco +laboris nisi ut aliquip ex ea +commodo consequat. +EOF +$SKIP 2 test.in --token dolor > test.out +$DIFF test.expect test.out +rm test.in test.expect test.out + +echo "> handle unknown parameter with simple error message" +cat< test.expect.err +error: unexpected argument '--foo' found + + note: to pass '--foo' as a value, use '-- --foo' + +Usage: skip [OPTIONS] [FILE] + +For more information, try '--help'. +EOF +cat< test.expect +EOF +touch test.out test.err +$SKIP --foo > test.out 2> test.err || true +$DIFF test.expect test.out +$DIFF test.expect.err test.err +rm test.expect test.out +rm test.expect.err test.err + +echo "> handle ignore-extra when token is missing" +cat< test.expect.err +error: the following required arguments were not provided: + --token + + +Usage: skip --ignore-extras --token [FILE] + +For more information, try '--help'. +EOF +cat< test.expect +EOF +touch test.out test.err +$SKIP --ignore-extras > test.out 2> test.err || true +$DIFF test.expect test.out +$DIFF test.expect.err test.err +rm test.expect test.out +rm test.expect.err test.err + +echo "> skip lines until 4 tokens seen - ignored extra tokens on same line" +cat< test.in +Lorem ipsum dolor sit amet, +consectetur adipiscing elit, +sed do eiusmod tempor incididunt +ut labore et dolore magna aliqua. +Ut enim ad minim veniam, +quis nostrud exercitation ullamco +laboris nisi ut aliquip ex ea +commodo consequat. +EOF +cat< test.expect +quis nostrud exercitation ullamco +laboris nisi ut aliquip ex ea +commodo consequat. +EOF +$SKIP 4 test.in --token m --ignore-extras > test.out +$DIFF test.expect test.out +rm test.in test.expect test.out + +echo done