diff --git a/add-synced-lyrics.sh b/add-synced-lyrics.sh index 63a05bc..9251b22 100755 --- a/add-synced-lyrics.sh +++ b/add-synced-lyrics.sh @@ -76,13 +76,9 @@ process_inputs() { output='' output="$(set_lyric_file_name "${input}")" - if [[ -f "${output}" ]]; then - if is_valid_lrc "${output}"; then - echo_pass "${input} already has lyrics, skipping" - continue - else - rm "${output}" || return 1 - fi + if validate_lrc "${output}"; then + echo_pass "${input} already has lyrics, skipping" + continue fi isrc='' @@ -93,24 +89,29 @@ process_inputs() { local tmpOutput="${TMPDIR}/tmp.lrc" for func in "${downloadFuncs[@]}"; do - "${func}" "${isrc}" "${tmpOutput}" && break + # try to download + if ! "${func}" "${isrc}" "${tmpOutput}"; then + continue + fi + + # validate + if validate_lrc "${tmpOutput}"; then + # passed download and validation, stop processing + break + fi done - if [[ -f "${tmpOutput}" ]]; then - if is_valid_lrc "${tmpOutput}"; then - mv "${tmpOutput}" "${output}" &>/dev/null || return 1 - else - rm "${tmpOutput}" || return 1 - fi - fi + # skip processing since no files should be modified + [[ ${DRY_RUN} == true ]] && continue - if ! void test -f "${output}"; then + if [[ -f "${tmpOutput}" ]]; then + mv "${tmpOutput}" "${output}" &>/dev/null || return 1 + echo_pass "added lyrics for ${input}" + else echo_fail "could not find lyrics for ${input}" ret=1 - continue - else - void echo_pass "added lyrics for ${input}" fi + done return ${ret} diff --git a/lib/0-utils.sh b/lib/0-utils.sh index 2e44dc8..ccf4ea8 100644 --- a/lib/0-utils.sh +++ b/lib/0-utils.sh @@ -55,7 +55,7 @@ debug() { "$@" } -# only perform the command given if ${DRY_RUN} is enabled +# only perform the command given if ${DRY_RUN} is disabled void() { if [[ ${DRY_RUN} == true ]]; then return 0 @@ -138,6 +138,8 @@ cache_command() { } create_tmp_dir() { + [[ -n "${TMPDIR:-}" ]] && return + TMPDIR="$(mktemp -d)" || return 1 readonly TMPDIR trap 'rm -rf ${TMPDIR}' EXIT @@ -263,18 +265,27 @@ set_lyric_file_name() { echo "${file%.opus}.lrc" } -is_valid_lrc() { +validate_lrc() { local file="$1" - local valid=1 + # file does not exist, is invalid + if [[ ! -f "${file}" ]]; then + return 1 + fi + + local valid=0 while read -r line; do [[ "${#line}" -eq 0 ]] && continue - if [[ "${line}" != '['* ]]; then + if [[ "${line}" != '['* && "${line}" != '#'* ]]; then valid=1 break fi - valid=0 done <"${file}" + if [[ ${valid} -eq 1 ]]; then + debug echo_fail "${file} is not valid lrc file" + dry rm "${file}" || return 1 + fi + return ${valid} } diff --git a/lib/options.sh b/lib/options.sh index dd62b79..6da63d9 100644 --- a/lib/options.sh +++ b/lib/options.sh @@ -10,7 +10,7 @@ OPTIONS: -d, --dir process files in directory -r, --recurse recursively process directory -f, --file process one file - -n, --dry-run do not actually add synced lyrics, + -n, --dry-run do not add synced lyrics, just show the files that would be processed -e, --eval output bash functions that can be directly processed by eval for use with testing this script. diff --git a/test-add-synced-lyrics.sh b/test-add-synced-lyrics.sh index 52be64e..57cd4cf 100755 --- a/test-add-synced-lyrics.sh +++ b/test-add-synced-lyrics.sh @@ -149,6 +149,30 @@ test_determine_inputs_recurse() { RECURSE=true test_determine_inputs } +test_determine_inputs_find() { + local utils=( + base64 + cat + ffprobe + jq + librelyrics + spotify + ) + local path=() + for util in "${utils[@]}"; do + path+=("$(bash_dirname "$(command -v "${util}")")") + done + IFS=':' moddedPath="${path[*]}" + PATH="${moddedPath}" \ + add-synced-lyrics \ + --dir "${TEST_DIR}" \ + --dry-run "$@" +} + +test_determine_inputs_find_recurse() { + test_determine_inputs_find --recurse +} + test_file_dry() { add-synced-lyrics --file "${GOOD_SONG}" --dry-run } @@ -162,14 +186,16 @@ test_directory_recurse_dry() { } test_good_file() { - add-synced-lyrics --file "${GOOD_SONG}" + add-synced-lyrics --file "${GOOD_SONG}" --debug test -f "${GOOD_LYRICS}" } test_bad_file() { run_test_cmd \ 1 \ - add-synced-lyrics --file "${BAD_SONG}" + add-synced-lyrics \ + --file "${BAD_SONG}" \ + --debug run_test_cmd \ 1 \ test -f "${BAD_LYRICS}" @@ -196,6 +222,12 @@ test_directory_recurse_ignore() { return 0 } +test_missing_utils() { + PATH="$(bash_dirname "$(command -v bash)")" run_test_cmd \ + 1 \ + add-synced-lyrics +} + setup_test_data() { test -d "${TEST_DIR}" && rm -rf "${TEST_DIR}" mkdir -p "${TEST_DIR}/subdir" @@ -218,6 +250,8 @@ setup_test_data() { TESTS=( test_determine_inputs test_determine_inputs_recurse + test_determine_inputs_find + test_determine_inputs_find_recurse test_bad_input test_directory_dry test_directory_recurse_dry @@ -226,6 +260,7 @@ TESTS=( test_bad_file test_directory_recurse test_directory_recurse_ignore + test_missing_utils ) # make sure no test is accidentally missed @@ -338,6 +373,7 @@ if [[ $# -eq 0 ]]; then fi done + echo analyze_coverage else "$@"