--- /dev/null
+#!/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
+}
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 $?
}