From 5c694840de80684aaad1eacce140b3ee19c857f0 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Thu, 16 Jul 2026 20:11:07 +0200 Subject: [PATCH] add postrm script with tests --- src/debian/postrm | 37 ++++++++++++-------------------- tests/CMakeLists.txt | 1 + tests/postrm.bats | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 23 deletions(-) create mode 100755 tests/postrm.bats diff --git a/src/debian/postrm b/src/debian/postrm index b6589bb..1ffbafa 100755 --- a/src/debian/postrm +++ b/src/debian/postrm @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0670d78..a53e551 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 index 0000000..8c46de6 --- /dev/null +++ b/tests/postrm.bats @@ -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 -- 2.47.3