From 32212c62672b96ea7685da01b2dd4196a71f8221 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 26 Sep 2026 11:22:04 +0200 Subject: [PATCH] optimize test performance with mocking --- src/albentohandy | 13 ++- tests/albentohandy.bats | 246 ++++++++++++++++++++++++++++++++++++++-- tests/test.conf | 2 - 3 files changed, 245 insertions(+), 16 deletions(-) delete mode 100644 tests/test.conf diff --git a/src/albentohandy b/src/albentohandy index 789f896..0e22cb4 100755 --- a/src/albentohandy +++ b/src/albentohandy @@ -66,6 +66,14 @@ load_library() || return $? } +check_albumlist() +{ + local albumlist="$1" + [ -f "$albumlist" ] \ + || errlog "file $albumlist does not exists" \ + || return $? +} + make_symlinks_to_work() { local albumlist="$1" @@ -137,10 +145,7 @@ main() done load_library "$lib" || return $? - - [ -f "$albumlist" ] \ - || errlog "file $albumlist does not exists" \ - || return $? + check_albumlist "$albumlist" || return $? mkdir --parents "$OUTPUT_PATH" || return $? make_symlinks_to_work "$albumlist" || return $? diff --git a/tests/albentohandy.bats b/tests/albentohandy.bats index 14f207a..604b6e8 100755 --- a/tests/albentohandy.bats +++ b/tests/albentohandy.bats @@ -3,18 +3,13 @@ setup() { bats_load_library 'bats-support' bats_load_library 'bats-assert' - mkdir --parents "build/tests/lib" - mkdir --parents "build/tests/music/Alben/Artist1" - mkdir --parents "build/tests/output" - touch "build/tests/lib/vorbis.sh" - echo "Alben/Artist1" > "build/tests/albumlist" - echo "testfile" > "build/tests/music/Alben/Artist1/file.flac" - echo "cover" > "build/tests/music/Alben/Artist1/cover.jpg" - export TEST_MODE="ON" LIB_PATH="build/tests/lib" + export TEST_MODE="ON" export CONFIG_FILE="tests/test.conf" export ENCODE_COUNT=0 export ENCODE_LOG="build/tests/encode.log" + export MUSIC_PATH="build/tests/music" + export OUTPUT_PATH="build/tests/output" source src/albentohandy encode() { ((ENCODE_COUNT--)) @@ -42,6 +37,8 @@ teardown() { unset LIB_PATH unset ENCODE_COUNT unset ENCODE_LOG + unset MUSIC_PATH + unset OUTPUT_PATH } # bats test_tags=albentohandy:check_programs @@ -80,6 +77,8 @@ teardown() { # bats test_tags=albentohandy:load_config @test "failed: missing MUSIC_PATH" { + unset MUSIC_PATH + mkdir --parents "build/tests" CONFIG_FILE="build/tests/albentohandy.conf" echo "" > "$CONFIG_FILE" @@ -91,6 +90,7 @@ teardown() { # bats test_tags=albentohandy:load_config @test "failed: directory MUSIC_PATH does not exists" { + mkdir --parents "build/tests" CONFIG_FILE="build/tests/albentohandy.conf" echo "MUSIC_PATH=build/tests/not_exists" > "$CONFIG_FILE" @@ -102,6 +102,9 @@ teardown() { # bats test_tags=albentohandy:load_library,albentohandy:options @test "failed: lib vorbis.sh not found" { + load_config() { + : + } LIB_PATH="build/tests/lib1" run main @@ -112,6 +115,9 @@ teardown() { # bats test_tags=albentohandy:load_library,albentohandy:options @test "failed: lib mp3.sh not found" { + load_config() { + : + } LIB_PATH="build/tests/lib" run main -3 @@ -122,6 +128,9 @@ teardown() { # bats test_tags=albentohandy:load_library,albentohandy:options @test "failed: lib flac.sh not found" { + load_config() { + : + } LIB_PATH="build/tests/lib" run main -f @@ -132,6 +141,9 @@ teardown() { # bats test_tags=albentohandy:load_library,albentohandy:options @test "failed: lib opus.sh not found" { + load_config() { + : + } LIB_PATH="build/tests/lib" run main -o @@ -142,6 +154,10 @@ teardown() { # bats test_tags=albentohandy:options @test "failed: unknown option" { + load_config() { + : + } + run main -x assert_failure @@ -150,8 +166,8 @@ teardown() { # bats test_tags=albentohandy:load_library @test "failed: could not load library" { - MUSIC_PATH="build/tests/music" - OUTPUT_PATH="build/tests/output" + mkdir --parents "build/tests/lib" + touch "build/tests/lib/vorbis.sh" load_config() { : } @@ -167,6 +183,13 @@ teardown() { # bats test_tags=albentohandy:input @test "failed: albumlist not found" { + load_config() { + : + } + load_library() { + : + } + run main assert_failure @@ -175,6 +198,13 @@ teardown() { # bats test_tags=albentohandy:input @test "failed: input file asdf not found" { + load_config() { + : + } + load_library() { + : + } + run main -i "asdf" assert_failure @@ -183,9 +213,18 @@ teardown() { # bats test_tags=albentohandy:output @test "success: create output path" { + load_config() { + : + } + load_library() { + : + } make_symlinks_to_work() { : } + check_albumlist() { + : + } run main -i "build/tests/albumlist" @@ -195,7 +234,14 @@ teardown() { # bats test_tags=albentohandy:input @test "failed: directory build/tests/music/Alben/Artist2 does not exists" { + mkdir --parents "build/tests" echo "Alben/Artist2" > "build/tests/albumlist" + load_config() { + : + } + load_library() { + : + } run main -i "build/tests/albumlist" @@ -208,6 +254,15 @@ teardown() { cleanup_flac() { : } + load_config() { + MUSIC_PATH=$(realpath "$MUSIC_PATH") + } + load_library() { + : + } + mkdir --parents "build/tests/music/Artist1" + echo "testfile" > "build/tests/music/Artist1/file.flac" + echo "Artist1" > "build/tests/albumlist" run main -i "build/tests/albumlist" @@ -217,9 +272,17 @@ teardown() { # bats test_tags=albentohandy:copy @test "failed: create symlink for Alben/Artist1/file.flac" { + load_config() { + : + } + load_library() { + : + } cp() { return 1 } + mkdir --parents "build/tests/music/Alben/Artist1" + echo "Alben/Artist1" > "build/tests/albumlist" run main -i "build/tests/albumlist" @@ -235,6 +298,16 @@ teardown() { cleanup_flac() { : } + load_config() { + MUSIC_PATH=$(realpath "$MUSIC_PATH") + } + load_library() { + : + } + mkdir --parents "build/tests/music/Alben/Artist1" + echo "testfile" > "build/tests/music/Alben/Artist1/file.flac" + echo "coverfile" > "build/tests/music/Alben/Artist1/cover.jpg" + echo "Alben/Artist1" > "build/tests/albumlist" run main -i "build/tests/albumlist" @@ -246,9 +319,24 @@ teardown() { # bats test_tags=albentohandy:copy @test "failed: copy cover.jpg for Alben/Artist1" { + cleanup_cover() { + : + } + cleanup_flac() { + : + } + load_config() { + MUSIC_PATH=$(realpath "$MUSIC_PATH") + } + load_library() { + : + } rsync() { return 1 } + mkdir --parents "build/tests/music/Alben/Artist1" + echo "testfile" > "build/tests/music/Alben/Artist1/file.flac" + echo "Alben/Artist1" > "build/tests/albumlist" run main -i "build/tests/albumlist" @@ -258,6 +346,19 @@ teardown() { # bats test_tags=albentohandy:encode @test "success: call encode" { + load_config() { + : + } + load_library() { + : + } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + ENCODE_COUNT=1 run main -i "build/tests/albumlist" @@ -269,6 +370,18 @@ teardown() { # bats test_tags=albentohandy:encode @test "failed: could not encode" { + load_config() { + : + } + load_library() { + : + } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } encode() { return 1 } @@ -281,8 +394,24 @@ teardown() { # bats test_tags=albentohandy:cleanup @test "success: cleanup cover" { + load_config() { + : + } + load_library() { + : + } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + encode() { + : + } mkdir --parents "build/tests/output/Artist1/" echo "metadatacover" > "build/tests/output/Artist1/cover.txt" + echo "metadatacover" > "build/tests/output/Artist1/cover.jpg" run main -i "build/tests/albumlist" @@ -293,6 +422,21 @@ teardown() { # bats test_tags=albentohandy:cleanup @test "failed: cleanup cover" { + load_config() { + : + } + load_library() { + : + } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + encode() { + : + } cleanup_cover() { return 1 } @@ -305,6 +449,27 @@ teardown() { # bats test_tags=albentohandy:cleanup @test "success: cleanup flac" { + load_config() { + : + } + load_library() { + : + } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + encode() { + : + } + cleanup_cover() { + : + } + mkdir --parents "build/tests/output/Artist1" + echo "testfile" > "build/tests/output/Artist1/file.flac" + run main -i "build/tests/albumlist" assert_success @@ -313,9 +478,26 @@ teardown() { # bats test_tags=albentohandy:cleanup @test "success: not cleanup flac with flac lib" { + load_config() { + : + } load_library() { : } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + encode() { + : + } + cleanup_cover() { + : + } + mkdir --parents "build/tests/output/Artist1" + echo "testfile" > "build/tests/output/Artist1/file.flac" run main -i "build/tests/albumlist" -f @@ -325,9 +507,24 @@ teardown() { # bats test_tags=albentohandy:cleanup @test "failed: could not cleanup flac" { + load_config() { + : + } load_library() { : } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + encode() { + : + } + cleanup_cover() { + : + } cleanup_flac() { return 1 } @@ -340,6 +537,27 @@ teardown() { # bats test_tags=albentohandy:checksum @test "success: create checksum" { + load_config() { + : + } + load_library() { + : + } + check_albumlist() { + : + } + make_symlinks_to_work() { + : + } + encode() { + : + } + cleanup_cover() { + : + } + cleanup_flac() { + : + } mkdir --parents "build/tests/output/Artist1/Album1" echo "testfile3" > "build/tests/output/Artist1/Album1/blub.ogg" echo "testfile2" > "build/tests/output/Artist1/Album1/02.ogg" @@ -358,6 +576,10 @@ teardown() { # bats test_tags=albentohandy:usage @test "success: print usage" { + load_config() { + : + } + run main -h assert_success @@ -374,6 +596,10 @@ teardown() { # bats test_tags=albentohandy:usage @test "failure: print usage by unknown command" { + load_config() { + : + } + run main -x assert_failure diff --git a/tests/test.conf b/tests/test.conf deleted file mode 100644 index be8ab0b..0000000 --- a/tests/test.conf +++ /dev/null @@ -1,2 +0,0 @@ -MUSIC_PATH="build/tests/music" -OUTPUT_PATH="build/tests/output" \ No newline at end of file -- 2.47.3