mirror of
https://github.com/levogevo/add-synced-lyrics.git
synced 2026-07-21 21:45:21 +00:00
increase coverage
This commit is contained in:
+20
-19
@@ -76,13 +76,9 @@ process_inputs() {
|
|||||||
output=''
|
output=''
|
||||||
output="$(set_lyric_file_name "${input}")"
|
output="$(set_lyric_file_name "${input}")"
|
||||||
|
|
||||||
if [[ -f "${output}" ]]; then
|
if validate_lrc "${output}"; then
|
||||||
if is_valid_lrc "${output}"; then
|
echo_pass "${input} already has lyrics, skipping"
|
||||||
echo_pass "${input} already has lyrics, skipping"
|
continue
|
||||||
continue
|
|
||||||
else
|
|
||||||
rm "${output}" || return 1
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
isrc=''
|
isrc=''
|
||||||
@@ -93,24 +89,29 @@ process_inputs() {
|
|||||||
|
|
||||||
local tmpOutput="${TMPDIR}/tmp.lrc"
|
local tmpOutput="${TMPDIR}/tmp.lrc"
|
||||||
for func in "${downloadFuncs[@]}"; do
|
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
|
done
|
||||||
|
|
||||||
if [[ -f "${tmpOutput}" ]]; then
|
# skip processing since no files should be modified
|
||||||
if is_valid_lrc "${tmpOutput}"; then
|
[[ ${DRY_RUN} == true ]] && continue
|
||||||
mv "${tmpOutput}" "${output}" &>/dev/null || return 1
|
|
||||||
else
|
|
||||||
rm "${tmpOutput}" || return 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
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}"
|
echo_fail "could not find lyrics for ${input}"
|
||||||
ret=1
|
ret=1
|
||||||
continue
|
|
||||||
else
|
|
||||||
void echo_pass "added lyrics for ${input}"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
return ${ret}
|
return ${ret}
|
||||||
|
|||||||
+16
-5
@@ -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() {
|
void() {
|
||||||
if [[ ${DRY_RUN} == true ]]; then
|
if [[ ${DRY_RUN} == true ]]; then
|
||||||
return 0
|
return 0
|
||||||
@@ -138,6 +138,8 @@ cache_command() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create_tmp_dir() {
|
create_tmp_dir() {
|
||||||
|
[[ -n "${TMPDIR:-}" ]] && return
|
||||||
|
|
||||||
TMPDIR="$(mktemp -d)" || return 1
|
TMPDIR="$(mktemp -d)" || return 1
|
||||||
readonly TMPDIR
|
readonly TMPDIR
|
||||||
trap 'rm -rf ${TMPDIR}' EXIT
|
trap 'rm -rf ${TMPDIR}' EXIT
|
||||||
@@ -263,18 +265,27 @@ set_lyric_file_name() {
|
|||||||
echo "${file%.opus}.lrc"
|
echo "${file%.opus}.lrc"
|
||||||
}
|
}
|
||||||
|
|
||||||
is_valid_lrc() {
|
validate_lrc() {
|
||||||
local file="$1"
|
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
|
while read -r line; do
|
||||||
[[ "${#line}" -eq 0 ]] && continue
|
[[ "${#line}" -eq 0 ]] && continue
|
||||||
if [[ "${line}" != '['* ]]; then
|
if [[ "${line}" != '['* && "${line}" != '#'* ]]; then
|
||||||
valid=1
|
valid=1
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
valid=0
|
|
||||||
done <"${file}"
|
done <"${file}"
|
||||||
|
|
||||||
|
if [[ ${valid} -eq 1 ]]; then
|
||||||
|
debug echo_fail "${file} is not valid lrc file"
|
||||||
|
dry rm "${file}" || return 1
|
||||||
|
fi
|
||||||
|
|
||||||
return ${valid}
|
return ${valid}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ OPTIONS:
|
|||||||
-d, --dir process files in directory
|
-d, --dir process files in directory
|
||||||
-r, --recurse recursively process directory
|
-r, --recurse recursively process directory
|
||||||
-f, --file process one file
|
-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
|
just show the files that would be processed
|
||||||
-e, --eval output bash functions that can be directly processed
|
-e, --eval output bash functions that can be directly processed
|
||||||
by eval for use with testing this script.
|
by eval for use with testing this script.
|
||||||
|
|||||||
@@ -149,6 +149,30 @@ test_determine_inputs_recurse() {
|
|||||||
RECURSE=true test_determine_inputs
|
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() {
|
test_file_dry() {
|
||||||
add-synced-lyrics --file "${GOOD_SONG}" --dry-run
|
add-synced-lyrics --file "${GOOD_SONG}" --dry-run
|
||||||
}
|
}
|
||||||
@@ -162,14 +186,16 @@ test_directory_recurse_dry() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test_good_file() {
|
test_good_file() {
|
||||||
add-synced-lyrics --file "${GOOD_SONG}"
|
add-synced-lyrics --file "${GOOD_SONG}" --debug
|
||||||
test -f "${GOOD_LYRICS}"
|
test -f "${GOOD_LYRICS}"
|
||||||
}
|
}
|
||||||
|
|
||||||
test_bad_file() {
|
test_bad_file() {
|
||||||
run_test_cmd \
|
run_test_cmd \
|
||||||
1 \
|
1 \
|
||||||
add-synced-lyrics --file "${BAD_SONG}"
|
add-synced-lyrics \
|
||||||
|
--file "${BAD_SONG}" \
|
||||||
|
--debug
|
||||||
run_test_cmd \
|
run_test_cmd \
|
||||||
1 \
|
1 \
|
||||||
test -f "${BAD_LYRICS}"
|
test -f "${BAD_LYRICS}"
|
||||||
@@ -196,6 +222,12 @@ test_directory_recurse_ignore() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_missing_utils() {
|
||||||
|
PATH="$(bash_dirname "$(command -v bash)")" run_test_cmd \
|
||||||
|
1 \
|
||||||
|
add-synced-lyrics
|
||||||
|
}
|
||||||
|
|
||||||
setup_test_data() {
|
setup_test_data() {
|
||||||
test -d "${TEST_DIR}" && rm -rf "${TEST_DIR}"
|
test -d "${TEST_DIR}" && rm -rf "${TEST_DIR}"
|
||||||
mkdir -p "${TEST_DIR}/subdir"
|
mkdir -p "${TEST_DIR}/subdir"
|
||||||
@@ -218,6 +250,8 @@ setup_test_data() {
|
|||||||
TESTS=(
|
TESTS=(
|
||||||
test_determine_inputs
|
test_determine_inputs
|
||||||
test_determine_inputs_recurse
|
test_determine_inputs_recurse
|
||||||
|
test_determine_inputs_find
|
||||||
|
test_determine_inputs_find_recurse
|
||||||
test_bad_input
|
test_bad_input
|
||||||
test_directory_dry
|
test_directory_dry
|
||||||
test_directory_recurse_dry
|
test_directory_recurse_dry
|
||||||
@@ -226,6 +260,7 @@ TESTS=(
|
|||||||
test_bad_file
|
test_bad_file
|
||||||
test_directory_recurse
|
test_directory_recurse
|
||||||
test_directory_recurse_ignore
|
test_directory_recurse_ignore
|
||||||
|
test_missing_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
# make sure no test is accidentally missed
|
# make sure no test is accidentally missed
|
||||||
@@ -338,6 +373,7 @@ if [[ $# -eq 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
echo
|
||||||
analyze_coverage
|
analyze_coverage
|
||||||
else
|
else
|
||||||
"$@"
|
"$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user