]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
add postrm script with tests
authorBastian Dehn <hhaalo@arcor.de>
Thu, 16 Jul 2026 18:11:07 +0000 (20:11 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Thu, 16 Jul 2026 18:11:07 +0000 (20:11 +0200)
src/debian/postrm
tests/CMakeLists.txt
tests/postrm.bats [new file with mode: 0755]

index b6589bbbabe80c814e9bb3d46ef721d9ecb760fa..1ffbafaeceb61c76ee66d99cb5d5766dc1c3303c 100755 (executable)
@@ -1,36 +1,27 @@
 #!/bin/bash
-
 set -e
 
-errlog()
+find_config()
 {
-       local lastexit=$?
-       local msg="$1"
-
-       echo "$msg"
+       [ ! -f "$DLLCONFIG" ] \
+               && echo "WARNING: $DLLCONFIG does not exist" \
+               && return 1
+       echo "found $DLLCONFIG"
+}
 
-       return $lastexit
+delete_entries()
+{
+       local entry="kds_s2000w_net"
+       sed --in-place "/$entry/d"  "$DLLCONFIG"
+       echo "remove $entry from $DLLCONFIG"
 }
 
 main()
 {
-       local DLLCONFIG="/etc/sane.d/dll.conf"
-       local ENTRY="kds_s2000w_net"
-
-       [ -f "$DLLCONFIG" ] \
-               || errlog "ERROR: $DLLCONFIG does not exist" \
-               || return 0
-
-       echo "found $DLLCONFIG"
-       cat $DLLCONFIG | grep $ENTRY \
-               || errlog "$ENTRY entry does not exists in $DLLCONFIG" \
-               || return 0
-
-       sed -i "/$ENTRY/d" $DLLCONFIG \
-               || errlog "could not remove $ENTRY from $DLLCONFIG"  \
-               || return 0
+       DLLCONFIG=${DLLCONFIG:="/etc/sane.d/dll.conf"}
 
-       echo "remove $ENTRY from $DLLCONFIG"
+       find_config || return 0
+       delete_entries
 }
 
 main
\ No newline at end of file
index 0670d781d869ec315a679a2c59993cab3d019bfa..a53e5514e862a79652858addbc50a2bacef105b1 100644 (file)
@@ -37,6 +37,7 @@ add_custom_target(runningtests
        DEPENDS tests)
 add_custom_target(runningbatstests
        ALL ./postinst.bats
+       COMMAND ./postrm.bats
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
 add_custom_command(TARGET runningtests
        POST_BUILD
diff --git a/tests/postrm.bats b/tests/postrm.bats
new file mode 100755 (executable)
index 0000000..8c46de6
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/bats
+
+setup() {
+       bats_load_library bats-support
+       bats_load_library bats-assert
+       mkdir --parents ../build
+       echo kds_s2000w_net > ../build/dllpostrm.conf
+}
+
+teardown() {
+       rm -rf ../build/dllpostrm.conf
+       unset DLLCONFIG
+}
+
+@test "ignore file does not exist" {
+       export DLLCONFIG="../build/dll-not-exists.conf"
+
+       run ../src/debian/postrm
+
+       assert_success
+       assert_output "WARNING: ../build/dll-not-exists.conf does not exist"
+}
+
+@test "delete one entry kds_s2000w_net from dll.conf {
+       export DLLCONFIG="../build/dllpostrm.conf"
+
+       run ../src/debian/postrm
+
+       assert_success
+       assert_line --index 0 "found ../build/dllpostrm.conf"
+       assert_line --index 1 "remove kds_s2000w_net from ../build/dllpostrm.conf"
+       assert_equal "$(cat ../build/dllpostrm.conf)" ""
+       assert_equal "$(cat ../build/dllpostrm.conf | wc --lines)" "0"
+}
+
+@test "delete multiple entry kds_s2000w_net from dll.conf {
+       echo kds_s2000w_net >> ../build/dllpostrm.conf
+       echo kds_s2000w_net >> ../build/dllpostrm.conf
+       echo kds_s2000w_net >> ../build/dllpostrm.conf
+       export DLLCONFIG="../build/dllpostrm.conf"
+
+       run ../src/debian/postrm
+       run ../src/debian/postrm
+       run ../src/debian/postrm
+
+       assert_success
+       assert_line --index 0 "found ../build/dllpostrm.conf"
+       assert_line --index 1 "remove kds_s2000w_net from ../build/dllpostrm.conf"
+       assert_equal "$(cat ../build/dllpostrm.conf)" ""
+       assert_equal "$(cat ../build/dllpostrm.conf | wc --lines)" "0"
+}
\ No newline at end of file