]> gitweb.hhaalo.de Git - scantopdf.git/commitdiff
add test for optimize method
authorBastian Dehn <hhaalo@arcor.de>
Sat, 5 Jul 2025 08:31:20 +0000 (10:31 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 5 Jul 2025 08:31:20 +0000 (10:31 +0200)
scanbasic.bats [new file with mode: 0755]
scanbasic.sh

diff --git a/scanbasic.bats b/scanbasic.bats
new file mode 100755 (executable)
index 0000000..075b3cf
--- /dev/null
@@ -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
+}
index 454af45eb56f042481bc6ef21946067744d5830e..309ccb5dcf2b6712c1e6bb1a12728a10635d5101 100644 (file)
@@ -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 $?
 }