]> gitweb.hhaalo.de Git - scantopdf.git/commitdiff
add test addocr
authorBastian Dehn <hhaalo@arcor.de>
Sat, 5 Jul 2025 08:53:38 +0000 (10:53 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 5 Jul 2025 08:54:48 +0000 (10:54 +0200)
scanbasic.bats
scanbasic.sh

index 075b3cf52e061bb55ac935576810c79b796d9b8f..aec0d1124467bbbf1ee8476af13d85b11737fe25 100755 (executable)
@@ -1,5 +1,10 @@
 #!/usr/bin/bats
 
+ocrmypdf() {
+       echo "$ocrmypdf_return"
+       return "$ocrmypdf_exit"
+}
+
 qpdf() {
        echo "$qpdf_return"
        return "$qpdf_exit"
@@ -15,10 +20,24 @@ setup() {
        load "/usr/lib/bats/bats-support/load"
 
        source ./scanbasic.sh
+
+       export -f qpdf
        qpdf_return=
        qpdf_exit=0
+
+       export -f mv
        mv_return=
        mv_exit=0
+
+       export -f ocrmypdf
+       ocrmypdf_return=
+       ocrmypdf_exit=0
+}
+
+teardown() {
+       export -n qpdf
+       export -n mv
+       export -n ocrmypdf
 }
 
 @test "should failure optimize input parameter is missing" {
@@ -52,3 +71,25 @@ setup() {
 
        assert_success
 }
+
+@test "should failure addocr missing input parameter" {
+       run addocr
+
+       assert_failure
+       assert_line --index 0 "❌ ERROR: missing input"
+}
+
+@test "should failure addocr ocrmypdf fails" {
+       ocrmypdf_exit=1
+
+       run addocr "testfile"
+
+       assert_failure
+       assert_line --index 0 "❌ ERROR: could not add ocr on testfile"
+}
+
+@test "should success addocr" {
+       run addocr "testfile"
+
+       assert_success
+}
index 309ccb5dcf2b6712c1e6bb1a12728a10635d5101..637a9c9411e141e69ed992e83642dfc1b29c1b59 100644 (file)
@@ -29,28 +29,26 @@ optimize()
 
 addocr()
 {
-       local input=$1
-       local output=$input-out
-       local error=""
+       local input="$1"
+       local output="$input-out"
 
        command -v ocrmypdf > /dev/null \
                || errlog "command ocrmypdf not found" \
                || return $?
-       echo $input
-       error=$(ocrmypdf -l deu $input $output 2>&1) \
+       [ -n "$input" ] \
+               || errlog "missing input" \
+               || return $?
+
+       ocrmypdf -l deu $input $output 2>&1 \
                || errlog "could not add ocr on $input" \
                || return $?
 
-       if [ -n "$(echo $error | grep offset)" ]; then
-               printf "%s: %s\n" $input $error >> $HOME/addocr-error.log
-       fi
+       [ ! -f $output ] && return 0
 
-       if [ -f $output ]; then
-               mv $output $input \
-                       || errlog "could not rename $output to $input" \
-                       || return $?
-               optimize $input || return $?
-       fi
+       mv $output $input \
+               || errlog "could not rename $output to $input" \
+               || return $?
+       optimize $input || return $?
 }
 
 createonepdf()