From f8eace721cc82c4106510f1dfaa3ddcac5c01b23 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 5 Jul 2025 10:31:20 +0200 Subject: [PATCH] add test for optimize method --- scanbasic.bats | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ scanbasic.sh | 9 ++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100755 scanbasic.bats diff --git a/scanbasic.bats b/scanbasic.bats new file mode 100755 index 0000000..075b3cf --- /dev/null +++ b/scanbasic.bats @@ -0,0 +1,54 @@ +#!/usr/bin/bats + +qpdf() { + echo "$qpdf_return" + return "$qpdf_exit" +} + +mv() { + echo "$mv_return" + return "$mv_exit" +} + +setup() { + load "/usr/lib/bats/bats-assert/load" + load "/usr/lib/bats/bats-support/load" + + source ./scanbasic.sh + qpdf_return= + qpdf_exit=0 + mv_return= + mv_exit=0 +} + +@test "should failure optimize input parameter is missing" { + run optimize + + assert_failure + assert_line --index 0 "❌ ERROR: missing input" +} + +@test "should failure optimize when qpdf failes" { + qpdf_exit=1 + + run optimize "testfile" + + assert_failure + assert_line --index 0 "❌ ERROR: could not linearize testfile" +} + +@test "should failure optimize when mv fails" { + mv_exit=1 + + run optimize "testfile" + + assert_failure + assert_line --index 0 \ + "❌ ERROR: could not rename testfile-out to testfile" +} + +@test "should success optimize" { + run optimize "testfile" + + assert_success +} diff --git a/scanbasic.sh b/scanbasic.sh index 454af45..309ccb5 100644 --- a/scanbasic.sh +++ b/scanbasic.sh @@ -10,17 +10,20 @@ errlog() optimize() { - local input=$1 - local output=$input-out + local input="$1" + local output="$input-out" command -v qpdf > /dev/null \ || errlog "command qpdf not found" \ || return $? + [ -n "$input" ] \ + || errlog "missing input" \ + || return $? qpdf --linearize $input $output \ || errlog "could not linearize $input" \ || return $? mv $output $input \ - || errlog "cloud not rename $ouptut to $input" \ + || errlog "could not rename $output to $input" \ || return $? } -- 2.47.3